a21a05
diff -ru postgresql-9.2.24/src/bin/pg_dump/pg_backup_archiver.c postgresql-9.2.24_new/src/bin/pg_dump/pg_backup_archiver.c
a21a05
--- postgresql-9.2.24/src/bin/pg_dump/pg_backup_archiver.c	2017-11-06 23:17:39.000000000 +0100
a21a05
+++ postgresql-9.2.24_new/src/bin/pg_dump/pg_backup_archiver.c	2021-04-13 08:08:24.730000000 +0200
a21a05
@@ -368,9 +368,7 @@
a21a05
 		AHX->minRemoteVersion = 0;
a21a05
 		AHX->maxRemoteVersion = 999999;
a21a05
 
a21a05
-		ConnectDatabase(AHX, ropt->dbname,
a21a05
-						ropt->pghost, ropt->pgport, ropt->username,
a21a05
-						ropt->promptPassword);
a21a05
+		ConnectDatabase(AHX, &ropt->cparams, false);
a21a05
 
a21a05
 		/*
a21a05
 		 * If we're talking to the DB directly, don't send comments since they
a21a05
@@ -640,16 +638,8 @@
a21a05
 		/* If we created a DB, connect to it... */
a21a05
 		if (strcmp(te->desc, "DATABASE") == 0)
a21a05
 		{
a21a05
-			PQExpBufferData connstr;
a21a05
-
a21a05
-			initPQExpBuffer(&connstr);
a21a05
-			appendPQExpBufferStr(&connstr, "dbname=");
a21a05
-			appendConnStrVal(&connstr, te->tag);
a21a05
-			/* Abandon struct, but keep its buffer until process exit. */
a21a05
-
a21a05
 			ahlog(AH, 1, "connecting to new database \"%s\"\n", te->tag);
a21a05
 			_reconnectToDB(AH, te->tag);
a21a05
-			ropt->dbname = connstr.data;
a21a05
 		}
a21a05
 	}
a21a05
 
a21a05
@@ -778,7 +768,7 @@
a21a05
 
a21a05
 	/* set any fields that shouldn't default to zeroes */
a21a05
 	opts->format = archUnknown;
a21a05
-	opts->promptPassword = TRI_DEFAULT;
a21a05
+	opts->cparams.promptPassword = TRI_DEFAULT;
a21a05
 	opts->dumpSections = DUMP_UNSECTIONED;
a21a05
 	opts->number_of_jobs = 1;
a21a05
 
a21a05
@@ -2155,8 +2145,6 @@
a21a05
 	else
a21a05
 		AH->format = fmt;
a21a05
 
a21a05
-	AH->promptPassword = TRI_DEFAULT;
a21a05
-
a21a05
 	switch (AH->format)
a21a05
 	{
a21a05
 		case archCustom:
a21a05
@@ -2764,7 +2752,7 @@
a21a05
 _reconnectToDB(ArchiveHandle *AH, const char *dbname)
a21a05
 {
a21a05
 	if (RestoringToDB(AH))
a21a05
-		ReconnectToServer(AH, dbname, NULL);
a21a05
+		ReconnectToServer(AH, dbname);
a21a05
 	else
a21a05
 	{
a21a05
 		if (dbname)
a21a05
@@ -2777,7 +2765,13 @@
a21a05
 			termPQExpBuffer(&connectbuf);
a21a05
 		}
a21a05
 		else
a21a05
+		{
a21a05
+			PQExpBufferData connectbuf;
a21a05
+			initPQExpBuffer(&connectbuf);
a21a05
 			ahprintf(AH, "%s\n", "\\connect -\n");
a21a05
+			appendPsqlMetaConnect(&connectbuf, dbname);
a21a05
+			termPQExpBuffer(&connectbuf);
a21a05
+		}
a21a05
 	}
a21a05
 
a21a05
 	/*
a21a05
@@ -2798,7 +2792,8 @@
a21a05
 	AH->currWithOids = -1;
a21a05
 
a21a05
 	/* re-establish fixed state */
a21a05
-	_doSetFixedOutputState(AH);
a21a05
+	if (AH->mode == archModeRead)
a21a05
+		_doSetFixedOutputState(AH);
a21a05
 }
a21a05
 
a21a05
 /*
a21a05
@@ -3775,9 +3770,7 @@
a21a05
 	/*
a21a05
 	 * Now reconnect the single parent connection.
a21a05
 	 */
a21a05
-	ConnectDatabase((Archive *) AH, ropt->dbname,
a21a05
-					ropt->pghost, ropt->pgport, ropt->username,
a21a05
-					ropt->promptPassword);
a21a05
+	ConnectDatabase((Archive *) AH, &ropt->cparams, true);
a21a05
 
a21a05
 	_doSetFixedOutputState(AH);
a21a05
 
a21a05
@@ -4092,11 +4085,10 @@
a21a05
 	/*
a21a05
 	 * We need our own database connection, too
a21a05
 	 */
a21a05
-	ConnectDatabase((Archive *) AH, ropt->dbname,
a21a05
-					ropt->pghost, ropt->pgport, ropt->username,
a21a05
-					ropt->promptPassword);
a21a05
+	ConnectDatabase((Archive *) AH, &AH->ropt->cparams, true);
a21a05
 
a21a05
-	_doSetFixedOutputState(AH);
a21a05
+	if (AH->mode == archModeRead)
a21a05
+		_doSetFixedOutputState(AH);
a21a05
 
a21a05
 	/* Restore the TOC item */
a21a05
 	retval = restore_toc_entry(AH, te, ropt, true);
a21a05
diff -ru postgresql-9.2.24/src/bin/pg_dump/pg_backup_archiver.h postgresql-9.2.24_new/src/bin/pg_dump/pg_backup_archiver.h
a21a05
--- postgresql-9.2.24/src/bin/pg_dump/pg_backup_archiver.h	2017-11-06 23:17:39.000000000 +0100
a21a05
+++ postgresql-9.2.24_new/src/bin/pg_dump/pg_backup_archiver.h	2021-04-13 08:08:24.730000000 +0200
a21a05
@@ -234,7 +234,6 @@
a21a05
 
a21a05
 	/* Stuff for direct DB connection */
a21a05
 	char	   *archdbname;		/* DB name *read* from archive */
a21a05
-	enum trivalue promptPassword;
a21a05
 	char	   *savedPassword;	/* password for ropt->username, if known */
a21a05
 	PGconn	   *connection;
a21a05
 	int			connectToDB;	/* Flag to indicate if direct DB connection is
a21a05
@@ -372,7 +371,7 @@
a21a05
 
a21a05
 extern bool isValidTarHeader(char *header);
a21a05
 
a21a05
-extern int	ReconnectToServer(ArchiveHandle *AH, const char *dbname, const char *newUser);
a21a05
+extern void ReconnectToServer(ArchiveHandle *AH, const char *dbname);
a21a05
 extern void DropBlobIfExists(ArchiveHandle *AH, Oid oid);
a21a05
 
a21a05
 int			ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH);
a21a05
diff -ru postgresql-9.2.24/src/bin/pg_dump/pg_backup_db.c postgresql-9.2.24_new/src/bin/pg_dump/pg_backup_db.c
a21a05
--- postgresql-9.2.24/src/bin/pg_dump/pg_backup_db.c	2017-11-06 23:17:39.000000000 +0100
a21a05
+++ postgresql-9.2.24_new/src/bin/pg_dump/pg_backup_db.c	2021-04-13 08:08:54.527000000 +0200
a21a05
@@ -10,9 +10,12 @@
a21a05
  *-------------------------------------------------------------------------
a21a05
  */
a21a05
 
a21a05
-#include "pg_backup_db.h"
a21a05
+#include "postgres_fe.h"
a21a05
+
a21a05
 #include "dumpmem.h"
a21a05
 #include "dumputils.h"
a21a05
+#include "pg_backup_archiver.h"
a21a05
+#include "pg_backup_db.h"
a21a05
 
a21a05
 #include <unistd.h>
a21a05
 #include <ctype.h>
a21a05
@@ -27,7 +30,6 @@
a21a05
 static const char *modulename = gettext_noop("archiver (db)");
a21a05
 
a21a05
 static void _check_database_version(ArchiveHandle *AH);
a21a05
-static PGconn *_connectDB(ArchiveHandle *AH, const char *newdbname, const char *newUser);
a21a05
 static void notice_processor(void *arg, const char *message);
a21a05
 
a21a05
 static int
a21a05
@@ -74,160 +76,37 @@
a21a05
 
a21a05
 /*
a21a05
  * Reconnect to the server.  If dbname is not NULL, use that database,
a21a05
- * else the one associated with the archive handle.  If username is
a21a05
- * not NULL, use that user name, else the one from the handle.  If
a21a05
- * both the database and the user match the existing connection already,
a21a05
- * nothing will be done.
a21a05
- *
a21a05
- * Returns 1 in any case.
a21a05
- */
a21a05
-int
a21a05
-ReconnectToServer(ArchiveHandle *AH, const char *dbname, const char *username)
a21a05
-{
a21a05
-	PGconn	   *newConn;
a21a05
-	const char *newdbname;
a21a05
-	const char *newusername;
a21a05
-
a21a05
-	if (!dbname)
a21a05
-		newdbname = PQdb(AH->connection);
a21a05
-	else
a21a05
-		newdbname = dbname;
a21a05
-
a21a05
-	if (!username)
a21a05
-		newusername = PQuser(AH->connection);
a21a05
-	else
a21a05
-		newusername = username;
a21a05
-
a21a05
-	/* Let's see if the request is already satisfied */
a21a05
-	if (strcmp(newdbname, PQdb(AH->connection)) == 0 &&
a21a05
-		strcmp(newusername, PQuser(AH->connection)) == 0)
a21a05
-		return 1;
a21a05
-
a21a05
-	newConn = _connectDB(AH, newdbname, newusername);
a21a05
-
a21a05
-	PQfinish(AH->connection);
a21a05
-	AH->connection = newConn;
a21a05
-
a21a05
-	return 1;
a21a05
-}
a21a05
-
a21a05
-/*
a21a05
- * Connect to the db again.
a21a05
- *
a21a05
- * Note: it's not really all that sensible to use a single-entry password
a21a05
- * cache if the username keeps changing.  In current usage, however, the
a21a05
- * username never does change, so one savedPassword is sufficient.  We do
a21a05
- * update the cache on the off chance that the password has changed since the
a21a05
+ * else the one associated with the archive handle
a21a05
  * start of the run.
a21a05
  */
