diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml
index cffabbc8f7ec109e5de97e1879e4674b65c7aaff..9c0e4c8f9c6caffd29afce39b9105d5941dde642 100644
--- a/doc/src/sgml/libpq.sgml
+++ b/doc/src/sgml/libpq.sgml
@@ -1463,6 +1463,10 @@ char *PQpass(const PGconn *conn);
      <listitem>
       <para>
        Returns the server host name of the connection.
+       This can be a host name, an IP address, or a directory path if the
+       connection is via Unix socket.  (The path case can be distinguished
+       because it will always be an absolute path, beginning
+       with <literal>/</>.)
 <synopsis>
 char *PQhost(const PGconn *conn);
 </synopsis>
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 3a82e37401c549159eaf6e24744d7c108ca598fb..438a4ec8ea243e27887ba63cc51bb5773e74eb4c 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -322,8 +322,6 @@ exec_command(const char *cmd,
 			PQconninfoOption *option;
 
 			host = PQhost(pset.db);
-			if (host == NULL)
-				host = DEFAULT_PGSOCKET_DIR;
 			/* A usable "hostaddr" overrides the basic sense of host. */
 			connOptions = PQconninfo(pset.db);
 			if (connOptions == NULL)
@@ -1750,16 +1748,14 @@ do_connect(char *dbname, char *user, char *host, char *port)
 	/*
 	 * Any change in the parameters read above makes us discard the password.
 	 * We also discard it if we're to use a conninfo rather than the
-	 * positional syntax.  Note that currently, PQhost() can return NULL for a
-	 * default Unix-socket connection, so we have to allow NULL for host.
+	 * positional syntax.
 	 */
 	if (has_connection_string)
 		keep_password = false;
 	else
 		keep_password =
 			(user && PQuser(o_conn) && strcmp(user, PQuser(o_conn)) == 0) &&
-			((host && PQhost(o_conn) && strcmp(host, PQhost(o_conn)) == 0) ||
-			 (host == NULL && PQhost(o_conn) == NULL)) &&
+			(host && PQhost(o_conn) && strcmp(host, PQhost(o_conn)) == 0) &&
 			(port && PQport(o_conn) && strcmp(port, PQport(o_conn)) == 0);
 
 	/*
@@ -1890,8 +1886,6 @@ do_connect(char *dbname, char *user, char *host, char *port)
 		{
 			char	   *host = PQhost(pset.db);
 
-			if (host == NULL)
-				host = DEFAULT_PGSOCKET_DIR;
 			/* If the host is an absolute path, the connection is via socket */
 			if (is_absolute_path(host))
 				printf(_("You are now connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n"),
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 9c51166b40a5b83ec8e6479191fd48850390e830..4a1a0272b6b833ebcb6d5c9e6e15f8ee5d322850 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -5353,7 +5353,10 @@ PQhost(const PGconn *conn)
 	else
 	{
 #ifdef HAVE_UNIX_SOCKETS
-		return conn->pgunixsocket;
+		if (conn->pgunixsocket != NULL && conn->pgunixsocket[0] != '\0')
+			return conn->pgunixsocket;
+		else
+			return DEFAULT_PGSOCKET_DIR;
 #else
 		return DefaultHost;
 #endif
diff --git a/src/interfaces/libpq/libpq-int.h b/src/interfaces/libpq/libpq-int.h
index 4b69c1216dbe6baaf3d2905d7ce65648cdf0a2ef..98ea6b19477ed7b0be43dbf4f328216399106b02 100644
--- a/src/interfaces/libpq/libpq-int.h
+++ b/src/interfaces/libpq/libpq-int.h
@@ -302,10 +302,9 @@ struct pg_conn
 	char	   *pghostaddr;		/* the numeric IP address of the machine on
 								 * which the server is running.  Takes
 								 * precedence over above. */
-	char	   *pgport;			/* the server's communication port */
-	char	   *pgunixsocket;	/* the Unix-domain socket that the server is
-								 * listening on; if NULL, uses a default
-								 * constructed from pgport */
+	char	   *pgport;			/* the server's communication port number */
+	char	   *pgunixsocket;	/* the directory of the server's Unix-domain
+								 * socket; if NULL, use DEFAULT_PGSOCKET_DIR */
 	char	   *pgtty;			/* tty on which the backend messages is
 								 * displayed (OBSOLETE, NOT USED) */
 	char	   *connect_timeout;	/* connection timeout (numeric string) */