diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index b518fb1..0c60746 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -302,13 +302,32 @@ exec_command(const char *cmd, else if (strcmp(cmd, "conninfo") == 0) { char *db = PQdb(pset.db); - char *host = PQhost(pset.db); if (db == NULL) printf(_("You are currently not connected to a database.\n")); else { - if (host == NULL) + char *host; + PQconninfoOption *connOptions; + PQconninfoOption *option; + + /* Report "hostaddr" if libpq used it, otherwise PQhost(). */ + host = PQhost(pset.db); + connOptions = PQconninfo(pset.db); + if (connOptions == NULL) + { + fprintf(stderr, _("out of memory\n")); + exit(EXIT_FAILURE); + } + for (option = connOptions; option && option->keyword; option++) + if (strcmp(option->keyword, "hostaddr") == 0) + { + if (option->val != NULL && option->val[0] != '\0') + host = option->val; + break; + } + + if (host == NULL) /* can't happen */ host = DEFAULT_PGSOCKET_DIR; /* If the host is an absolute path, the connection is via socket */ if (is_absolute_path(host)) @@ -318,6 +337,8 @@ exec_command(const char *cmd, printf(_("You are connected to database \"%s\" as user \"%s\" on host \"%s\" at port \"%s\".\n"), db, PQuser(pset.db), host, PQport(pset.db)); printSSLInfo(); + + PQconninfoFree(connOptions); } }