a21a05
-static PGconn *
a21a05
-_connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser)
a21a05
+void
a21a05
+ReconnectToServer(ArchiveHandle *AH, const char *dbname)
a21a05
 {
a21a05
-	PQExpBufferData connstr;
a21a05
-	PGconn	   *newConn;
a21a05
-	const char *newdb;
a21a05
-	const char *newuser;
a21a05
-	char	   *password = AH->savedPassword;
a21a05
-	bool		new_pass;
a21a05
-
a21a05
-	if (!reqdb)
a21a05
-		newdb = PQdb(AH->connection);
a21a05
-	else
a21a05
-		newdb = reqdb;
a21a05
-
a21a05
-	if (!requser || strlen(requser) == 0)
a21a05
-		newuser = PQuser(AH->connection);
a21a05
-	else
a21a05
-		newuser = requser;
a21a05
-
a21a05
-	ahlog(AH, 1, "connecting to database \"%s\" as user \"%s\"\n",
a21a05
-		  newdb, newuser);
a21a05
-
a21a05
-	if (AH->promptPassword == TRI_YES && password == NULL)
a21a05
-	{
a21a05
-		password = simple_prompt("Password: ", 100, false);
a21a05
-		if (password == NULL)
a21a05
-			exit_horribly(modulename, "out of memory\n");
a21a05
-	}
a21a05
-
a21a05
-	initPQExpBuffer(&connstr);
a21a05
-	appendPQExpBuffer(&connstr, "dbname=");
a21a05
-	appendConnStrVal(&connstr, newdb);
a21a05
+	PGconn         *oldConn = AH->connection;
a21a05
+	RestoreOptions *ropt = AH->ropt;
a21a05
 
a21a05
-	do
a21a05
-	{
a21a05
-#define PARAMS_ARRAY_SIZE	7
a21a05
-		const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
a21a05
-		const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
a21a05
-
a21a05
-		keywords[0] = "host";
a21a05
-		values[0] = PQhost(AH->connection);
a21a05
-		keywords[1] = "port";
a21a05
-		values[1] = PQport(AH->connection);
a21a05
-		keywords[2] = "user";
a21a05
-		values[2] = newuser;
a21a05
-		keywords[3] = "password";
a21a05
-		values[3] = password;
a21a05
-		keywords[4] = "dbname";
a21a05
-		values[4] = connstr.data;
a21a05
-		keywords[5] = "fallback_application_name";
a21a05
-		values[5] = progname;
a21a05
-		keywords[6] = NULL;
a21a05
-		values[6] = NULL;
a21a05
-
a21a05
-		new_pass = false;
a21a05
-		newConn = PQconnectdbParams(keywords, values, true);
a21a05
-
a21a05
-		free(keywords);
a21a05
-		free(values);
a21a05
-
a21a05
-		if (!newConn)
a21a05
-			exit_horribly(modulename, "failed to reconnect to database\n");
a21a05
-
a21a05
-		if (PQstatus(newConn) == CONNECTION_BAD)
a21a05
-		{
a21a05
-			if (!PQconnectionNeedsPassword(newConn))
a21a05
-				exit_horribly(modulename, "could not reconnect to database: %s",
a21a05
-							  PQerrorMessage(newConn));
a21a05
-			PQfinish(newConn);
a21a05
-
a21a05
-			if (password)
a21a05
-				fprintf(stderr, "Password incorrect\n");
a21a05
-
a21a05
-			fprintf(stderr, "Connecting to %s as %s\n",
a21a05
-					newdb, newuser);
a21a05
-
a21a05
-			if (password)
a21a05
-				free(password);
a21a05
-
a21a05
-			if (AH->promptPassword != TRI_NO)
a21a05
-				password = simple_prompt("Password: ", 100, false);
a21a05
-			else
a21a05
-				exit_horribly(modulename, "connection needs password\n");
a21a05
-
a21a05
-			if (password == NULL)
a21a05
-				exit_horribly(modulename, "out of memory\n");
a21a05
-			new_pass = true;
a21a05
-		}
a21a05
-	} while (new_pass);
a21a05
-
a21a05
-	AH->savedPassword = password;
a21a05
-
a21a05
-	termPQExpBuffer(&connstr);
a21a05
-
a21a05
-	/* check for version mismatch */
a21a05
-	_check_database_version(AH);
a21a05
-
a21a05
-	PQsetNoticeProcessor(newConn, notice_processor, NULL);
a21a05
-
a21a05
-	return newConn;
a21a05
+	if (dbname)
a21a05
+		ropt->cparams.override_dbname = pg_strdup(dbname);
a21a05
+	/*
a21a05
+	 * Note: we want to establish the new connection, and in particular update
a21a05
+	 * ArchiveHandle's connCancel, before closing old connection.  Otherwise
a21a05
+	 * an ill-timed SIGINT could try to access a dead connection.
a21a05
+	 */
a21a05
+	AH->connection = NULL;              /* dodge error check in ConnectDatabase */
a21a05
+	
a21a05
+	ConnectDatabase((Archive *) AH, &ropt->cparams, true);
a21a05
+	
a21a05
+	PQfinish(oldConn);
a21a05
 }
a21a05
 
a21a05
 
a21a05
 /*
a21a05
- * Make a database connection with the given parameters.  The
a21a05
- * connection handle is returned, the parameters are stored in AHX.
a21a05
+ * Make, or remake, a database connection with the given parameters.
a21a05
+ *
a21a05
+ * The resulting connection handle is stored in AHX->connection.
a21a05
+ *
a21a05
  * An interactive password prompt is automatically issued if required.
a21a05
+ * We store the results of that in AHX->savedPassword.
a21a05
  *
a21a05
  * Note: it's not really all that sensible to use a single-entry password
a21a05
  * cache if the username keeps changing.  In current usage, however, the
a21a05
@@ -235,26 +114,26 @@
a21a05
  */
a21a05
 void
a21a05
 ConnectDatabase(Archive *AHX,
a21a05
-				const char *dbname,
a21a05
-				const char *pghost,
a21a05
-				const char *pgport,
a21a05
-				const char *username,
a21a05
-				enum trivalue prompt_password)
a21a05
+		const ConnParams *cparams,
a21a05
+		bool isReconnect)
a21a05
 {
a21a05
 	ArchiveHandle *AH = (ArchiveHandle *) AHX;
a21a05
+	enum	trivalue prompt_password;
a21a05
 	char	   *password = AH->savedPassword;
a21a05
 	bool		new_pass;
a21a05
 
a21a05
 	if (AH->connection)
a21a05
 		exit_horribly(modulename, "already connected to a database\n");
a21a05
 
a21a05
+	/* Never prompt for a password during a reconnection */
a21a05
+	prompt_password = isReconnect ? TRI_NO : cparams->promptPassword;
a21a05
+
a21a05
 	if (prompt_password == TRI_YES && password == NULL)
a21a05
 	{
a21a05
 		password = simple_prompt("Password: ", 100, false);
a21a05
 		if (password == NULL)
a21a05
 			exit_horribly(modulename, "out of memory\n");
a21a05
 	}
a21a05
-	AH->promptPassword = prompt_password;
a21a05
 
a21a05
 	/*
a21a05
 	 * Start the connection.  Loop until we have a password if requested by
a21a05
@@ -263,53 +142,70 @@
a21a05
 	do
a21a05
 	{
a21a05
 #define PARAMS_ARRAY_SIZE	7
a21a05
-		const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
a21a05
-		const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
a21a05
-
a21a05
-		keywords[0] = "host";
a21a05
-		values[0] = pghost;
a21a05
-		keywords[1] = "port";
a21a05
-		values[1] = pgport;
a21a05
-		keywords[2] = "user";
a21a05
-		values[2] = username;
a21a05
-		keywords[3] = "password";
a21a05
-		values[3] = password;
a21a05
-		keywords[4] = "dbname";
a21a05
-		values[4] = dbname;
a21a05
-		keywords[5] = "fallback_application_name";
a21a05
-		values[5] = progname;
a21a05
-		keywords[6] = NULL;
a21a05
-		values[6] = NULL;
a21a05
-
a21a05
-		new_pass = false;
a21a05
-		AH->connection = PQconnectdbParams(keywords, values, true);
a21a05
-
a21a05
-		free(keywords);
a21a05
-		free(values);
a21a05
-
a21a05
-		if (!AH->connection)
a21a05
-			exit_horribly(modulename, "failed to connect to database\n");
a21a05
-
a21a05
-		if (PQstatus(AH->connection) == CONNECTION_BAD &&
a21a05
-			PQconnectionNeedsPassword(AH->connection) &&
a21a05
-			password == NULL &&
a21a05
-			prompt_password != TRI_NO)
a21a05
-		{
a21a05
-			PQfinish(AH->connection);
a21a05
-			password = simple_prompt("Password: ", 100, false);
a21a05
-			if (password == NULL)
a21a05
-				exit_horribly(modulename, "out of memory\n");
a21a05
-			new_pass = true;
a21a05
-		}
a21a05
+		const char *keywords[8];
a21a05
+		const char *values[8];
a21a05
+		int			i = 0;
a21a05
+
a21a05
+        /*
a21a05
+         * If dbname is a connstring, its entries can override the other
a21a05
+         * values obtained from cparams; but in turn, override_dbname can
a21a05
+         * override the dbname component of it.
a21a05
+         */
a21a05
+        keywords[i] = "host";
a21a05
+        values[i++] = cparams->pghost;
a21a05
+        keywords[i] = "port";
a21a05
+        values[i++] = cparams->pgport;
a21a05
+        keywords[i] = "user";
a21a05
+        values[i++] = cparams->username;
a21a05
+        keywords[i] = "password";
a21a05
+        values[i++] = password;
a21a05
+        keywords[i] = "dbname";
a21a05
+        values[i++] = cparams->dbname;
a21a05
+        if (cparams->override_dbname)
a21a05
+        {
a21a05
+                keywords[i] = "dbname";
a21a05
+                values[i++] = cparams->override_dbname;
a21a05
+        }
a21a05
+        keywords[i] = "fallback_application_name";
a21a05
+        values[i++] = progname;
a21a05
+        keywords[i] = NULL;
a21a05
+        values[i++] = NULL;
a21a05
+
a21a05
+	new_pass = false;
a21a05
+	AH->connection = PQconnectdbParams(keywords, values, true);
a21a05
+
a21a05
+
a21a05
+	if (!AH->connection)
a21a05
+		exit_horribly(modulename, "failed to connect to database\n");
a21a05
+
a21a05
+	if (PQstatus(AH->connection) == CONNECTION_BAD &&
a21a05
+		PQconnectionNeedsPassword(AH->connection) &&
a21a05
+		password == NULL &&
a21a05
+		prompt_password != TRI_NO)
a21a05
+	{
a21a05
+		PQfinish(AH->connection);
a21a05
+		password = simple_prompt("Password: ", 100, false);
a21a05
+		if (password == NULL)
a21a05
+			exit_horribly(modulename, "out of memory\n");
a21a05
+		new_pass = true;
a21a05
+	}
a21a05
 	} while (new_pass);
