Report the true database name on connection errors
authorAlvaro Herrera <[email protected]>
Tue, 26 Jan 2021 19:42:13 +0000 (16:42 -0300)
committerAlvaro Herrera <[email protected]>
Tue, 26 Jan 2021 19:42:13 +0000 (16:42 -0300)
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 2930c05634bc, 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 <[email protected]>
Discussion: https://postgr.es/m/CA+TgmobssJ6rS22dspWnu-oDxXevGmhMD8VcRBjmj-b9UDqRjw@mail.gmail.com

contrib/oid2name/oid2name.c
contrib/vacuumlo/vacuumlo.c
src/bin/pg_dump/pg_dumpall.c
src/bin/pgbench/pgbench.c

index 769e527384c880cad339d457bebf07339f6955d5..cb2dae52d6611fe795f540b8f5f3d651d819f288 100644 (file)
@@ -320,7 +320,7 @@ sql_conn(struct options *my_opts)
    if (PQstatus(conn) == CONNECTION_BAD)
    {
        fprintf(stderr, "%s: could not connect to database %s: %s",
-               "oid2name", my_opts->dbname, PQerrorMessage(conn));
+               "oid2name", PQdb(conn) ? PQdb(conn) : "", PQerrorMessage(conn));
        PQfinish(conn);
        exit(1);
    }
index da98dab1927825a8c25624df633b9d248b179dbc..9ee46985f87a427e11d34b8fad03fdff67b60f3c 100644 (file)
@@ -129,7 +129,7 @@ vacuumlo(const char *database, const struct _param *param)
    if (PQstatus(conn) == CONNECTION_BAD)
    {
        fprintf(stderr, "Connection to database \"%s\" failed:\n%s",
-               database, PQerrorMessage(conn));
+               PQdb(conn) ? PQdb(conn) : "", PQerrorMessage(conn));
        PQfinish(conn);
        return -1;
    }
index e817cf04d30a7b052aa4b32d56c2aa5a6875ac17..ad0393133459811a23207143567511319d3fa398 100644 (file)
@@ -2075,7 +2075,7 @@ connectDatabase(const char *dbname, const char *connection_string,
        {
            fprintf(stderr,
                    _("%s: could not connect to database \"%s\": %s"),
-                   progname, dbname, PQerrorMessage(conn));
+                   progname, PQdb(conn) ? PQdb(conn) : "", PQerrorMessage(conn));
            exit_nicely(1);
        }
        else
index 247f1e1eaddaf7ea1846cb2cac9582e49dd5a23c..d1f091a65cdc34ecca3321a5e765878d1e5d234c 100644 (file)
@@ -904,7 +904,7 @@ doConnect(void)
    if (PQstatus(conn) == CONNECTION_BAD)
    {
        fprintf(stderr, "connection to database \"%s\" failed:\n%s",
-               dbName, PQerrorMessage(conn));
+               PQdb(conn), PQerrorMessage(conn));
        PQfinish(conn);
        return NULL;
    }
@@ -4166,7 +4166,8 @@ main(int argc, char **argv)
 
    if (PQstatus(con) == CONNECTION_BAD)
    {
-       fprintf(stderr, "connection to database \"%s\" failed\n", dbName);
+       fprintf(stderr, "connection to database \"%s\" failed\n",
+               PQdb(con) ? PQdb(con) : "");
        fprintf(stderr, "%s", PQerrorMessage(con));
        exit(1);
    }