diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 6263f8a77987a65f32b35d7551e8ae0dc8691c00..394746a5ce0544d927986950e210d5c9f2e64493 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -3117,8 +3117,14 @@ do_connect(enum trivalue reuse_previous_specification,
 			pg_log_error("\\connect: %s", PQerrorMessage(n_conn));
 			if (o_conn)
 			{
+				/*
+				 * Transition to having no connection.  Keep this bit in sync
+				 * with CheckConnection().
+				 */
 				PQfinish(o_conn);
 				pset.db = NULL;
+				ResetCancelConn();
+				UnsyncVariables();
 			}
 		}
 
@@ -3132,7 +3138,8 @@ do_connect(enum trivalue reuse_previous_specification,
 
 	/*
 	 * Replace the old connection with the new one, and update
-	 * connection-dependent variables.
+	 * connection-dependent variables.  Keep the resynchronization logic in
+	 * sync with CheckConnection().
 	 */
 	PQsetNoticeProcessor(n_conn, NoticeProcessor, NULL);
 	pset.db = n_conn;
@@ -3223,7 +3230,8 @@ connection_warnings(bool in_startup)
 										 sverbuf, sizeof(sverbuf)));
 
 #ifdef WIN32
-		checkWin32Codepage();
+		if (in_startup)
+			checkWin32Codepage();
 #endif
 		printSSLInfo();
 		printGSSInfo();
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index 44a782478d1970c9a5136b06642d1a1e9e306363..4b2679360fc2ebbaae4d82efeae09c9962fd7ae7 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -402,13 +402,27 @@ CheckConnection(void)
 		if (!OK)
 		{
 			fprintf(stderr, _("Failed.\n"));
+
+			/*
+			 * Transition to having no connection.  Keep this bit in sync with
+			 * do_connect().
+			 */
 			PQfinish(pset.db);
 			pset.db = NULL;
 			ResetCancelConn();
 			UnsyncVariables();
 		}
 		else
+		{
 			fprintf(stderr, _("Succeeded.\n"));
+
+			/*
+			 * Re-sync, just in case anything changed.  Keep this in sync with
+			 * do_connect().
+			 */
+			SyncVariables();
+			connection_warnings(false); /* Must be after SyncVariables */
+		}
 	}
 
 	return OK;