diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index b1523a6a893422af5df9bd25abbe6da9d554aa42..8d9400be0fc7c30f27a7ac949912d79ef7da4be3 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -1031,7 +1031,8 @@ connectFailureMessage(PGconn *conn, int errorno)
 			strcpy(host_addr, "???");
 
 		display_host_addr = (conn->pghostaddr == NULL) &&
-			(strcmp(conn->pghost, host_addr) != 0);
+							(conn->pghost != NULL) &&
+							(strcmp(conn->pghost, host_addr) != 0);
 
 		appendPQExpBuffer(&conn->errorMessage,
 						  libpq_gettext("could not connect to server: %s\n"