Skip to content

Commit f17e8f3

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 58cd8dca3de0. Reported-by: Robert Haas <robertmhaas@gmail.com> Discussion: https://postgr.es/m/CA+TgmobssJ6rS22dspWnu-oDxXevGmhMD8VcRBjmj-b9UDqRjw@mail.gmail.com
1 parent 64bdb6e commit f17e8f3

File tree

4 files changed

+6
-6
lines changed

4 files changed

+6
-6
lines changed

contrib/oid2name/oid2name.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,8 @@ sql_conn(struct options *my_opts)
348348
/* check to see that the backend connection was successfully made */
349349
if (PQstatus(conn) == CONNECTION_BAD)
350350
{
351-
pg_log_error("could not connect to database %s: %s",
352-
my_opts->dbname, PQerrorMessage(conn));
351+
pg_log_error("could not connect to database \"%s\": %s",
352+
PQdb(conn) ? PQdb(conn) : "", PQerrorMessage(conn));
353353
PQfinish(conn);
354354
exit(1);
355355
}

contrib/vacuumlo/vacuumlo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ vacuumlo(const char *database, const struct _param *param)
129129
if (PQstatus(conn) == CONNECTION_BAD)
130130
{
131131
pg_log_error("connection to database \"%s\" failed: %s",
132-
database, PQerrorMessage(conn));
132+
PQdb(conn) ? PQdb(conn) : "", PQerrorMessage(conn));
133133
PQfinish(conn);
134134
return -1;
135135
}

src/bin/pg_dump/pg_dumpall.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1773,7 +1773,7 @@ connectDatabase(const char *dbname, const char *connection_string,
17731773
if (fail_on_error)
17741774
{
17751775
pg_log_error("could not connect to database \"%s\": %s",
1776-
dbname, PQerrorMessage(conn));
1776+
PQdb(conn) ? PQdb(conn) : "", PQerrorMessage(conn));
17771777
exit_nicely(1);
17781778
}
17791779
else

src/bin/pgbench/pgbench.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,7 @@ doConnect(void)
12281228
if (PQstatus(conn) == CONNECTION_BAD)
12291229
{
12301230
pg_log_error("connection to database \"%s\" failed: %s",
1231-
dbName, PQerrorMessage(conn));
1231+
PQdb(conn), PQerrorMessage(conn));
12321232
PQfinish(conn);
12331233
return NULL;
12341234
}
@@ -6047,7 +6047,7 @@ main(int argc, char **argv)
60476047
if (PQstatus(con) == CONNECTION_BAD)
60486048
{
60496049
pg_log_fatal("connection to database \"%s\" failed: %s",
6050-
dbName, PQerrorMessage(con));
6050+
PQdb(con) ? PQdb(con) : "", PQerrorMessage(con));
60516051
exit(1);
60526052
}
60536053

0 commit comments

Comments
 (0)