Thread: pg_dumpall default database
Per discussion on -hackers, the attached patch allow the default database to be specified on the pg_dumpall command line, eg. pg_dumpall [options...] [dbname] If dbname is not specified, postgres/template1 are used per current behaviour. If a connection cannot be made to dbname, an error is returned and pg_dumpall exits. I've also added a similar error handler to catch failures to connect to postgres and pg_dumpall (currently, the dump will still be attempted). This patch should be applied on top of my previous patch (pg_dumpall_global_objects2.diff) Regards, Dave diff -c -r pgsql.orig/doc/src/sgml/ref/pg_dumpall.sgml pgsql/doc/src/sgml/ref/pg_dumpall.sgml *** pgsql.orig/doc/src/sgml/ref/pg_dumpall.sgml Mon Jan 15 13:26:07 2007 --- pgsql/doc/src/sgml/ref/pg_dumpall.sgml Mon Jan 15 13:34:31 2007 *************** *** 23,28 **** --- 23,29 ---- <cmdsynopsis> <command>pg_dumpall</command> <arg rep="repeat"><replaceable>option</replaceable></arg> + <arg><replaceable>dbname</replaceable></arg> </cmdsynopsis> </refsynopsisdiv> *************** *** 354,359 **** --- 355,372 ---- </para> </listitem> </varlistentry> + + <varlistentry> + <term><replaceable class="parameter">dbname</replaceable></term> + <listitem> + <para> + Specifies the name of the database to connect to to dump global + objects and discover what other databases should be dumped. If + not specified, the <quote>postgres</quote> database will be used, + and if that does not exist, <quote>template1</quote> will be used. + </para> + </listitem> + </varlistentry> </variablelist> </para> </refsect1> diff -c -r pgsql.orig/src/bin/pg_dump/pg_dumpall.c pgsql/src/bin/pg_dump/pg_dumpall.c *** pgsql.orig/src/bin/pg_dump/pg_dumpall.c Mon Jan 15 13:26:07 2007 --- pgsql/src/bin/pg_dump/pg_dumpall.c Mon Jan 15 12:52:34 2007 *************** *** 75,80 **** --- 75,81 ---- char *pghost = NULL; char *pgport = NULL; char *pguser = NULL; + char *pgdb = NULL; bool force_password = false; bool data_only = false; bool globals_only = false; *************** *** 299,304 **** --- 300,312 ---- if (use_setsessauth) appendPQExpBuffer(pgdumpopts, " --use-set-session-authorization"); + /* If there is an argument left, assume it's a database name */ + if (optind < argc) + { + pgdb = argv[optind]; + optind++; + } + if (optind < argc) { fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"), *************** *** 337,351 **** } /* ! * First try to connect to database "postgres", and failing that * "template1". "postgres" is the preferred choice for 8.1 and later * servers, but it usually will not exist on older ones. */ ! conn = connectDatabase("postgres", pghost, pgport, pguser, force_password, false); ! if (!conn) ! conn = connectDatabase("template1", pghost, pgport, pguser, ! force_password, true); /* * Get the active encoding and the standard_conforming_strings setting, so --- 345,384 ---- } /* ! * If there was a database specified on the command line, use that, ! * otherwise try to connect to database "postgres", and failing that * "template1". "postgres" is the preferred choice for 8.1 and later * servers, but it usually will not exist on older ones. */ ! if (pgdb) ! { ! conn = connectDatabase(pgdb, pghost, pgport, pguser, ! force_password, false); ! ! if (!conn) ! { ! fprintf(stderr, _("%s: could not connect to database \"%s\"\n"), ! progname, pgdb); ! exit(1); ! } ! } ! else ! { ! conn = connectDatabase("postgres", pghost, pgport, pguser, force_password, false); ! if (!conn) ! conn = connectDatabase("template1", pghost, pgport, pguser, ! force_password, true); ! ! if (!conn) ! { ! fprintf(stderr, _("%s: could not connect to databases \"postgres\" or \"template1\". Please specify an alternativedatabase\n"), ! progname); ! fprintf(stderr, _("Try \"%s --help\" for more information.\n"), ! progname); ! exit(1); ! } ! } /* * Get the active encoding and the standard_conforming_strings setting, so
Am Montag, 15. Januar 2007 14:44 schrieb Dave Page: > Per discussion on -hackers, the attached patch allow the default > database to be specified on the pg_dumpall command line, eg. > > pg_dumpall [options...] [dbname] I think this should be a separate option. Otherwise it would be too easy to confuse this with how pg_dump treats the argument. -- Peter Eisentraut http://developer.postgresql.org/~petere/
Peter Eisentraut wrote: > Am Montag, 15. Januar 2007 14:44 schrieb Dave Page: >> Per discussion on -hackers, the attached patch allow the default >> database to be specified on the pg_dumpall command line, eg. >> >> pg_dumpall [options...] [dbname] > > I think this should be a separate option. Otherwise it would be too easy to > confuse this with how pg_dump treats the argument. You're thinking the user might expect it dump all of that database? Not sure I agree with that, but I'm not wedded to the syntax. Any other opinions? Regards, Dave.
On Mon, 2007-01-15 at 17:07 +0000, Dave Page wrote: > You're thinking the user might expect it dump all of that database? Not > sure I agree with that, but I'm not wedded to the syntax. Any other > opinions? I agree with Peter: specifying the database name as implemented seems like it would be prone to confusion. -Neil
Neil Conway wrote: > On Mon, 2007-01-15 at 17:07 +0000, Dave Page wrote: >> You're thinking the user might expect it dump all of that database? Not >> sure I agree with that, but I'm not wedded to the syntax. Any other >> opinions? > > I agree with Peter: specifying the database name as implemented seems > like it would be prone to confusion. OK, updated patch attached. This has -E <dbname> or --default-database=<dbname> Regards, Dave. diff -c -r pgsql.orig/doc/src/sgml/ref/pg_dumpall.sgml pgsql/doc/src/sgml/ref/pg_dumpall.sgml *** pgsql.orig/doc/src/sgml/ref/pg_dumpall.sgml Mon Jan 15 13:26:07 2007 --- pgsql/doc/src/sgml/ref/pg_dumpall.sgml Mon Jan 15 21:06:23 2007 *************** *** 312,318 **** --- 312,332 ---- <variablelist> <varlistentry> + <term>-E <replaceable>dbname</replaceable></term> + <term>--default-database=<replaceable>dbname</replaceable></term> + <listitem> + <para> + Specifies the name of the database to connect to to dump global + objects and discover what other databases should be dumped. If + not specified, the <quote>postgres</quote> database will be used, + and if that does not exist, <quote>template1</quote> will be used. + </para> + </listitem> + </varlistentry> + + <varlistentry> <term>-h <replaceable>host</replaceable></term> + <term>--host=<replaceable>host</replaceable></term> <listitem> <para> Specifies the host name of the machine on which the database *************** *** 326,331 **** --- 340,346 ---- <varlistentry> <term>-p <replaceable>port</replaceable></term> + <term>--port=<replaceable>port</replaceable></term> <listitem> <para> Specifies the TCP port or local Unix domain socket file *************** *** 338,343 **** --- 353,359 ---- <varlistentry> <term>-U <replaceable>username</replaceable></term> + <term>--username=<replaceable>username</replaceable></term> <listitem> <para> Connect as the given user. *************** *** 347,352 **** --- 363,369 ---- <varlistentry> <term>-W</term> + <term>--password</term> <listitem> <para> Force a password prompt. This should happen automatically if diff -c -r pgsql.orig/src/bin/pg_dump/pg_dumpall.c pgsql/src/bin/pg_dump/pg_dumpall.c *** pgsql.orig/src/bin/pg_dump/pg_dumpall.c Mon Jan 15 13:26:07 2007 --- pgsql/src/bin/pg_dump/pg_dumpall.c Mon Jan 15 20:58:25 2007 *************** *** 75,80 **** --- 75,81 ---- char *pghost = NULL; char *pgport = NULL; char *pguser = NULL; + char *pgdb = NULL; bool force_password = false; bool data_only = false; bool globals_only = false; *************** *** 93,98 **** --- 94,100 ---- {"inserts", no_argument, NULL, 'd'}, {"attribute-inserts", no_argument, NULL, 'D'}, {"column-inserts", no_argument, NULL, 'D'}, + {"default-database", required_argument, NULL, 'E'}, {"globals-only", no_argument, NULL, 'g'}, {"host", required_argument, NULL, 'h'}, {"ignore-version", no_argument, NULL, 'i'}, *************** *** 165,171 **** pgdumpopts = createPQExpBuffer(); ! while ((c = getopt_long(argc, argv, "acdDgh:ioOp:rsS:tU:vWxX:", long_options, &optindex)) != -1) { switch (c) { --- 167,173 ---- pgdumpopts = createPQExpBuffer(); ! while ((c = getopt_long(argc, argv, "acdDE:gh:ioOp:rsS:tU:vWxX:", long_options, &optindex)) != -1) { switch (c) { *************** *** 182,187 **** --- 184,193 ---- case 'D': appendPQExpBuffer(pgdumpopts, " -%c", c); break; + + case 'E': + pgdb = optarg; + break; case 'g': globals_only = true; *************** *** 337,351 **** } /* ! * First try to connect to database "postgres", and failing that * "template1". "postgres" is the preferred choice for 8.1 and later * servers, but it usually will not exist on older ones. */ ! conn = connectDatabase("postgres", pghost, pgport, pguser, force_password, false); ! if (!conn) ! conn = connectDatabase("template1", pghost, pgport, pguser, ! force_password, true); /* * Get the active encoding and the standard_conforming_strings setting, so --- 343,382 ---- } /* ! * If there was a database specified on the command line, use that, ! * otherwise try to connect to database "postgres", and failing that * "template1". "postgres" is the preferred choice for 8.1 and later * servers, but it usually will not exist on older ones. */ ! if (pgdb) ! { ! conn = connectDatabase(pgdb, pghost, pgport, pguser, ! force_password, false); ! ! if (!conn) ! { ! fprintf(stderr, _("%s: could not connect to database \"%s\"\n"), ! progname, pgdb); ! exit(1); ! } ! } ! else ! { ! conn = connectDatabase("postgres", pghost, pgport, pguser, force_password, false); ! if (!conn) ! conn = connectDatabase("template1", pghost, pgport, pguser, ! force_password, true); ! ! if (!conn) ! { ! fprintf(stderr, _("%s: could not connect to databases \"postgres\" or \"template1\". Please specify an alternativedatabase\n"), ! progname); ! fprintf(stderr, _("Try \"%s --help\" for more information.\n"), ! progname); ! exit(1); ! } ! } /* * Get the active encoding and the standard_conforming_strings setting, so *************** *** 443,448 **** --- 474,481 ---- " OWNER TO commands\n")); printf(_("\nConnection options:\n")); + printf(_(" -E,--default-database=dbname\n" + " specify an alternate default database\n")); printf(_(" -h, --host=HOSTNAME database server host or socket directory\n")); printf(_(" -p, --port=PORT database server port number\n")); printf(_(" -U, --username=NAME connect as specified database user\n"));
Dave Page <dpage@postgresql.org> writes: > OK, updated patch attached. This has > -E <dbname> or --default-database=<dbname> Not sure that "default" database is a particularly helpful adjective; why shouldn't the switch just be --database? Other than that, looks fine. regards, tom lane
Dave Page wrote: >> I agree with Peter: specifying the database name as implemented seems >> like it would be prone to confusion. > > OK, updated patch attached. This has > > -E <dbname> or --default-database=<dbname> Ouch. This calls for confusion with the -E flag of pg_dump, which means 'encoding'. Something else might be better. It would be nice to be able to specify the encoding for pg_dumpall, though, rather than having to set PG_CLIENTENCODING. Yours, Laurenz Albe
Tom Lane wrote: > Dave Page <dpage@postgresql.org> writes: >> OK, updated patch attached. This has > >> -E <dbname> or --default-database=<dbname> > > Not sure that "default" database is a particularly helpful adjective; > why shouldn't the switch just be --database? > > Other than that, looks fine. Updated to -l, --database to address yours, and Albe's concerns. Regards, Dave diff -c -r pgsql.orig/doc/src/sgml/ref/pg_dumpall.sgml pgsql.defaultdb/doc/src/sgml/ref/pg_dumpall.sgml *** pgsql.orig/doc/src/sgml/ref/pg_dumpall.sgml Mon Jan 15 13:26:07 2007 --- pgsql.defaultdb/doc/src/sgml/ref/pg_dumpall.sgml Tue Jan 16 09:23:19 2007 *************** *** 313,318 **** --- 313,319 ---- <variablelist> <varlistentry> <term>-h <replaceable>host</replaceable></term> + <term>--host=<replaceable>host</replaceable></term> <listitem> <para> Specifies the host name of the machine on which the database *************** *** 323,331 **** --- 324,346 ---- </para> </listitem> </varlistentry> + + <varlistentry> + <term>-l <replaceable>dbname</replaceable></term> + <term>--database=<replaceable>dbname</replaceable></term> + <listitem> + <para> + Specifies the name of the database to connect to to dump global + objects and discover what other databases should be dumped. If + not specified, the <quote>postgres</quote> database will be used, + and if that does not exist, <quote>template1</quote> will be used. + </para> + </listitem> + </varlistentry> <varlistentry> <term>-p <replaceable>port</replaceable></term> + <term>--port=<replaceable>port</replaceable></term> <listitem> <para> Specifies the TCP port or local Unix domain socket file *************** *** 338,343 **** --- 353,359 ---- <varlistentry> <term>-U <replaceable>username</replaceable></term> + <term>--username=<replaceable>username</replaceable></term> <listitem> <para> Connect as the given user. *************** *** 347,352 **** --- 363,369 ---- <varlistentry> <term>-W</term> + <term>--password</term> <listitem> <para> Force a password prompt. This should happen automatically if diff -c -r pgsql.orig/src/bin/pg_dump/pg_dumpall.c pgsql.defaultdb/src/bin/pg_dump/pg_dumpall.c *** pgsql.orig/src/bin/pg_dump/pg_dumpall.c Mon Jan 15 13:26:07 2007 --- pgsql.defaultdb/src/bin/pg_dump/pg_dumpall.c Tue Jan 16 09:26:03 2007 *************** *** 75,80 **** --- 75,81 ---- char *pghost = NULL; char *pgport = NULL; char *pguser = NULL; + char *pgdb = NULL; bool force_password = false; bool data_only = false; bool globals_only = false; *************** *** 96,101 **** --- 97,103 ---- {"globals-only", no_argument, NULL, 'g'}, {"host", required_argument, NULL, 'h'}, {"ignore-version", no_argument, NULL, 'i'}, + {"database", required_argument, NULL, 'l'}, {"oids", no_argument, NULL, 'o'}, {"no-owner", no_argument, NULL, 'O'}, {"port", required_argument, NULL, 'p'}, *************** *** 165,171 **** pgdumpopts = createPQExpBuffer(); ! while ((c = getopt_long(argc, argv, "acdDgh:ioOp:rsS:tU:vWxX:", long_options, &optindex)) != -1) { switch (c) { --- 167,173 ---- pgdumpopts = createPQExpBuffer(); ! while ((c = getopt_long(argc, argv, "acdDgh:il:oOp:rsS:tU:vWxX:", long_options, &optindex)) != -1) { switch (c) { *************** *** 201,206 **** --- 203,212 ---- ignoreVersion = true; appendPQExpBuffer(pgdumpopts, " -i"); break; + + case 'l': + pgdb = optarg; + break; case 'o': appendPQExpBuffer(pgdumpopts, " -o"); *************** *** 337,351 **** } /* ! * First try to connect to database "postgres", and failing that * "template1". "postgres" is the preferred choice for 8.1 and later * servers, but it usually will not exist on older ones. */ ! conn = connectDatabase("postgres", pghost, pgport, pguser, force_password, false); ! if (!conn) ! conn = connectDatabase("template1", pghost, pgport, pguser, ! force_password, true); /* * Get the active encoding and the standard_conforming_strings setting, so --- 343,382 ---- } /* ! * If there was a database specified on the command line, use that, ! * otherwise try to connect to database "postgres", and failing that * "template1". "postgres" is the preferred choice for 8.1 and later * servers, but it usually will not exist on older ones. */ ! if (pgdb) ! { ! conn = connectDatabase(pgdb, pghost, pgport, pguser, ! force_password, false); ! ! if (!conn) ! { ! fprintf(stderr, _("%s: could not connect to database \"%s\"\n"), ! progname, pgdb); ! exit(1); ! } ! } ! else ! { ! conn = connectDatabase("postgres", pghost, pgport, pguser, force_password, false); ! if (!conn) ! conn = connectDatabase("template1", pghost, pgport, pguser, ! force_password, true); ! ! if (!conn) ! { ! fprintf(stderr, _("%s: could not connect to databases \"postgres\" or \"template1\". Please specify an alternativedatabase\n"), ! progname); ! fprintf(stderr, _("Try \"%s --help\" for more information.\n"), ! progname); ! exit(1); ! } ! } /* * Get the active encoding and the standard_conforming_strings setting, so *************** *** 444,449 **** --- 475,481 ---- printf(_("\nConnection options:\n")); printf(_(" -h, --host=HOSTNAME database server host or socket directory\n")); + printf(_(" -l, --database=dbname specify an alternate default database\n")); printf(_(" -p, --port=PORT database server port number\n")); printf(_(" -U, --username=NAME connect as specified database user\n")); printf(_(" -W, --password force password prompt (should happen automatically)\n"));
Patch applied. Thanks. --------------------------------------------------------------------------- Dave Page wrote: > Tom Lane wrote: > > Dave Page <dpage@postgresql.org> writes: > >> OK, updated patch attached. This has > > > >> -E <dbname> or --default-database=<dbname> > > > > Not sure that "default" database is a particularly helpful adjective; > > why shouldn't the switch just be --database? > > > > Other than that, looks fine. > > Updated to -l, --database to address yours, and Albe's concerns. > > Regards, Dave > diff -c -r pgsql.orig/doc/src/sgml/ref/pg_dumpall.sgml pgsql.defaultdb/doc/src/sgml/ref/pg_dumpall.sgml > *** pgsql.orig/doc/src/sgml/ref/pg_dumpall.sgml Mon Jan 15 13:26:07 2007 > --- pgsql.defaultdb/doc/src/sgml/ref/pg_dumpall.sgml Tue Jan 16 09:23:19 2007 > *************** > *** 313,318 **** > --- 313,319 ---- > <variablelist> > <varlistentry> > <term>-h <replaceable>host</replaceable></term> > + <term>--host=<replaceable>host</replaceable></term> > <listitem> > <para> > Specifies the host name of the machine on which the database > *************** > *** 323,331 **** > --- 324,346 ---- > </para> > </listitem> > </varlistentry> > + > + <varlistentry> > + <term>-l <replaceable>dbname</replaceable></term> > + <term>--database=<replaceable>dbname</replaceable></term> > + <listitem> > + <para> > + Specifies the name of the database to connect to to dump global > + objects and discover what other databases should be dumped. If > + not specified, the <quote>postgres</quote> database will be used, > + and if that does not exist, <quote>template1</quote> will be used. > + </para> > + </listitem> > + </varlistentry> > > <varlistentry> > <term>-p <replaceable>port</replaceable></term> > + <term>--port=<replaceable>port</replaceable></term> > <listitem> > <para> > Specifies the TCP port or local Unix domain socket file > *************** > *** 338,343 **** > --- 353,359 ---- > > <varlistentry> > <term>-U <replaceable>username</replaceable></term> > + <term>--username=<replaceable>username</replaceable></term> > <listitem> > <para> > Connect as the given user. > *************** > *** 347,352 **** > --- 363,369 ---- > > <varlistentry> > <term>-W</term> > + <term>--password</term> > <listitem> > <para> > Force a password prompt. This should happen automatically if > diff -c -r pgsql.orig/src/bin/pg_dump/pg_dumpall.c pgsql.defaultdb/src/bin/pg_dump/pg_dumpall.c > *** pgsql.orig/src/bin/pg_dump/pg_dumpall.c Mon Jan 15 13:26:07 2007 > --- pgsql.defaultdb/src/bin/pg_dump/pg_dumpall.c Tue Jan 16 09:26:03 2007 > *************** > *** 75,80 **** > --- 75,81 ---- > char *pghost = NULL; > char *pgport = NULL; > char *pguser = NULL; > + char *pgdb = NULL; > bool force_password = false; > bool data_only = false; > bool globals_only = false; > *************** > *** 96,101 **** > --- 97,103 ---- > {"globals-only", no_argument, NULL, 'g'}, > {"host", required_argument, NULL, 'h'}, > {"ignore-version", no_argument, NULL, 'i'}, > + {"database", required_argument, NULL, 'l'}, > {"oids", no_argument, NULL, 'o'}, > {"no-owner", no_argument, NULL, 'O'}, > {"port", required_argument, NULL, 'p'}, > *************** > *** 165,171 **** > > pgdumpopts = createPQExpBuffer(); > > ! while ((c = getopt_long(argc, argv, "acdDgh:ioOp:rsS:tU:vWxX:", long_options, &optindex)) != -1) > { > switch (c) > { > --- 167,173 ---- > > pgdumpopts = createPQExpBuffer(); > > ! while ((c = getopt_long(argc, argv, "acdDgh:il:oOp:rsS:tU:vWxX:", long_options, &optindex)) != -1) > { > switch (c) > { > *************** > *** 201,206 **** > --- 203,212 ---- > ignoreVersion = true; > appendPQExpBuffer(pgdumpopts, " -i"); > break; > + > + case 'l': > + pgdb = optarg; > + break; > > case 'o': > appendPQExpBuffer(pgdumpopts, " -o"); > *************** > *** 337,351 **** > } > > /* > ! * First try to connect to database "postgres", and failing that > * "template1". "postgres" is the preferred choice for 8.1 and later > * servers, but it usually will not exist on older ones. > */ > ! conn = connectDatabase("postgres", pghost, pgport, pguser, > force_password, false); > ! if (!conn) > ! conn = connectDatabase("template1", pghost, pgport, pguser, > ! force_password, true); > > /* > * Get the active encoding and the standard_conforming_strings setting, so > --- 343,382 ---- > } > > /* > ! * If there was a database specified on the command line, use that, > ! * otherwise try to connect to database "postgres", and failing that > * "template1". "postgres" is the preferred choice for 8.1 and later > * servers, but it usually will not exist on older ones. > */ > ! if (pgdb) > ! { > ! conn = connectDatabase(pgdb, pghost, pgport, pguser, > ! force_password, false); > ! > ! if (!conn) > ! { > ! fprintf(stderr, _("%s: could not connect to database \"%s\"\n"), > ! progname, pgdb); > ! exit(1); > ! } > ! } > ! else > ! { > ! conn = connectDatabase("postgres", pghost, pgport, pguser, > force_password, false); > ! if (!conn) > ! conn = connectDatabase("template1", pghost, pgport, pguser, > ! force_password, true); > ! > ! if (!conn) > ! { > ! fprintf(stderr, _("%s: could not connect to databases \"postgres\" or \"template1\". Please specify an alternativedatabase\n"), > ! progname); > ! fprintf(stderr, _("Try \"%s --help\" for more information.\n"), > ! progname); > ! exit(1); > ! } > ! } > > /* > * Get the active encoding and the standard_conforming_strings setting, so > *************** > *** 444,449 **** > --- 475,481 ---- > > printf(_("\nConnection options:\n")); > printf(_(" -h, --host=HOSTNAME database server host or socket directory\n")); > + printf(_(" -l, --database=dbname specify an alternate default database\n")); > printf(_(" -p, --port=PORT database server port number\n")); > printf(_(" -U, --username=NAME connect as specified database user\n")); > printf(_(" -W, --password force password prompt (should happen automatically)\n")); > > ---------------------------(end of broadcast)--------------------------- > TIP 2: Don't 'kill -9' the postmaster -- Bruce Momjian bruce@momjian.us EnterpriseDB http://www.enterprisedb.com + If your life is a hard drive, Christ can be your backup. +