Skip to content

Commit 691e595

Browse files
committed
Add -d/--dbname option to pg_dump.
You could already pass a database name just by passing it as the last option, without -d. This is an alias for that, like the -d/--dbname option in psql and many other client applications. For consistency.
1 parent a64e33f commit 691e595

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

doc/src/sgml/ref/pg_dump.sgml

+20
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,26 @@ PostgreSQL documentation
817817
The following command-line options control the database connection parameters.
818818

819819
<variablelist>
820+
<varlistentry>
821+
<term><option>-d <replaceable class="parameter">dbname</replaceable></></term>
822+
<term><option>--dbname=<replaceable class="parameter">dbname</replaceable></></term>
823+
<listitem>
824+
<para>
825+
Specifies the name of the database to connect to. This is
826+
equivalent to specifying <replaceable
827+
class="parameter">dbname</replaceable> as the first non-option
828+
argument on the command line.
829+
</para>
830+
<para>
831+
If this parameter contains an <symbol>=</symbol> sign or starts
832+
with a valid <acronym>URI</acronym> prefix
833+
(<literal>postgresql://</literal>
834+
or <literal>postgres://</literal>), it is treated as a
835+
<parameter>conninfo</parameter> string. See <xref linkend="libpq-connect"> for more information.
836+
</para>
837+
</listitem>
838+
</varlistentry>
839+
820840
<varlistentry>
821841
<term><option>-h <replaceable class="parameter">host</replaceable></option></term>
822842
<term><option>--host=<replaceable class="parameter">host</replaceable></option></term>

src/bin/pg_dump/pg_dump.c

+12-3
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ main(int argc, char **argv)
307307
{"blobs", no_argument, NULL, 'b'},
308308
{"clean", no_argument, NULL, 'c'},
309309
{"create", no_argument, NULL, 'C'},
310+
{"dbname", required_argument, NULL, 'd'},
310311
{"file", required_argument, NULL, 'f'},
311312
{"format", required_argument, NULL, 'F'},
312313
{"host", required_argument, NULL, 'h'},
@@ -387,7 +388,7 @@ main(int argc, char **argv)
387388
}
388389
}
389390

390-
while ((c = getopt_long(argc, argv, "abcCE:f:F:h:in:N:oOp:RsS:t:T:U:vwWxZ:",
391+
while ((c = getopt_long(argc, argv, "abcCd:E:f:F:h:iK:n:N:oOp:RsS:t:T:U:vwWxZ:",
391392
long_options, &optindex)) != -1)
392393
{
393394
switch (c)
@@ -408,6 +409,10 @@ main(int argc, char **argv)
408409
outputCreateDB = 1;
409410
break;
410411

412+
case 'd': /* database name */
413+
dbname = pg_strdup(optarg);
414+
break;
415+
411416
case 'E': /* Dump encoding */
412417
dumpencoding = pg_strdup(optarg);
413418
break;
@@ -520,8 +525,11 @@ main(int argc, char **argv)
520525
}
521526
}
522527

523-
/* Get database name from command line */
524-
if (optind < argc)
528+
/*
529+
* Non-option argument specifies database name as long as it wasn't
530+
* already specified with -d / --dbname
531+
*/
532+
if (optind < argc && dbname == NULL)
525533
dbname = argv[optind++];
526534

527535
/* Complain if any arguments remain */
@@ -872,6 +880,7 @@ help(const char *progname)
872880
" ALTER OWNER commands to set ownership\n"));
873881

874882
printf(_("\nConnection options:\n"));
883+
printf(_(" -d, --dbname=DBNAME database to dump\n"));
875884
printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
876885
printf(_(" -p, --port=PORT database server port number\n"));
877886
printf(_(" -U, --username=NAME connect as specified database user\n"));

0 commit comments

Comments
 (0)