a21a05
 
a21a05
 	AH->savedPassword = password;
a21a05
 
a21a05
 	/* check to see that the backend connection was successfully made */
a21a05
 	if (PQstatus(AH->connection) == CONNECTION_BAD)
a21a05
-		exit_horribly(modulename, "connection to database \"%s\" failed: %s",
a21a05
-					  PQdb(AH->connection) ? PQdb(AH->connection) : "",
a21a05
-					  PQerrorMessage(AH->connection));
a21a05
+	{
a21a05
+	        if (isReconnect)
a21a05
+	                exit_horribly(modulename, "reconnection to database \"%s\" failed: %s",
a21a05
+	                                          PQdb(AH->connection) ? PQdb(AH->connection) : "",
a21a05
+	                                          PQerrorMessage(AH->connection));
a21a05
+	        else
a21a05
+	                exit_horribly(modulename, "connection to database \"%s\" failed: %s",
a21a05
+	                                          PQdb(AH->connection) ? PQdb(AH->connection) : "",
a21a05
+	                                          PQerrorMessage(AH->connection));
a21a05
+	}
a21a05
+
a21a05
 
a21a05
 	/* check for version mismatch */
a21a05
 	_check_database_version(AH);
a21a05
diff -ru postgresql-9.2.24/src/bin/pg_dump/pg_backup.h postgresql-9.2.24_new/src/bin/pg_dump/pg_backup.h
a21a05
--- postgresql-9.2.24/src/bin/pg_dump/pg_backup.h	2017-11-06 23:17:39.000000000 +0100
a21a05
+++ postgresql-9.2.24_new/src/bin/pg_dump/pg_backup.h	2021-04-13 08:08:24.728000000 +0200
a21a05
@@ -94,6 +94,20 @@
a21a05
 
a21a05
 typedef int (*DataDumperPtr) (Archive *AH, void *userArg);
a21a05
 
a21a05
+/* Parameters needed by ConnectDatabase; same for dump and restore */
a21a05
+typedef struct _connParams
a21a05
+{
a21a05
+ /* These fields record the actual command line parameters */
a21a05
+ char           *dbname;                     /* this may be a connstring! */
a21a05
+ char           *pgport;
a21a05
+ char           *pghost;
a21a05
+ char           *username;
a21a05
+ enum trivalue   promptPassword;
a21a05
+ /* If not NULL, this overrides the dbname obtained from command line */
a21a05
+ /* (but *only* the DB name, not anything else in the connstring) */
a21a05
+ char           *override_dbname;
a21a05
+} ConnParams;
a21a05
+
a21a05
 typedef struct _restoreOptions
a21a05
 {
a21a05
 	int			createDB;		/* Issue commands to create the database */
a21a05
@@ -130,10 +144,7 @@
a21a05
 	char	   *triggerNames;
a21a05
 
a21a05
 	int			useDB;
a21a05
-	char	   *dbname;			/* subject to expand_dbname */
a21a05
-	char	   *pgport;
a21a05
-	char	   *pghost;
a21a05
-	char	   *username;
a21a05
+	ConnParams	cparams;
a21a05
 	int			noDataForFailedTables;
a21a05
 	enum trivalue promptPassword;
a21a05
 	int			exit_on_error;
a21a05
@@ -152,11 +163,8 @@
a21a05
  */
a21a05
 
a21a05
 extern void ConnectDatabase(Archive *AH,
a21a05
-				const char *dbname,
a21a05
-				const char *pghost,
a21a05
-				const char *pgport,
a21a05
-				const char *username,
a21a05
-				enum trivalue prompt_password);
a21a05
+			 const ConnParams *cparams,
a21a05
+		     bool isReconnect);
a21a05
 extern void DisconnectDatabase(Archive *AHX);
a21a05
 extern PGconn *GetConnection(Archive *AHX);
a21a05
 
a21a05
diff -ru postgresql-9.2.24/src/bin/pg_dump/pg_dump.c postgresql-9.2.24_new/src/bin/pg_dump/pg_dump.c
a21a05
--- postgresql-9.2.24/src/bin/pg_dump/pg_dump.c	2017-11-06 23:17:39.000000000 +0100
a21a05
+++ postgresql-9.2.24_new/src/bin/pg_dump/pg_dump.c	2021-04-13 08:08:54.530000000 +0200
a21a05
@@ -276,10 +276,7 @@
a21a05
 	int			c;
a21a05
 	const char *filename = NULL;
a21a05
 	const char *format = "p";
a21a05
-	const char *dbname = NULL;
a21a05
-	const char *pghost = NULL;
a21a05
-	const char *pgport = NULL;
a21a05
-	const char *username = NULL;
a21a05
+	ConnParams	cparams;
a21a05
 	const char *dumpencoding = NULL;
a21a05
 	bool		oids = false;
a21a05
 	TableInfo  *tblinfo;
a21a05
@@ -288,7 +285,6 @@
a21a05
 	int			numObjs;
a21a05
 	DumpableObject *boundaryObjs;
a21a05
 	int			i;
a21a05
-	enum trivalue prompt_password = TRI_DEFAULT;
a21a05
 	int			compressLevel = -1;
a21a05
 	int			plainText = 0;
a21a05
 	int			outputClean = 0;
a21a05
@@ -307,7 +303,12 @@
a21a05
 	static int	disable_triggers = 0;
a21a05
 	static int	outputNoTablespaces = 0;
a21a05
 	static int	use_setsessauth = 0;
a21a05
-
a21a05
+	cparams.pghost = NULL;
a21a05
+	cparams.pgport = NULL;
a21a05
+	cparams.username = NULL;
a21a05
+	cparams.dbname = NULL;
a21a05
+	cparams.override_dbname = NULL;
a21a05
+	
a21a05
 	static struct option long_options[] = {
a21a05
 		{"data-only", no_argument, NULL, 'a'},
a21a05
 		{"blobs", no_argument, NULL, 'b'},
a21a05
@@ -427,7 +428,7 @@
a21a05
 				break;
a21a05
 
a21a05
 			case 'h':			/* server host */
a21a05
-				pghost = optarg;
a21a05
+				cparams.pghost = optarg;
a21a05
 				break;
a21a05
 
a21a05
 			case 'i':
a21a05
@@ -452,7 +453,7 @@
a21a05
 				break;
a21a05
 
a21a05
 			case 'p':			/* server port */
a21a05
-				pgport = optarg;
a21a05
+				cparams.pgport = optarg;
a21a05
 				break;
a21a05
 
a21a05
 			case 'R':
a21a05
@@ -477,7 +478,7 @@
a21a05
 				break;
a21a05
 
a21a05
 			case 'U':
a21a05
-				username = optarg;
a21a05
+				cparams.username = optarg;
a21a05
 				break;
a21a05
 
a21a05
 			case 'v':			/* verbose */
a21a05
@@ -485,11 +486,11 @@
a21a05
 				break;
a21a05
 
a21a05
 			case 'w':
a21a05
-				prompt_password = TRI_NO;
a21a05
+				cparams.promptPassword = TRI_NO;
a21a05
 				break;
a21a05
 
a21a05
 			case 'W':
a21a05
-				prompt_password = TRI_YES;
a21a05
+				cparams.promptPassword = TRI_YES;
a21a05
 				break;
a21a05
 
a21a05
 			case 'x':			/* skip ACL dump */
a21a05
@@ -532,8 +533,8 @@
a21a05
 	}
a21a05
 
a21a05
 	/* Get database name from command line */
