Skip to content

Commit f2dc962

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 131825c commit f2dc962

File tree

4 files changed

+6
-5
lines changed

4 files changed

+6
-5
lines changed

contrib/oid2name/oid2name.c

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1954,7 +1954,7 @@ connectDatabase(const char *dbname, const char *connection_string,
19541954
{
19551955
fprintf(stderr,
19561956
_("%s: could not connect to database \"%s\": %s"),
1957-
progname, dbname, PQerrorMessage(conn));
1957+
progname, PQdb(conn) ? PQdb(conn) : "", PQerrorMessage(conn));
19581958
exit_nicely(1);
19591959
}
19601960
else

src/bin/pgbench/pgbench.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ doConnect(void)
686686
if (PQstatus(conn) == CONNECTION_BAD)
687687
{
688688
fprintf(stderr, "connection to database \"%s\" failed:\n%s",
689-
dbName, PQerrorMessage(conn));
689+
PQdb(conn), PQerrorMessage(conn));
690690
PQfinish(conn);
691691
return NULL;
692692
}
@@ -3312,7 +3312,8 @@ main(int argc, char **argv)
33123312

33133313
if (PQstatus(con) == CONNECTION_BAD)
33143314
{
3315-
fprintf(stderr, "connection to database \"%s\" failed\n", dbName);
3315+
fprintf(stderr, "connection to database \"%s\" failed\n",
3316+
PQdb(con) ? PQdb(con) : "");
33163317
fprintf(stderr, "%s", PQerrorMessage(con));
33173318
exit(1);
33183319
}

0 commit comments

Comments
 (0)