diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
index 64603b6a324f8aa97043f0cbd88211d54849bbe5..429d037129d910efe3c8473474c09dcf78dcae13 100644
--- a/src/bin/pg_dump/pg_backup_archiver.c
+++ b/src/bin/pg_dump/pg_backup_archiver.c
@@ -15,7 +15,7 @@
  *
  *
  * IDENTIFICATION
- *		$PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.96 2004/08/30 19:44:14 tgl Exp $
+ *		$PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.97 2004/09/10 20:05:18 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -56,7 +56,7 @@ static void fixPriorBlobRefs(ArchiveHandle *AH, TocEntry *blobte,
 static void _doSetFixedOutputState(ArchiveHandle *AH);
 static void _doSetSessionAuth(ArchiveHandle *AH, const char *user);
 static void _doSetWithOids(ArchiveHandle *AH, const bool withOids);
-static void _reconnectToDB(ArchiveHandle *AH, const char *dbname, const char *user);
+static void _reconnectToDB(ArchiveHandle *AH, const char *dbname);
 static void _becomeUser(ArchiveHandle *AH, const char *user);
 static void _becomeOwner(ArchiveHandle *AH, TocEntry *te);
 static void _selectOutputSchema(ArchiveHandle *AH, const char *schemaName);
@@ -277,8 +277,8 @@ RestoreArchive(Archive *AHX, RestoreOptions *ropt)
 			/* If we created a DB, connect to it... */
 			if (strcmp(te->desc, "DATABASE") == 0)
 			{
-				ahlog(AH, 1, "connecting to new database \"%s\" as user \"%s\"\n", te->tag, te->owner);
-				_reconnectToDB(AH, te->tag, te->owner);
+				ahlog(AH, 1, "connecting to new database \"%s\"\n", te->tag);
+				_reconnectToDB(AH, te->tag);
 			}
 		}
 
@@ -2151,26 +2151,25 @@ _doSetWithOids(ArchiveHandle *AH, const bool withOids)
 
 
 /*
- * Issue the commands to connect to the specified database
- * as the specified user.
+ * Issue the commands to connect to the specified database.
  *
  * If we're currently restoring right into a database, this will
  * actually establish a connection. Otherwise it puts a \connect into
  * the script output.
+ *
+ * NULL dbname implies reconnecting to the current DB (pretty useless).
  */
 static void
-_reconnectToDB(ArchiveHandle *AH, const char *dbname, const char *user)
+_reconnectToDB(ArchiveHandle *AH, const char *dbname)
 {
 	if (RestoringToDB(AH))
-		ReconnectToServer(AH, dbname, user);
+		ReconnectToServer(AH, dbname, NULL);
 	else
 	{
 		PQExpBuffer qry = createPQExpBuffer();
 
-		appendPQExpBuffer(qry, "\\connect %s",
+		appendPQExpBuffer(qry, "\\connect %s\n\n",
 						  dbname ? fmtId(dbname) : "-");
-		appendPQExpBuffer(qry, " %s\n\n",
-						  fmtId(user));
 
 		ahprintf(AH, qry->data);
 
@@ -2179,12 +2178,12 @@ _reconnectToDB(ArchiveHandle *AH, const char *dbname, const char *user)
 
 	/*
 	 * NOTE: currUser keeps track of what the imaginary session user in
-	 * our script is
+	 * our script is.  It's now effectively reset to the original userID.
 	 */
 	if (AH->currUser)
 		free(AH->currUser);
 
-	AH->currUser = strdup(user);
+	AH->currUser = strdup("");
 
 	/* don't assume we still know the output schema */
 	if (AH->currSchema)
@@ -2448,6 +2447,7 @@ _printTocEntry(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt, bool isDat
 		strlen(te->owner) > 0 && strlen(te->dropStmt) > 0 &&
 		(strcmp(te->desc, "AGGREGATE") == 0 ||
 		 strcmp(te->desc, "CONVERSION") == 0 ||
+		 strcmp(te->desc, "DATABASE") == 0 ||
 		 strcmp(te->desc, "DOMAIN") == 0 ||
 		 strcmp(te->desc, "FUNCTION") == 0 ||
 		 strcmp(te->desc, "OPERATOR") == 0 ||
diff --git a/src/bin/pg_dump/pg_backup_db.c b/src/bin/pg_dump/pg_backup_db.c
index 1489bfbc16721f65c8bcadb3c814b4768833c6d1..360e41b1f998f8f60deccf625e2bed13c4c71837 100644
--- a/src/bin/pg_dump/pg_backup_db.c
+++ b/src/bin/pg_dump/pg_backup_db.c
@@ -5,7 +5,7 @@
  *	Implements the basic DB functions used by the archiver.
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.57 2004/08/29 05:06:53 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.58 2004/09/10 20:05:18 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -88,8 +88,8 @@ _check_database_version(ArchiveHandle *AH, bool ignoreVersion)
  * Reconnect to the server.  If dbname is not NULL, use that database,
  * else the one associated with the archive handle.  If username is
  * not NULL, use that user name, else the one from the handle.	If
- * both the database and the user and match the existing connection
- * already, nothing will be done.
+ * both the database and the user match the existing connection already,
+ * nothing will be done.
  *
  * Returns 1 in any case.
  */
@@ -111,8 +111,8 @@ ReconnectToServer(ArchiveHandle *AH, const char *dbname, const char *username)
 		newusername = username;
 
 	/* Let's see if the request is already satisfied */
-	if (strcmp(newusername, PQuser(AH->connection)) == 0
-		&& strcmp(newdbname, PQdb(AH->connection)) == 0)
+	if (strcmp(newdbname, PQdb(AH->connection)) == 0 &&
+		strcmp(newusername, PQuser(AH->connection)) == 0)
 		return 1;
 
 	newConn = _connectDB(AH, newdbname, newusername);