a21a05
-	if (optind < argc)
a21a05
-		dbname = argv[optind++];
a21a05
+	if (optind < argc && cparams.dbname == NULL)
a21a05
+		cparams.dbname = argv[optind++];
a21a05
 
a21a05
 	/* Complain if any arguments remain */
a21a05
 	if (optind < argc)
a21a05
@@ -605,7 +606,7 @@
a21a05
 	 * Open the database using the Archiver, so it knows about it. Errors mean
a21a05
 	 * death.
a21a05
 	 */
a21a05
-	ConnectDatabase(fout, dbname, pghost, pgport, username, prompt_password);
a21a05
+	ConnectDatabase(fout, &cparams, false);
a21a05
 	setup_connection(fout, dumpencoding, use_role);
a21a05
 
a21a05
 	/*
a21a05
@@ -791,9 +792,15 @@
a21a05
 	for (i = 0; i < numObjs; i++)
a21a05
 		dumpDumpableObject(fout, dobjs[i]);
a21a05
 
a21a05
+	ropt=malloc(sizeof(RestoreOptions));
a21a05
 	/*
a21a05
 	 * Set up options info to ensure we dump what we want.
a21a05
 	 */
a21a05
+	ropt->cparams.dbname = cparams.dbname ? pg_strdup(cparams.dbname) : NULL;
a21a05
+	ropt->cparams.pgport = cparams.pgport ? pg_strdup(cparams.pgport) : NULL;
a21a05
+	ropt->cparams.pghost = cparams.pghost ? pg_strdup(cparams.pghost) : NULL;
a21a05
+	ropt->cparams.username = cparams.username ? pg_strdup(cparams.username) : NULL;
a21a05
+	ropt->cparams.promptPassword = cparams.promptPassword;
a21a05
 	ropt = NewRestoreOptions();
a21a05
 	ropt->filename = filename;
a21a05
 	ropt->dropSchema = outputClean;
a21a05
@@ -837,6 +844,7 @@
a21a05
 		RestoreArchive(fout);
a21a05
 
a21a05
 	CloseArchive(fout);
a21a05
+	free(ropt);
a21a05
 
a21a05
 	exit_nicely(0);
a21a05
 }
a21a05
diff -ru postgresql-9.2.24/src/bin/pg_dump/pg_restore.c postgresql-9.2.24_new/src/bin/pg_dump/pg_restore.c
a21a05
--- postgresql-9.2.24/src/bin/pg_dump/pg_restore.c	2017-11-06 23:17:39.000000000 +0100
a21a05
+++ postgresql-9.2.24_new/src/bin/pg_dump/pg_restore.c	2021-04-13 08:08:24.736000000 +0200
a21a05
@@ -162,7 +162,7 @@
a21a05
 				opts->createDB = 1;
a21a05
 				break;
a21a05
 			case 'd':
a21a05
-				opts->dbname = pg_strdup(optarg);
a21a05
+				opts->cparams.dbname = pg_strdup(optarg);
a21a05
 				break;
a21a05
 			case 'e':
a21a05
 				opts->exit_on_error = true;
a21a05
@@ -176,7 +176,7 @@
a21a05
 				break;
a21a05
 			case 'h':
a21a05
 				if (strlen(optarg) != 0)
a21a05
-					opts->pghost = pg_strdup(optarg);
a21a05
+					opts->cparams.pghost = pg_strdup(optarg);
a21a05
 				break;
a21a05
 			case 'i':
a21a05
 				/* ignored, deprecated option */
a21a05
@@ -204,7 +204,7 @@
a21a05
 
a21a05
 			case 'p':
a21a05
 				if (strlen(optarg) != 0)
a21a05
-					opts->pgport = pg_strdup(optarg);
a21a05
+					opts->cparams.pgport = pg_strdup(optarg);
a21a05
 				break;
a21a05
 			case 'R':
a21a05
 				/* no-op, still accepted for backwards compatibility */
a21a05
@@ -238,7 +238,7 @@
a21a05
 				break;
a21a05
 
a21a05
 			case 'U':
a21a05
-				opts->username = optarg;
a21a05
+				opts->cparams.username = optarg;
a21a05
 				break;
a21a05
 
a21a05
 			case 'v':			/* verbose */
a21a05
@@ -246,11 +246,11 @@
a21a05
 				break;
a21a05
 
a21a05
 			case 'w':
a21a05
-				opts->promptPassword = TRI_NO;
a21a05
+				opts->cparams.promptPassword = TRI_NO;
a21a05
 				break;
a21a05
 
a21a05
 			case 'W':
a21a05
-				opts->promptPassword = TRI_YES;
a21a05
+				opts->cparams.promptPassword = TRI_YES;
a21a05
 				break;
a21a05
 
a21a05
 			case 'x':			/* skip ACL dump */
a21a05
@@ -300,7 +300,7 @@
a21a05
 	}
a21a05
 
a21a05
 	/* Should get at most one of -d and -f, else user is confused */
a21a05
-	if (opts->dbname)
a21a05
+	if (opts->cparams.dbname)
a21a05
 	{
a21a05
 		if (opts->filename)
a21a05
 		{
a21a05
diff -ru postgresql-9.2.24/src/bin/scripts/clusterdb.c postgresql-9.2.24_new/src/bin/scripts/clusterdb.c
a21a05
--- postgresql-9.2.24/src/bin/scripts/clusterdb.c	2017-11-06 23:17:39.000000000 +0100
a21a05
+++ postgresql-9.2.24_new/src/bin/scripts/clusterdb.c	2021-04-13 08:08:24.736000000 +0200
a21a05
@@ -14,14 +14,10 @@
a21a05
 #include "dumputils.h"
a21a05
 
a21a05
 
a21a05
-static void cluster_one_database(const char *dbname, bool verbose, const char *table,
a21a05
-					 const char *host, const char *port,
a21a05
-					 const char *username, enum trivalue prompt_password,
a21a05
-					 const char *progname, bool echo);
a21a05
-static void cluster_all_databases(bool verbose, const char *maintenance_db,
a21a05
-					  const char *host, const char *port,
a21a05
-					  const char *username, enum trivalue prompt_password,
a21a05
-					  const char *progname, bool echo, bool quiet);
a21a05
+static void cluster_one_database(const ConnParams *cparams, const char *table,
a21a05
+                                                          const char *progname, bool verbose, bool echo);
a21a05
+static void cluster_all_databases(ConnParams *cparams, const char *progname,
a21a05
+                                                           bool verbose, bool echo, bool quiet);
a21a05
 
a21a05
 static void help(const char *progname);
a21a05
 
a21a05
@@ -55,6 +51,7 @@
a21a05
 	char	   *port = NULL;
a21a05
 	char	   *username = NULL;
a21a05
 	enum trivalue prompt_password = TRI_DEFAULT;
a21a05
+	ConnParams	cparams;
a21a05
 	bool		echo = false;
a21a05
 	bool		quiet = false;
a21a05
 	bool		alldb = false;
a21a05
@@ -130,6 +127,13 @@
a21a05
 		exit(1);
a21a05
 	}
a21a05
 
a21a05
+	/* fill cparams except for dbname, which is set below */
a21a05
+	cparams.pghost = host;
a21a05
+	cparams.pgport = port;
a21a05
+	cparams.pguser = username;
a21a05
+	cparams.prompt_password = prompt_password;
a21a05
+	cparams.override_dbname = NULL;
a21a05
+
a21a05
 	setup_cancel_handler();
a21a05
 
a21a05
 	if (alldb)
a21a05
@@ -147,8 +151,9 @@
a21a05
 			exit(1);
a21a05
 		}
a21a05
 
a21a05
-		cluster_all_databases(verbose, maintenance_db, host, port, username, prompt_password,
a21a05
-							  progname, echo, quiet);
a21a05
+		cparams.dbname = maintenance_db;
a21a05
+
a21a05
+		cluster_all_databases(&cparams, progname, verbose, echo, quiet);
a21a05
 	}
a21a05
 	else
a21a05
 	{
a21a05
@@ -162,9 +167,9 @@
a21a05
 				dbname = get_user_name(progname);
a21a05
 		}
a21a05
 
a21a05
-		cluster_one_database(dbname, verbose, table,
a21a05
-							 host, port, username, prompt_password,
a21a05
-							 progname, echo);
a21a05
+		cparams.dbname = dbname;
a21a05
+
a21a05
+		cluster_one_database(&cparams, table, progname, verbose, echo);
a21a05
 	}
a21a05
 
a21a05
 	exit(0);
a21a05
@@ -172,10 +177,8 @@
a21a05
 
a21a05
 
a21a05
 static void
