From 5c78f797701b90546279b994791fc07af5bbb5c9 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Mon, 19 Oct 2020 19:03:47 -0400 Subject: [PATCH] Fix connection string handling in src/bin/scripts/ programs. When told to process all databases, clusterdb, reindexdb, and vacuumdb would reconnect by replacing their --maintenance-db parameter with the name of the target database. If that parameter is a connstring (which has been allowed for a long time, though we failed to document that before this patch), we'd lose any other options it might specify, for example SSL or GSS parameters, possibly resulting in failure to connect. Thus, this is the same bug as commit a45bc8a4f fixed in pg_dump and pg_restore. We can fix it in the same way, by using libpq's rules for handling multiple "dbname" parameters to add the target database name separately. I chose to apply the same refactoring approach as in that patch, with a struct to handle the command line parameters that need to be passed through to connectDatabase. (Maybe someday we can unify the very similar functions here and in pg_dump/pg_restore.) Per Peter Eisentraut's comments on bug #16604. Back-patch to all supported branches. Discussion: https://postgr.es/m/16604-933f4b8791227b15@postgresql.org --- doc/src/sgml/ref/clusterdb.sgml | 20 +++++--- doc/src/sgml/ref/createdb.sgml | 3 ++ doc/src/sgml/ref/dropdb.sgml | 3 ++ doc/src/sgml/ref/reindexdb.sgml | 20 +++++--- doc/src/sgml/ref/vacuumdb.sgml | 20 +++++--- src/bin/scripts/clusterdb.c | 67 +++++++++++-------------- src/bin/scripts/common.c | 87 ++++++++++++++++++++------------- src/bin/scripts/common.h | 26 +++++++--- src/bin/scripts/createdb.c | 11 ++++- src/bin/scripts/createlang.c | 12 ++++- src/bin/scripts/createuser.c | 11 ++++- src/bin/scripts/dropdb.c | 12 +++-- src/bin/scripts/droplang.c | 12 ++++- src/bin/scripts/dropuser.c | 13 +++-- src/bin/scripts/reindexdb.c | 85 ++++++++++++++++---------------- src/bin/scripts/vacuumdb.c | 70 ++++++++++++-------------- 16 files changed, 273 insertions(+), 199 deletions(-) diff --git a/doc/src/sgml/ref/clusterdb.sgml b/doc/src/sgml/ref/clusterdb.sgml index 8e4f9c9cbbf..60edd33a370 100644 --- a/doc/src/sgml/ref/clusterdb.sgml +++ b/doc/src/sgml/ref/clusterdb.sgml @@ -90,9 +90,9 @@ PostgreSQL documentation - Specifies the name of the database to be clustered. - If this is not specified and (or - ) is not used, the database name is read + Specifies the name of the database to be clustered, + when / is not used. + If this is not specified, the database name is read from the environment variable PGDATABASE. If that is not set, the user name specified for the connection is used. The dbname can be a diff --git a/doc/src/sgml/ref/dropdb.sgml b/doc/src/sgml/ref/dropdb.sgml index 16c49e7928b..0d7170d6655 100644 --- a/doc/src/sgml/ref/dropdb.sgml +++ b/doc/src/sgml/ref/dropdb.sgml @@ -205,6 +205,9 @@ PostgreSQL documentation target database. If not specified, the postgres database will be used; if that does not exist (or is the database being dropped), template1 will be used. + This can be a connection + string. If so, connection string parameters will override any + conflicting command line options. diff --git a/doc/src/sgml/ref/reindexdb.sgml b/doc/src/sgml/ref/reindexdb.sgml index 2f4b4618edc..a426a32e9c8 100644 --- a/doc/src/sgml/ref/reindexdb.sgml +++ b/doc/src/sgml/ref/reindexdb.sgml @@ -123,9 +123,9 @@ PostgreSQL documentation - Specifies the name of the database to be reindexed. - If this is not specified and (or - ) is not used, the database name is read + Specifies the name of the database to be reindexed, + when / is not used. + If this is not specified, the database name is read from the environment variable PGDATABASE. If that is not set, the user name specified for the connection is used. The dbname can be a