mirror of
https://github.com/postgres/postgres.git
synced 2025-07-03 20:02:46 +03:00
Make command-line tools smarter about finding a DB to connect to.
If unable to connect to "postgres", try "template1". This allows things to work more smoothly in the case where the postgres database has been dropped. And just in case that's not good enough, also allow the user to specify a maintenance database to be used for the initial connection, to cover the case where neither postgres nor template1 is suitable.
This commit is contained in:
@ -18,7 +18,8 @@ static void cluster_one_database(const char *dbname, bool verbose, const char *t
|
||||
const char *host, const char *port,
|
||||
const char *username, enum trivalue prompt_password,
|
||||
const char *progname, bool echo);
|
||||
static void cluster_all_databases(bool verbose, const char *host, const char *port,
|
||||
static void cluster_all_databases(bool verbose, const char *maintenance_db,
|
||||
const char *host, const char *port,
|
||||
const char *username, enum trivalue prompt_password,
|
||||
const char *progname, bool echo, bool quiet);
|
||||
|
||||
@ -40,6 +41,7 @@ main(int argc, char *argv[])
|
||||
{"all", no_argument, NULL, 'a'},
|
||||
{"table", required_argument, NULL, 't'},
|
||||
{"verbose", no_argument, NULL, 'v'},
|
||||
{"maintenance-db", required_argument, NULL, 2},
|
||||
{NULL, 0, NULL, 0}
|
||||
};
|
||||
|
||||
@ -48,6 +50,7 @@ main(int argc, char *argv[])
|
||||
int c;
|
||||
|
||||
const char *dbname = NULL;
|
||||
const char *maintenance_db = NULL;
|
||||
char *host = NULL;
|
||||
char *port = NULL;
|
||||
char *username = NULL;
|
||||
@ -100,6 +103,9 @@ main(int argc, char *argv[])
|
||||
case 'v':
|
||||
verbose = true;
|
||||
break;
|
||||
case 2:
|
||||
maintenance_db = optarg;
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
||||
exit(1);
|
||||
@ -137,7 +143,7 @@ main(int argc, char *argv[])
|
||||
exit(1);
|
||||
}
|
||||
|
||||
cluster_all_databases(verbose, host, port, username, prompt_password,
|
||||
cluster_all_databases(verbose, maintenance_db, host, port, username, prompt_password,
|
||||
progname, echo, quiet);
|
||||
}
|
||||
else
|
||||
@ -180,7 +186,8 @@ cluster_one_database(const char *dbname, bool verbose, const char *table,
|
||||
appendPQExpBuffer(&sql, " %s", table);
|
||||
appendPQExpBuffer(&sql, ";\n");
|
||||
|
||||
conn = connectDatabase(dbname, host, port, username, prompt_password, progname);
|
||||
conn = connectDatabase(dbname, host, port, username, prompt_password,
|
||||
progname, false);
|
||||
if (!executeMaintenanceCommand(conn, sql.data, echo))
|
||||
{
|
||||
if (table)
|
||||
@ -198,7 +205,8 @@ cluster_one_database(const char *dbname, bool verbose, const char *table,
|
||||
|
||||
|
||||
static void
|
||||
cluster_all_databases(bool verbose, const char *host, const char *port,
|
||||
cluster_all_databases(bool verbose, const char *maintenance_db,
|
||||
const char *host, const char *port,
|
||||
const char *username, enum trivalue prompt_password,
|
||||
const char *progname, bool echo, bool quiet)
|
||||
{
|
||||
@ -206,7 +214,8 @@ cluster_all_databases(bool verbose, const char *host, const char *port,
|
||||
PGresult *result;
|
||||
int i;
|
||||
|
||||
conn = connectDatabase("postgres", host, port, username, prompt_password, progname);
|
||||
conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
|
||||
prompt_password, progname);
|
||||
result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname, echo);
|
||||
PQfinish(conn);
|
||||
|
||||
@ -250,6 +259,7 @@ help(const char *progname)
|
||||
printf(_(" -U, --username=USERNAME user name to connect as\n"));
|
||||
printf(_(" -w, --no-password never prompt for password\n"));
|
||||
printf(_(" -W, --password force password prompt\n"));
|
||||
printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
|
||||
printf(_("\nRead the description of the SQL command CLUSTER for details.\n"));
|
||||
printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
|
||||
}
|
||||
|
Reference in New Issue
Block a user