a21a05
-cluster_one_database(const char *dbname, bool verbose, const char *table,
a21a05
-					 const char *host, const char *port,
a21a05
-					 const char *username, enum trivalue prompt_password,
a21a05
-					 const char *progname, bool echo)
a21a05
+cluster_one_database(const ConnParams *cparams, const char *table,
a21a05
+					 const char *progname, bool verbose, bool echo)
a21a05
 {
a21a05
 	PQExpBufferData sql;
a21a05
 
a21a05
@@ -190,8 +193,7 @@
a21a05
 		appendPQExpBuffer(&sql, " %s", table);
a21a05
 	appendPQExpBuffer(&sql, ";\n");
a21a05
 
a21a05
-	conn = connectDatabase(dbname, host, port, username, prompt_password,
a21a05
-						   progname, false);
a21a05
+	conn = connectDatabase(cparams, progname, echo, false, false);
a21a05
 	if (!executeMaintenanceCommand(conn, sql.data, echo))
a21a05
 	{
a21a05
 		if (table)
a21a05
@@ -209,22 +211,17 @@
a21a05
 
a21a05
 
a21a05
 static void
a21a05
-cluster_all_databases(bool verbose, const char *maintenance_db,
a21a05
-					  const char *host, const char *port,
a21a05
-					  const char *username, enum trivalue prompt_password,
a21a05
-					  const char *progname, bool echo, bool quiet)
a21a05
+cluster_all_databases(ConnParams *cparams, const char *progname,
a21a05
+					  bool verbose, bool echo, bool quiet)
a21a05
 {
a21a05
 	PGconn	   *conn;
a21a05
 	PGresult   *result;
a21a05
-	PQExpBufferData connstr;
a21a05
 	int			i;
a21a05
 
a21a05
-	conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
a21a05
-									  prompt_password, progname);
a21a05
+	conn = connectMaintenanceDatabase(cparams, progname, echo);
a21a05
 	result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname, echo);
a21a05
 	PQfinish(conn);
a21a05
 
a21a05
-	initPQExpBuffer(&connstr);
a21a05
 	for (i = 0; i < PQntuples(result); i++)
a21a05
 	{
a21a05
 		char	   *dbname = PQgetvalue(result, i, 0);
a21a05
@@ -235,15 +232,10 @@
a21a05
 			fflush(stdout);
a21a05
 		}
a21a05
 
a21a05
-		resetPQExpBuffer(&connstr);
a21a05
-		appendPQExpBuffer(&connstr, "dbname=");
a21a05
-		appendConnStrVal(&connstr, dbname);
a21a05
-
a21a05
-		cluster_one_database(connstr.data, verbose, NULL,
a21a05
-							 host, port, username, prompt_password,
a21a05
-							 progname, echo);
a21a05
+		cparams->override_dbname = dbname;
a21a05
+
a21a05
+		cluster_one_database(cparams, NULL, progname, verbose, echo);
a21a05
 	}
a21a05
-	termPQExpBuffer(&connstr);
a21a05
 
a21a05
 	PQclear(result);
a21a05
 }
a21a05
diff -ru postgresql-9.2.24/src/bin/scripts/common.c postgresql-9.2.24_new/src/bin/scripts/common.c
a21a05
--- postgresql-9.2.24/src/bin/scripts/common.c	2017-11-06 23:17:39.000000000 +0100
a21a05
+++ postgresql-9.2.24_new/src/bin/scripts/common.c	2021-04-13 08:08:54.530000000 +0200
a21a05
@@ -91,15 +91,14 @@
a21a05
  * interactive password prompt is automatically issued if required.
a21a05
  */
a21a05
 PGconn *
a21a05
-connectDatabase(const char *dbname, const char *pghost, const char *pgport,
a21a05
-				const char *pguser, enum trivalue prompt_password,
a21a05
-				const char *progname, bool fail_ok)
a21a05
+connectDatabase(const ConnParams *cparams, const char *progname,
a21a05
+				bool echo, bool fail_ok, bool allow_password_reuse)
a21a05
 {
a21a05
 	PGconn	   *conn;
a21a05
 	char	   *password = NULL;
a21a05
 	bool		new_pass;
a21a05
 
a21a05
-	if (prompt_password == TRI_YES)
a21a05
+	if (cparams->prompt_password == TRI_YES)
a21a05
 		password = simple_prompt("Password: ", 100, false);
a21a05
 
a21a05
 	/*
a21a05
@@ -109,47 +108,50 @@
a21a05
 	do
a21a05
 	{
a21a05
 #define PARAMS_ARRAY_SIZE	7
a21a05
-		const char **keywords = malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
a21a05
-		const char **values = malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
a21a05
-
a21a05
-		if (!keywords || !values)
a21a05
+		const char *keywords[8];
a21a05
+		const char *values[8];
a21a05
+		int                 i = 0;
a21a05
+
a21a05
+		/*
a21a05
+		 * If dbname is a connstring, its entries can override the other
a21a05
+		 * values obtained from cparams; but in turn, override_dbname can
a21a05
+		 * override the dbname component of it.
a21a05
+		 */
a21a05
+		keywords[i] = "host";
a21a05
+		values[i++] = cparams->pghost;
a21a05
+		keywords[i] = "port";
a21a05
+		values[i++] = cparams->pgport;
a21a05
+		keywords[i] = "user";
a21a05
+		values[i++] = cparams->pguser;
a21a05
+		keywords[i] = "password";
a21a05
+		values[i++] = password;
a21a05
+		keywords[i] = "dbname";
a21a05
+		values[i++] = cparams->dbname;
a21a05
+		if (cparams->override_dbname)
a21a05
 		{
a21a05
-			fprintf(stderr, _("%s: out of memory\n"), progname);
a21a05
-			exit(1);
a21a05
+			keywords[i] = "dbname";
a21a05
+			values[i++] = cparams->override_dbname;
a21a05
 		}
a21a05
-
a21a05
-		keywords[0] = "host";
a21a05
-		values[0] = pghost;
a21a05
-		keywords[1] = "port";
a21a05
-		values[1] = pgport;
a21a05
-		keywords[2] = "user";
a21a05
-		values[2] = pguser;
a21a05
-		keywords[3] = "password";
a21a05
-		values[3] = password;
a21a05
-		keywords[4] = "dbname";
a21a05
-		values[4] = dbname;
a21a05
-		keywords[5] = "fallback_application_name";
a21a05
-		values[5] = progname;
a21a05
-		keywords[6] = NULL;
a21a05
-		values[6] = NULL;
a21a05
+		keywords[i] = "fallback_application_name";
a21a05
+		values[i++] = progname;
a21a05
+		keywords[i] = NULL;
a21a05
+		values[i++] = NULL;
a21a05
 
a21a05
 		new_pass = false;
a21a05
 		conn = PQconnectdbParams(keywords, values, true);
a21a05
 
a21a05
-		free(keywords);
a21a05
-		free(values);
a21a05
 
a21a05
 		if (!conn)
a21a05
 		{
a21a05
 			fprintf(stderr, _("%s: could not connect to database %s\n"),
a21a05
-					progname, dbname);
a21a05
+					progname, cparams->dbname);
a21a05
 			exit(1);
a21a05
 		}
a21a05
 
a21a05
 		if (PQstatus(conn) == CONNECTION_BAD &&
a21a05
-			PQconnectionNeedsPassword(conn) &&
a21a05
-			password == NULL &&
a21a05
-			prompt_password != TRI_NO)
a21a05
+				PQconnectionNeedsPassword(conn) &&
a21a05
+				password == NULL &&
a21a05
+				cparams->prompt_password != TRI_NO)
a21a05
 		{
a21a05
 			PQfinish(conn);
a21a05
 			password = simple_prompt("Password: ", 100, false);
a21a05
@@ -169,7 +171,7 @@
a21a05
 			return NULL;
a21a05
 		}
a21a05
 		fprintf(stderr, _("%s: could not connect to database %s: %s"),
a21a05
-				progname, dbname, PQerrorMessage(conn));
a21a05
+				progname, cparams->dbname, PQerrorMessage(conn));
a21a05
 		exit(1);
a21a05
 	}
a21a05
 
a21a05
@@ -178,26 +180,29 @@
a21a05
 
a21a05
 /*
a21a05
  * Try to connect to the appropriate maintenance database.
a21a05
+ *
a21a05
+ * This differs from connectDatabase only in that it has a rule for
a21a05
+ * inserting a default "dbname" if none was given (which is why cparams
a21a05
+ * is not const).  Note that cparams->dbname should typically come from
a21a05
+ * a --maintenance-db command line parameter.
a21a05
  */
a21a05
 PGconn *
a21a05
-connectMaintenanceDatabase(const char *maintenance_db, const char *pghost,
a21a05
-						   const char *pgport, const char *pguser,
a21a05
-						   enum trivalue prompt_password,
a21a05
-						   const char *progname)
a21a05
+connectMaintenanceDatabase(ConnParams *cparams, const char *progname, bool echo)
a21a05
 {
a21a05
 	PGconn	   *conn;
a21a05
 
a21a05
 	/* If a maintenance database name was specified, just connect to it. */
a21a05
-	if (maintenance_db)
a21a05
-		return connectDatabase(maintenance_db, pghost, pgport, pguser,
a21a05
-							   prompt_password, progname, false);
a21a05
+	if (cparams->dbname)
a21a05
+		return connectDatabase(cparams, progname, echo, false, false);
a21a05
 
a21a05
 	/* Otherwise, try postgres first and then template1. */
a21a05
-	conn = connectDatabase("postgres", pghost, pgport, pguser, prompt_password,
a21a05
-						   progname, true);
a21a05
+	cparams->dbname = "postgres";
a21a05
+	conn = connectDatabase(cparams, progname, echo, true, false);
a21a05
 	if (!conn)
a21a05
-		conn = connectDatabase("template1", pghost, pgport, pguser,
a21a05
-							   prompt_password, progname, false);
a21a05
+	{
a21a05
+		cparams->dbname = "template1";
a21a05
+		conn = connectDatabase(cparams, progname, echo, false, false);
a21a05
+	}
a21a05
 
a21a05
 	return conn;
a21a05
 }
a21a05
diff -ru postgresql-9.2.24/src/bin/scripts/common.h postgresql-9.2.24_new/src/bin/scripts/common.h
a21a05
--- postgresql-9.2.24/src/bin/scripts/common.h	2017-11-06 23:17:39.000000000 +0100
a21a05
+++ postgresql-9.2.24_new/src/bin/scripts/common.h	2021-04-13 08:08:24.737000000 +0200
a21a05
@@ -20,6 +20,20 @@
a21a05
 	TRI_YES
a21a05
 };
a21a05
 
