From c8e27956e64db7d095c51570d5ebb0c8cd9cafd1 Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Tue, 25 Apr 2017 10:56:55 +0900 Subject: [PATCH] Fix queries and permissions for the repmgr extension --- repmgr-client.c | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/repmgr-client.c b/repmgr-client.c index 35981878..58b2eb01 100644 --- a/repmgr-client.c +++ b/repmgr-client.c @@ -425,6 +425,7 @@ do_help(void) printf(_(" -b, --pg_bindir=PATH path to PostgreSQL binaries (optional)\n")); printf(_(" -f, --config-file=PATH path to the configuration file\n")); printf(_(" -F, --force force potentially dangerous operations to happen\n")); + printf(_(" -S, --superuser=USERNAME superuser to use if repmgr user is not superuser\n")); puts(""); printf(_("Logging options:\n")); printf(_(" -L, --log-level set log level (overrides configuration file; default: NOTICE)\n")); @@ -623,7 +624,16 @@ bool create_repmgr_extension(PGconn *conn) return false; } - /* 1. Check if extension installed */ + + /* 1. Check extension is actually available */ + + if (PQntuples(res) == 0) + { + log_error(_("\"repmgr\" extension is not available")); + return false; + } + + /* 2. Check if extension installed */ if (PQgetisnull(res, 0, 1) == 0) { /* TODO: check version */ @@ -631,13 +641,7 @@ bool create_repmgr_extension(PGconn *conn) return true; } - /* 2. If not, check extension available */ - if (PQgetisnull(res, 0, 0) == 1) - { - log_error(_("\"repmgr\" extension is not available")); - return false; - } PQclear(res); termPQExpBuffer(&query); @@ -665,7 +669,8 @@ bool create_repmgr_extension(PGconn *conn) if (PQstatus(superuser_conn) != CONNECTION_OK) { - log_error(_("unable to establish superuser connection as \"%s\""), runtime_options.superuser); + log_error(_("unable to establish superuser connection as \"%s\""), + runtime_options.superuser); return false; } @@ -713,6 +718,26 @@ bool create_repmgr_extension(PGconn *conn) { initPQExpBuffer(&query); + appendPQExpBuffer(&query, + "GRANT USAGE ON SCHEMA repmgr TO %s", + current_user); + res = PQexec(schema_create_conn, query.data); + + termPQExpBuffer(&query); + if (PQresultStatus(res) != PGRES_COMMAND_OK) + { + log_error(_("unable to grant usage on \"repmgr\" extension to %s:\n %s"), + current_user, + PQerrorMessage(schema_create_conn)); + PQclear(res); + + if (superuser_conn != 0) + PQfinish(superuser_conn); + + return false; + } + + initPQExpBuffer(&query); appendPQExpBuffer(&query, "GRANT ALL ON ALL TABLES IN SCHEMA repmgr TO %s", current_user); @@ -722,7 +747,7 @@ bool create_repmgr_extension(PGconn *conn) if (PQresultStatus(res) != PGRES_COMMAND_OK) { - log_error(_("unable to grant usage on \"repmgr\" extension to %s:\n %s"), + log_error(_("unable to grant permission on tables on \"repmgr\" extension to %s:\n %s"), current_user, PQerrorMessage(schema_create_conn)); PQclear(res);