diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index abb18a19c2..8108a76a77 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -2990,7 +2990,8 @@ do_connect(enum trivalue reuse_previous_specification,
 	{
 		if (!user)
 			user = PQuser(o_conn);
-		if (host && strcmp(host, PQhost(o_conn)) == 0 &&
+		if (host && PQhost(o_conn) &&
+			strcmp(host, PQhost(o_conn)) == 0 &&
 			has_hostaddr(o_conn))
 		{
 			hostaddr = PQhostaddr(o_conn);