a21a05
+/* Parameters needed by connectDatabase/connectMaintenanceDatabase */
a21a05
+typedef struct _connParams
a21a05
+{
a21a05
+ /* These fields record the actual command line parameters */
a21a05
+ const char *dbname;                 /* this may be a connstring! */
a21a05
+ const char *pghost;
a21a05
+ const char *pgport;
a21a05
+ const char *pguser;
a21a05
+ enum trivalue prompt_password;
a21a05
+ /* If not NULL, this overrides the dbname obtained from command line */
a21a05
+ /* (but *only* the DB name, not anything else in the connstring) */
a21a05
+ const char *override_dbname;
a21a05
+} ConnParams;
a21a05
+
a21a05
 typedef void (*help_handler) (const char *progname);
a21a05
 
a21a05
 extern const char *get_user_name(const char *progname);
a21a05
@@ -28,14 +42,13 @@
a21a05
 						 const char *fixed_progname,
a21a05
 						 help_handler hlp);
a21a05
 
a21a05
-extern PGconn *connectDatabase(const char *dbname, const char *pghost,
a21a05
-				const char *pgport, const char *pguser,
a21a05
-				enum trivalue prompt_password, const char *progname,
a21a05
-				bool fail_ok);
a21a05
-
a21a05
-extern PGconn *connectMaintenanceDatabase(const char *maintenance_db,
a21a05
-				  const char *pghost, const char *pgport, const char *pguser,
a21a05
-						enum trivalue prompt_password, const char *progname);
a21a05
+extern PGconn *connectDatabase(const ConnParams *cparams,
a21a05
+							   const char *progname,
a21a05
+							   bool echo, bool fail_ok,
a21a05
+							   bool allow_password_reuse);
a21a05
+
a21a05
+extern PGconn *connectMaintenanceDatabase(ConnParams *cparams,
a21a05
+										  const char *progname, bool echo);
a21a05
 
a21a05
 extern PGresult *executeQuery(PGconn *conn, const char *query,
a21a05
 			 const char *progname, bool echo);
a21a05
diff -ru postgresql-9.2.24/src/bin/scripts/createdb.c postgresql-9.2.24_new/src/bin/scripts/createdb.c
a21a05
--- postgresql-9.2.24/src/bin/scripts/createdb.c	2017-11-06 23:17:39.000000000 +0100
a21a05
+++ postgresql-9.2.24_new/src/bin/scripts/createdb.c	2021-04-13 08:08:24.737000000 +0200
a21a05
@@ -50,6 +50,7 @@
a21a05
 	char	   *port = NULL;
a21a05
 	char	   *username = NULL;
a21a05
 	enum trivalue prompt_password = TRI_DEFAULT;
a21a05
+	ConnParams	cparams;
a21a05
 	bool		echo = false;
a21a05
 	char	   *owner = NULL;
a21a05
 	char	   *tablespace = NULL;
a21a05
@@ -201,8 +202,14 @@
a21a05
 	if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
a21a05
 		maintenance_db = "template1";
a21a05
 
a21a05
-	conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
a21a05
-									  prompt_password, progname);
a21a05
+	cparams.dbname = maintenance_db;
a21a05
+	cparams.pghost = host;
a21a05
+	cparams.pgport = port;
a21a05
+	cparams.pguser = username;
a21a05
+	cparams.prompt_password = prompt_password;
a21a05
+	cparams.override_dbname = NULL;
a21a05
+
a21a05
+	conn = connectMaintenanceDatabase(&cparams, progname, echo);
a21a05
 
a21a05
 	if (echo)
a21a05
 		printf("%s", sql.data);
a21a05
diff -ru postgresql-9.2.24/src/bin/scripts/createlang.c postgresql-9.2.24_new/src/bin/scripts/createlang.c
a21a05
--- postgresql-9.2.24/src/bin/scripts/createlang.c	2017-11-06 23:17:39.000000000 +0100
a21a05
+++ postgresql-9.2.24_new/src/bin/scripts/createlang.c	2021-04-13 08:08:24.737000000 +0200
a21a05
@@ -42,6 +42,7 @@
a21a05
 	char	   *port = NULL;
a21a05
 	char	   *username = NULL;
a21a05
 	enum trivalue prompt_password = TRI_DEFAULT;
a21a05
+	ConnParams	cparams;
a21a05
 	bool		echo = false;
a21a05
 	char	   *langname = NULL;
a21a05
 
a21a05
@@ -130,6 +131,13 @@
a21a05
 			dbname = get_user_name(progname);
a21a05
 	}
a21a05
 
