From 81301b85781245676f874ae3908015f05d3aaaa2 Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Sat, 7 May 2011 08:55:13 -0400
Subject: [PATCH] Check that the pg_upgrade user specified is a super-user.

Also report the error message when the post-pg_ctl connection fails.

Per private bug report from EnterpriseDB.
---
 contrib/pg_upgrade/check.c  | 29 ++++++++++++++++++++++++++++-
 contrib/pg_upgrade/server.c |  6 ++++--
 2 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/contrib/pg_upgrade/check.c b/contrib/pg_upgrade/check.c
index 35b178e8286..26dec39b35b 100644
--- a/contrib/pg_upgrade/check.c
+++ b/contrib/pg_upgrade/check.c
@@ -15,6 +15,7 @@ static void check_new_cluster_is_empty(void);
 static void check_old_cluster_has_new_cluster_dbs(void);
 static void check_locale_and_encoding(ControlData *oldctrl,
 						  ControlData *newctrl);
+static void check_is_super_user(ClusterInfo *cluster);
 static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster);
 static void check_for_reg_data_type_usage(ClusterInfo *cluster);
 
@@ -63,7 +64,7 @@ check_old_cluster(bool live_check,
 	/*
 	 * Check for various failure cases
 	 */
-
+	check_is_super_user(&old_cluster);
 	check_for_reg_data_type_usage(&old_cluster);
 	check_for_isn_and_int8_passing_mismatch(&old_cluster);
 
@@ -472,6 +473,32 @@ create_script_for_old_cluster_deletion(
 }
 
 
+/*
+ *	check_is_super_user()
+ *
+ *	Make sure we are the super-user.
+ */
+static void
+check_is_super_user(ClusterInfo *cluster)
+{
+	PGresult   *res;
+	PGconn	   *conn = connectToServer(cluster, "template1");
+
+	/* Can't use pg_authid because only superusers can view it. */
+	res = executeQueryOrDie(conn,
+							"SELECT rolsuper "
+							"FROM pg_catalog.pg_roles "
+							"WHERE rolname = current_user");
+
+	if (PQntuples(res) != 1 || strcmp(PQgetvalue(res, 0, 0), "t") != 0)
+		pg_log(PG_FATAL, "the database user is not a superuser\n");
+
+	PQclear(res);
+
+	PQfinish(conn);
+}
+
+
 /*
  *	check_for_isn_and_int8_passing_mismatch()
  *
diff --git a/contrib/pg_upgrade/server.c b/contrib/pg_upgrade/server.c
index 9a550756e56..d6efe9af6b4 100644
--- a/contrib/pg_upgrade/server.c
+++ b/contrib/pg_upgrade/server.c
@@ -27,7 +27,7 @@ connectToServer(ClusterInfo *cluster, const char *db_name)
 
 	if (conn == NULL || PQstatus(conn) != CONNECTION_OK)
 	{
-		pg_log(PG_REPORT, "Connection to database failed: %s\n",
+		pg_log(PG_REPORT, "connection to database failed: %s\n",
 			   PQerrorMessage(conn));
 
 		if (conn)
@@ -189,7 +189,9 @@ start_postmaster(ClusterInfo *cluster)
 	if ((conn = get_db_conn(cluster, "template1")) == NULL ||
 		PQstatus(conn) != CONNECTION_OK)
 	{
-		if (conn)
+		pg_log(PG_REPORT, "\nconnection to database failed: %s\n",
+			   PQerrorMessage(conn));
+ 		if (conn)
 			PQfinish(conn);
 		pg_log(PG_FATAL, "unable to connect to %s postmaster started with the command: %s\n"
 			   "Perhaps pg_hba.conf was not set to \"trust\".\n",
-- 
GitLab