Skip to content

Commit bcae842

Browse files
committed
Report the true database name on connection errors
When reporting connection errors, we might show a database name in the message that's not the one we actually tried to connect to, if the database was taken from libpq defaults instead of from user parameters. Fix such error messages to use PQdb(), which reports the correct name. (But, per commit 2930c05, make sure not to try to print NULL.) Apply to branches 9.5 through 13. Branch master has already been changed differently by commit 58cd8dc. Reported-by: Robert Haas <robertmhaas@gmail.com> Discussion: https://postgr.es/m/CA+TgmobssJ6rS22dspWnu-oDxXevGmhMD8VcRBjmj-b9UDqRjw@mail.gmail.com
1 parent 2c2e134 commit bcae842

File tree

4 files changed

+6
-5
lines changed

4 files changed

+6
-5
lines changed

contrib/oid2name/oid2name.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ sql_conn(struct options * my_opts)
319319
if (PQstatus(conn) == CONNECTION_BAD)
320320
{
321321
fprintf(stderr, "%s: could not connect to database %s: %s",
322-
"oid2name", my_opts->dbname, PQerrorMessage(conn));
322+
"oid2name", PQdb(conn) ? PQdb(conn) : "", PQerrorMessage(conn));
323323
PQfinish(conn);
324324
exit(1);
325325
}

contrib/vacuumlo/vacuumlo.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ vacuumlo(const char *database, const struct _param * param)
124124
if (PQstatus(conn) == CONNECTION_BAD)
125125
{
126126
fprintf(stderr, "Connection to database \"%s\" failed:\n%s",
127-
database, PQerrorMessage(conn));
127+
PQdb(conn) ? PQdb(conn) : "", PQerrorMessage(conn));
128128
PQfinish(conn);
129129
return -1;
130130
}

src/bin/pg_dump/pg_dumpall.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2088,7 +2088,7 @@ connectDatabase(const char *dbname, const char *connection_string,
20882088
{
20892089
fprintf(stderr,
20902090
_("%s: could not connect to database \"%s\": %s"),
2091-
progname, dbname, PQerrorMessage(conn));
2091+
progname, PQdb(conn) ? PQdb(conn) : "", PQerrorMessage(conn));
20922092
exit_nicely(1);
20932093
}
20942094
else

src/bin/pgbench/pgbench.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ doConnect(void)
828828
if (PQstatus(conn) == CONNECTION_BAD)
829829
{
830830
fprintf(stderr, "connection to database \"%s\" failed:\n%s",
831-
dbName, PQerrorMessage(conn));
831+
PQdb(conn), PQerrorMessage(conn));
832832
PQfinish(conn);
833833
return NULL;
834834
}
@@ -3913,7 +3913,8 @@ main(int argc, char **argv)
39133913

39143914
if (PQstatus(con) == CONNECTION_BAD)
39153915
{
3916-
fprintf(stderr, "connection to database \"%s\" failed\n", dbName);
3916+
fprintf(stderr, "connection to database \"%s\" failed\n",
3917+
PQdb(con) ? PQdb(con) : "");
39173918
fprintf(stderr, "%s", PQerrorMessage(con));
39183919
exit(1);
39193920
}

0 commit comments

Comments
 (0)