diff options
author | Robert Haas <rhaas@postgresql.org> | 2011-12-06 08:48:15 -0500 |
---|---|---|
committer | Robert Haas <rhaas@postgresql.org> | 2011-12-06 08:48:15 -0500 |
commit | 68281e00540a29e7f9bfc154c25a78b875f560d1 (patch) | |
tree | edd65f9661d0efcdc11780cea0466c1f8aeb6715 /src/bin/scripts/createdb.c | |
parent | 6ef4ae1d4e829675c332c39895fdf90e15c3faeb (diff) | |
download | postgresql-68281e00540a29e7f9bfc154c25a78b875f560d1.tar.gz postgresql-68281e00540a29e7f9bfc154c25a78b875f560d1.zip |
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.
Diffstat (limited to 'src/bin/scripts/createdb.c')
-rw-r--r-- | src/bin/scripts/createdb.c | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/src/bin/scripts/createdb.c b/src/bin/scripts/createdb.c index d7c3928eb6e..64105412589 100644 --- a/src/bin/scripts/createdb.c +++ b/src/bin/scripts/createdb.c @@ -35,6 +35,7 @@ main(int argc, char *argv[]) {"lc-collate", required_argument, NULL, 1}, {"lc-ctype", required_argument, NULL, 2}, {"locale", required_argument, NULL, 'l'}, + {"maintenance-db", required_argument, NULL, 3}, {NULL, 0, NULL, 0} }; @@ -43,6 +44,7 @@ main(int argc, char *argv[]) int c; const char *dbname = NULL; + const char *maintenance_db = NULL; char *comment = NULL; char *host = NULL; char *port = NULL; @@ -110,6 +112,9 @@ main(int argc, char *argv[]) case 'l': locale = optarg; break; + case 3: + maintenance_db = optarg; + break; default: fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); @@ -192,13 +197,12 @@ main(int argc, char *argv[]) appendPQExpBuffer(&sql, ";\n"); - /* - * Connect to the 'postgres' database by default, except have the - * 'postgres' user use 'template1' so he can create the 'postgres' - * database. - */ - conn = connectDatabase(strcmp(dbname, "postgres") == 0 ? "template1" : "postgres", - host, port, username, prompt_password, progname); + /* No point in trying to use postgres db when creating postgres db. */ + if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0) + maintenance_db = "template1"; + + conn = connectMaintenanceDatabase(maintenance_db, host, port, username, + prompt_password, progname); if (echo) printf("%s", sql.data); @@ -264,6 +268,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(_("\nBy default, a database with the same name as the current user is created.\n")); printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n")); } |