a21a05
+	cparams.dbname = dbname;
a21a05
+	cparams.pghost = host;
a21a05
+	cparams.pgport = port;
a21a05
+	cparams.pguser = username;
a21a05
+	cparams.prompt_password = prompt_password;
a21a05
+	cparams.override_dbname = NULL;
a21a05
+
a21a05
 	initPQExpBuffer(&sql;;
a21a05
 
a21a05
 	/*
a21a05
@@ -140,8 +148,8 @@
a21a05
 		printQueryOpt popt;
a21a05
 		static const bool translate_columns[] = {false, true};
a21a05
 
a21a05
-		conn = connectDatabase(dbname, host, port, username, prompt_password,
a21a05
-							   progname, false);
a21a05
+		conn = connectDatabase(&cparams,
a21a05
+							   progname, echo, false, false);
a21a05
 
a21a05
 		printfPQExpBuffer(&sql, "SELECT lanname as \"%s\", "
a21a05
 				"(CASE WHEN lanpltrusted THEN '%s' ELSE '%s' END) as \"%s\" "
a21a05
@@ -178,8 +186,8 @@
a21a05
 		if (*p >= 'A' && *p <= 'Z')
a21a05
 			*p += ('a' - 'A');
a21a05
 
a21a05
-	conn = connectDatabase(dbname, host, port, username, prompt_password,
a21a05
-						   progname, false);
a21a05
+	conn = connectDatabase(&cparams,
a21a05
+						   progname, echo, false, false);
a21a05
 
a21a05
 	/*
a21a05
 	 * Make sure the language isn't already installed
a21a05
diff -ru postgresql-9.2.24/src/bin/scripts/createuser.c postgresql-9.2.24_new/src/bin/scripts/createuser.c
a21a05
--- postgresql-9.2.24/src/bin/scripts/createuser.c	2017-11-06 23:17:39.000000000 +0100
a21a05
+++ postgresql-9.2.24_new/src/bin/scripts/createuser.c	2021-04-13 08:08:24.738000000 +0200
a21a05
@@ -58,6 +58,7 @@
a21a05
 	char	   *port = NULL;
a21a05
 	char	   *username = NULL;
a21a05
 	enum trivalue prompt_password = TRI_DEFAULT;
a21a05
+	ConnParams	cparams;
a21a05
 	bool		echo = false;
a21a05
 	bool		interactive = false;
a21a05
 	char	   *conn_limit = NULL;
a21a05
@@ -245,8 +246,14 @@
a21a05
 	if (login == 0)
a21a05
 		login = TRI_YES;
a21a05
 
a21a05
-	conn = connectDatabase("postgres", host, port, username, prompt_password,
a21a05
-						   progname, false);
a21a05
+	cparams.dbname = NULL;              /* this program lacks any dbname option... */
a21a05
+	cparams.pghost = host;
a21a05
+	cparams.pgport = port;
a21a05
+	cparams.pguser = username;
a21a05
+	cparams.prompt_password = prompt_password;
a21a05
+	cparams.override_dbname = NULL;
a21a05
+
a21a05
+	conn = connectMaintenanceDatabase(&cparams, progname, echo);
a21a05
 
a21a05
 	initPQExpBuffer(&sql;;
a21a05
 
a21a05
diff -ru postgresql-9.2.24/src/bin/scripts/dropdb.c postgresql-9.2.24_new/src/bin/scripts/dropdb.c
a21a05
--- postgresql-9.2.24/src/bin/scripts/dropdb.c	2017-11-06 23:17:39.000000000 +0100
a21a05
+++ postgresql-9.2.24_new/src/bin/scripts/dropdb.c	2021-04-13 08:08:24.738000000 +0200
a21a05
@@ -46,6 +46,7 @@
a21a05
 	char	   *port = NULL;
a21a05
 	char	   *username = NULL;
a21a05
 	enum trivalue prompt_password = TRI_DEFAULT;
a21a05
+	ConnParams	cparams;
a21a05
 	bool		echo = false;
a21a05
 	bool		interactive = false;
a21a05
 
a21a05
@@ -128,8 +129,14 @@
a21a05
 	if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
a21a05
 		maintenance_db = "template1";
a21a05
 
a21a05
-	conn = connectMaintenanceDatabase(maintenance_db,
a21a05
-							host, port, username, prompt_password, progname);
a21a05
+	cparams.dbname = maintenance_db;
a21a05
+	cparams.pghost = host;
a21a05
+	cparams.pgport = port;
a21a05
+	cparams.pguser = username;
a21a05
+	cparams.prompt_password = prompt_password;
a21a05
+	cparams.override_dbname = NULL;
a21a05
+
a21a05
+	conn = connectMaintenanceDatabase(&cparams, progname, echo);
a21a05
 
a21a05
 	if (echo)
a21a05
 		printf("%s", sql.data);
a21a05
diff -ru postgresql-9.2.24/src/bin/scripts/droplang.c postgresql-9.2.24_new/src/bin/scripts/droplang.c
a21a05
--- postgresql-9.2.24/src/bin/scripts/droplang.c	2017-11-06 23:17:39.000000000 +0100
a21a05
+++ postgresql-9.2.24_new/src/bin/scripts/droplang.c	2021-04-13 08:08:24.738000000 +0200
a21a05
@@ -44,6 +44,7 @@
a21a05
 	char	   *port = NULL;
a21a05
 	char	   *username = NULL;
a21a05
 	enum trivalue prompt_password = TRI_DEFAULT;
a21a05
+	ConnParams	cparams;
a21a05
 	bool		echo = false;
a21a05
 	char	   *langname = NULL;
a21a05
 	char	   *p;
a21a05
@@ -129,6 +130,13 @@
a21a05
 			dbname = get_user_name(progname);
a21a05
 	}
a21a05
 
a21a05
+	cparams.dbname = dbname;
a21a05
+	cparams.pghost = host;
a21a05
+	cparams.pgport = port;
a21a05
+	cparams.pguser = username;
a21a05
+	cparams.prompt_password = prompt_password;
a21a05
+	cparams.override_dbname = NULL;
a21a05
+
a21a05
 	initPQExpBuffer(&sql;;
a21a05
 
a21a05
 	/*
a21a05
@@ -139,8 +147,8 @@
a21a05
 		printQueryOpt popt;
a21a05
 		static const bool translate_columns[] = {false, true};
a21a05
 
a21a05
-		conn = connectDatabase(dbname, host, port, username, prompt_password,
a21a05
-							   progname, false);
a21a05
+		conn = connectDatabase(&cparams,
a21a05
+							   progname, echo, false, false);
a21a05
 
a21a05
 		printfPQExpBuffer(&sql, "SELECT lanname as \"%s\", "
a21a05
 				"(CASE WHEN lanpltrusted THEN '%s' ELSE '%s' END) as \"%s\" "
a21a05
@@ -179,8 +187,8 @@
a21a05
 		if (*p >= 'A' && *p <= 'Z')
a21a05
 			*p += ('a' - 'A');
a21a05
 
a21a05
-	conn = connectDatabase(dbname, host, port, username, prompt_password,
a21a05
-						   progname, false);
a21a05
+	conn = connectDatabase(&cparams,
a21a05
+						   progname, echo, false, false);
a21a05
 
a21a05
 	/*
a21a05
 	 * Force schema search path to be just pg_catalog, so that we don't have
a21a05
diff -ru postgresql-9.2.24/src/bin/scripts/dropuser.c postgresql-9.2.24_new/src/bin/scripts/dropuser.c
a21a05
--- postgresql-9.2.24/src/bin/scripts/dropuser.c	2017-11-06 23:17:39.000000000 +0100
a21a05
+++ postgresql-9.2.24_new/src/bin/scripts/dropuser.c	2021-04-13 08:08:24.738000000 +0200
a21a05
@@ -44,6 +44,7 @@
a21a05
 	char	   *port = NULL;
a21a05
 	char	   *username = NULL;
a21a05
 	enum trivalue prompt_password = TRI_DEFAULT;
a21a05
+	ConnParams	cparams;
a21a05
 	bool		echo = false;
a21a05
 	bool		interactive = false;
a21a05
 
a21a05
@@ -124,13 +125,19 @@
a21a05
 			exit(0);
a21a05
 	}
a21a05
 
a21a05
+	cparams.dbname = NULL;              /* this program lacks any dbname option... */
a21a05
+	cparams.pghost = host;
a21a05
+	cparams.pgport = port;
a21a05
+	cparams.pguser = username;
a21a05
+	cparams.prompt_password = prompt_password;
a21a05
+	cparams.override_dbname = NULL;
a21a05
+
a21a05
+	conn = connectMaintenanceDatabase(&cparams, progname, echo);
a21a05
+
a21a05
 	initPQExpBuffer(&sql;;
a21a05
 	appendPQExpBuffer(&sql, "DROP ROLE %s%s;\n",
a21a05
 					  (if_exists ? "IF EXISTS " : ""), fmtId(dropuser));
a21a05
 
a21a05
-	conn = connectDatabase("postgres", host, port, username, prompt_password,
a21a05
-						   progname, false);
a21a05
-
a21a05
 	if (echo)
a21a05
 		printf("%s", sql.data);
a21a05
 	result = PQexec(conn, sql.data);
a21a05
diff -ru postgresql-9.2.24/src/bin/scripts/reindexdb.c postgresql-9.2.24_new/src/bin/scripts/reindexdb.c
a21a05
--- postgresql-9.2.24/src/bin/scripts/reindexdb.c	2017-11-06 23:17:39.000000000 +0100
a21a05
+++ postgresql-9.2.24_new/src/bin/scripts/reindexdb.c	2021-04-13 08:08:24.739000000 +0200
a21a05
@@ -14,20 +14,15 @@
a21a05
 #include "dumputils.h"
a21a05
 
a21a05
 
a21a05
-static void reindex_one_database(const char *name, const char *dbname,
a21a05
-					 const char *type, const char *host,
a21a05
-					 const char *port, const char *username,
a21a05
-					 enum trivalue prompt_password, const char *progname,
a21a05
-					 bool echo);
a21a05
-static void reindex_all_databases(const char *maintenance_db,
a21a05
-					  const char *host, const char *port,
a21a05
-					  const char *username, enum trivalue prompt_password,
a21a05
-					  const char *progname, bool echo,
a21a05
-					  bool quiet);
a21a05
-static void reindex_system_catalogs(const char *dbname,
a21a05
-						const char *host, const char *port,
a21a05
-						const char *username, enum trivalue prompt_password,
a21a05
-						const char *progname, bool echo);
a21a05
+static void reindex_one_database(const ConnParams *cparams,
a21a05
+		const char *type, const char *name,
a21a05
+		const char *progname,
a21a05
+		bool echo);
a21a05
+static void reindex_all_databases(ConnParams *cparams,
a21a05
+		const char *progname, bool echo,
a21a05
+		bool quiet);
a21a05
+static void reindex_system_catalogs(const ConnParams *cparams,
a21a05
+		const char *progname, bool echo);
a21a05
 static void help(const char *progname);
a21a05
 
a21a05
 int
a21a05
@@ -60,6 +55,7 @@
a21a05
 	const char *port = NULL;
a21a05
 	const char *username = NULL;
a21a05
 	enum trivalue prompt_password = TRI_DEFAULT;
a21a05
+	ConnParams	cparams;
a21a05
 	bool		syscatalog = false;
a21a05
 	bool		alldb = false;
a21a05
 	bool		echo = false;
a21a05
@@ -140,6 +136,13 @@
a21a05
 		exit(1);
a21a05
 	}
a21a05
 
a21a05
+	/* fill cparams except for dbname, which is set below */
a21a05
+	cparams.pghost = host;
a21a05
+	cparams.pgport = port;
a21a05
+	cparams.pguser = username;
a21a05
+	cparams.prompt_password = prompt_password;
a21a05
+	cparams.override_dbname = NULL;
a21a05
+
a21a05
 	setup_cancel_handler();
a21a05
 
a21a05
 	if (alldb)
a21a05
@@ -165,8 +168,9 @@
a21a05
 			exit(1);
a21a05
 		}
a21a05
 
a21a05
-		reindex_all_databases(maintenance_db, host, port, username,
a21a05
-							  prompt_password, progname, echo, quiet);
a21a05
+		cparams.dbname = maintenance_db;
a21a05
+
a21a05
+		reindex_all_databases(&cparams, progname, echo, quiet);
a21a05
 	}
a21a05
 	else if (syscatalog)
a21a05
 	{
a21a05
@@ -191,8 +195,9 @@
a21a05
 				dbname = get_user_name(progname);
a21a05
 		}
a21a05
 
a21a05
-		reindex_system_catalogs(dbname, host, port, username, prompt_password,
a21a05
-								progname, echo);
a21a05
+		cparams.dbname = dbname;
a21a05
+
a21a05
+		reindex_system_catalogs(&cparams, progname, echo);
a21a05
 	}
a21a05
 	else
a21a05
 	{
a21a05
@@ -206,32 +211,34 @@
a21a05
 				dbname = get_user_name(progname);
a21a05
 		}
a21a05
 
a21a05
+		cparams.dbname = dbname;
a21a05
+
a21a05
 		if (index)
a21a05
-			reindex_one_database(index, dbname, "INDEX", host, port,
a21a05
-								 username, prompt_password, progname, echo);
a21a05
+			reindex_one_database(&cparams, "INDEX", table,
a21a05
+					progname, echo);
a21a05
 		if (table)
a21a05
-			reindex_one_database(table, dbname, "TABLE", host, port,
a21a05
-								 username, prompt_password, progname, echo);
a21a05
+			reindex_one_database(&cparams, "TABLE", table,
a21a05
+					progname, echo);
a21a05
 		/* reindex database only if index or table is not specified */
a21a05
 		if (index == NULL && table == NULL)
a21a05
-			reindex_one_database(NULL, dbname, "DATABASE", host, port,
a21a05
-								 username, prompt_password, progname, echo);
a21a05
+			reindex_one_database(&cparams, "DATABASE", table,
a21a05
+					progname, echo);
a21a05
 	}
a21a05
 
a21a05
 	exit(0);
a21a05
 }
a21a05
 
a21a05
 static void
a21a05
-reindex_one_database(const char *name, const char *dbname, const char *type,
a21a05
-					 const char *host, const char *port, const char *username,
a21a05
-			  enum trivalue prompt_password, const char *progname, bool echo)
a21a05
+reindex_one_database(const ConnParams *cparams,
a21a05
+		const char *type, const char *name,
a21a05
+		const char *progname,
a21a05
+		bool echo)
a21a05
 {
a21a05
 	PQExpBufferData sql;
a21a05
 
a21a05
 	PGconn	   *conn;
a21a05
 
a21a05
-	conn = connectDatabase(dbname, host, port, username, prompt_password,
a21a05
-						   progname, false);
a21a05
+	conn = connectDatabase(cparams, progname, echo, false, false);
a21a05
 
a21a05
 	initPQExpBuffer(&sql;;
a21a05
 
a21a05
@@ -264,22 +271,17 @@
a21a05
 }
a21a05
 
a21a05
 static void
a21a05
-reindex_all_databases(const char *maintenance_db,
a21a05
-					  const char *host, const char *port,
a21a05
-					  const char *username, enum trivalue prompt_password,
a21a05
-					  const char *progname, bool echo, bool quiet)
a21a05
+reindex_all_databases(ConnParams *cparams, const char *progname, bool echo, bool quiet)
a21a05
 {
a21a05
 	PGconn	   *conn;
a21a05
 	PGresult   *result;
a21a05
 	PQExpBufferData connstr;
a21a05
 	int			i;
a21a05
 
a21a05
-	conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
a21a05
-									  prompt_password, progname);
a21a05
+	conn = connectMaintenanceDatabase(cparams, progname, echo);
a21a05
 	result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname, echo);
a21a05
 	PQfinish(conn);
a21a05
 
a21a05
-	initPQExpBuffer(&connstr);
a21a05
 	for (i = 0; i < PQntuples(result); i++)
a21a05
 	{
a21a05
 		char	   *dbname = PQgetvalue(result, i, 0);
a21a05
@@ -293,26 +295,22 @@
a21a05
 		resetPQExpBuffer(&connstr);
a21a05
 		appendPQExpBuffer(&connstr, "dbname=");
a21a05
 		appendConnStrVal(&connstr, dbname);
a21a05
+		cparams->override_dbname = dbname;
a21a05
 
a21a05
-		reindex_one_database(NULL, connstr.data, "DATABASE", host,
a21a05
-							 port, username, prompt_password,
a21a05
-							 progname, echo);
a21a05
+		reindex_one_database(cparams, "DATABASE", NULL, progname, echo);
a21a05
 	}
a21a05
-	termPQExpBuffer(&connstr);
a21a05
 
a21a05
 	PQclear(result);
a21a05
 }
a21a05
 
a21a05
 static void
a21a05
-reindex_system_catalogs(const char *dbname, const char *host, const char *port,
a21a05
-						const char *username, enum trivalue prompt_password,
a21a05
-						const char *progname, bool echo)
a21a05
+reindex_system_catalogs(const ConnParams *cparams,
a21a05
+		const char *progname, bool echo)
a21a05
 {
a21a05
 	PGconn	   *conn;
a21a05
 	PQExpBufferData sql;
a21a05
 
a21a05
-	conn = connectDatabase(dbname, host, port, username, prompt_password,
a21a05
-						   progname, false);
a21a05
+	conn = connectDatabase(cparams, progname, echo, false, false);
a21a05
 
a21a05
 	initPQExpBuffer(&sql;;
a21a05
 
a21a05
diff -ru postgresql-9.2.24/src/bin/scripts/vacuumdb.c postgresql-9.2.24_new/src/bin/scripts/vacuumdb.c
a21a05
--- postgresql-9.2.24/src/bin/scripts/vacuumdb.c	2017-11-06 23:17:39.000000000 +0100
a21a05
+++ postgresql-9.2.24_new/src/bin/scripts/vacuumdb.c	2021-04-13 08:08:24.739000000 +0200
a21a05
@@ -15,16 +15,13 @@
a21a05
 #include "dumputils.h"
a21a05
 
a21a05
 
a21a05
-static void vacuum_one_database(const char *dbname, bool full, bool verbose,
a21a05
+static void vacuum_one_database(const ConnParams *cparams, bool full, bool verbose,
a21a05
 					bool and_analyze, bool analyze_only, bool freeze,
a21a05
-					const char *table, const char *host, const char *port,
a21a05
-					const char *username, enum trivalue prompt_password,
a21a05
-					const char *progname, bool echo);
a21a05
-static void vacuum_all_databases(bool full, bool verbose, bool and_analyze,
a21a05
+					const char *table, const char *progname, bool echo);
a21a05
+
a21a05
+static void vacuum_all_databases(ConnParams *cparams, bool full, bool verbose,
a21a05
+					 bool and_analyze,
a21a05
 					 bool analyze_only, bool freeze,
a21a05
-					 const char *maintenance_db,
a21a05
-					 const char *host, const char *port,
a21a05
-					 const char *username, enum trivalue prompt_password,
a21a05
 					 const char *progname, bool echo, bool quiet);
a21a05
 
a21a05
 static void help(const char *progname);
a21a05
@@ -63,6 +60,7 @@
a21a05
 	char	   *port = NULL;
a21a05
 	char	   *username = NULL;
a21a05
 	enum trivalue prompt_password = TRI_DEFAULT;
a21a05
+	ConnParams	cparams;
a21a05
 	bool		echo = false;
a21a05
 	bool		quiet = false;
a21a05
 	bool		and_analyze = false;
a21a05
@@ -172,6 +170,13 @@
a21a05
 		/* allow 'and_analyze' with 'analyze_only' */
a21a05
 	}
a21a05
 
a21a05
+	/* fill cparams except for dbname, which is set below */
a21a05
+	cparams.pghost = host;
a21a05
+	cparams.pgport = port;
a21a05
+	cparams.pguser = username;
a21a05
+	cparams.prompt_password = prompt_password;
a21a05
+	cparams.override_dbname = NULL;
a21a05
+
a21a05
 	setup_cancel_handler();
a21a05
 
a21a05
 	if (alldb)
a21a05
@@ -189,9 +194,10 @@
a21a05
 			exit(1);
a21a05
 		}
a21a05
 
a21a05
-		vacuum_all_databases(full, verbose, and_analyze, analyze_only, freeze,
a21a05
-							 maintenance_db, host, port, username,
a21a05
-							 prompt_password, progname, echo, quiet);
a21a05
+		cparams.dbname = maintenance_db;
a21a05
+
a21a05
+		vacuum_all_databases(&cparams, full, verbose, and_analyze, analyze_only, freeze,
a21a05
+							 progname, echo, quiet);
a21a05
 	}
a21a05
 	else
a21a05
 	{
a21a05
@@ -205,9 +211,10 @@
a21a05
 				dbname = get_user_name(progname);
a21a05
 		}
a21a05
 
a21a05
-		vacuum_one_database(dbname, full, verbose, and_analyze, analyze_only,
a21a05
+		cparams.dbname = dbname;
a21a05
+
a21a05
+		vacuum_one_database(&cparams, full, verbose, and_analyze, analyze_only,
a21a05
 							freeze, table,
a21a05
-							host, port, username, prompt_password,
a21a05
 							progname, echo);
a21a05
 	}
a21a05
 
a21a05
@@ -216,10 +223,8 @@
a21a05
 
a21a05
 
a21a05
 static void
a21a05
-vacuum_one_database(const char *dbname, bool full, bool verbose, bool and_analyze,
a21a05
+vacuum_one_database(const ConnParams *cparams, bool full, bool verbose, bool and_analyze,
a21a05
 					bool analyze_only, bool freeze, const char *table,
a21a05
-					const char *host, const char *port,
a21a05
-					const char *username, enum trivalue prompt_password,
a21a05
 					const char *progname, bool echo)
a21a05
 {
a21a05
 	PQExpBufferData sql;
a21a05
@@ -228,8 +233,7 @@
a21a05
 
a21a05
 	initPQExpBuffer(&sql;;
a21a05
 
a21a05
-	conn = connectDatabase(dbname, host, port, username, prompt_password,
a21a05
-						   progname, false);
a21a05
+	conn = connectDatabase(cparams, progname, echo, false, true);
a21a05
 
a21a05
 	if (analyze_only)
a21a05
 	{
a21a05
@@ -302,19 +306,15 @@
a21a05
 
a21a05
 
a21a05
 static void
a21a05
-vacuum_all_databases(bool full, bool verbose, bool and_analyze, bool analyze_only,
a21a05
-					 bool freeze, const char *maintenance_db,
a21a05
-					 const char *host, const char *port,
a21a05
-					 const char *username, enum trivalue prompt_password,
a21a05
-					 const char *progname, bool echo, bool quiet)
a21a05
+vacuum_all_databases(ConnParams *cparams, bool full, bool verbose, bool and_analyze, bool analyze_only,
a21a05
+					 bool freeze, const char *progname, bool echo, bool quiet)
a21a05
 {
a21a05
 	PGconn	   *conn;
a21a05
 	PGresult   *result;
a21a05
 	PQExpBufferData connstr;
a21a05
 	int			i;
a21a05
 
a21a05
-	conn = connectMaintenanceDatabase(maintenance_db, host, port,
a21a05
-									  username, prompt_password, progname);
a21a05
+	conn = connectMaintenanceDatabase(cparams, progname, echo);
a21a05
 	result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname, echo);
a21a05
 	PQfinish(conn);
a21a05
 
a21a05
@@ -329,13 +329,10 @@
a21a05
 			fflush(stdout);
a21a05
 		}
a21a05
 
a21a05
-		resetPQExpBuffer(&connstr);
a21a05
-		appendPQExpBuffer(&connstr, "dbname=");
a21a05
-		appendConnStrVal(&connstr, PQgetvalue(result, i, 0));
a21a05
-
a21a05
-		vacuum_one_database(connstr.data, full, verbose, and_analyze,
a21a05
-							analyze_only,
a21a05
-						 freeze, NULL, host, port, username, prompt_password,
a21a05
+		cparams->override_dbname = PQgetvalue(result, i, 0);
a21a05
+
a21a05
+		vacuum_one_database(cparams, full, verbose, and_analyze,
a21a05
+							analyze_only, freeze, NULL,
a21a05
 							progname, echo);
a21a05
 	}
a21a05
 	termPQExpBuffer(&connstr);