66 * Portions Copyright (c) 1994, Regents of the University of California
77 *
88 *
9- * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.87 2007/01/25 02:30:32 momjian Exp $
9+ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.88 2007/01/25 02:46:33 momjian Exp $
1010 *
1111 *-------------------------------------------------------------------------
1212 */
@@ -75,6 +75,7 @@ main(int argc, char *argv[])
7575char * pghost = NULL ;
7676char * pgport = NULL ;
7777char * pguser = NULL ;
78+ char * pgdb = NULL ;
7879bool force_password = false;
7980bool data_only = false;
8081bool globals_only = false;
@@ -96,6 +97,7 @@ main(int argc, char *argv[])
9697{"globals-only" ,no_argument ,NULL ,'g' },
9798{"host" ,required_argument ,NULL ,'h' },
9899{"ignore-version" ,no_argument ,NULL ,'i' },
100+ {"database" ,required_argument ,NULL ,'l' },
99101{"oids" ,no_argument ,NULL ,'o' },
100102{"no-owner" ,no_argument ,NULL ,'O' },
101103{"port" ,required_argument ,NULL ,'p' },
@@ -165,7 +167,7 @@ main(int argc, char *argv[])
165167
166168pgdumpopts = createPQExpBuffer ();
167169
168- while ((c = getopt_long (argc ,argv ,"acdDgh:ioOp :rsS:tU:vWxX:" ,long_options ,& optindex ))!= -1 )
170+ while ((c = getopt_long (argc ,argv ,"acdDgh:il:oOp :rsS:tU:vWxX:" ,long_options ,& optindex ))!= -1 )
169171{
170172switch (c )
171173{
@@ -201,6 +203,10 @@ main(int argc, char *argv[])
201203ignoreVersion = true;
202204appendPQExpBuffer (pgdumpopts ," -i" );
203205break ;
206+
207+ case 'l' :
208+ pgdb = optarg ;
209+ break ;
204210
205211case 'o' :
206212appendPQExpBuffer (pgdumpopts ," -o" );
@@ -337,15 +343,40 @@ main(int argc, char *argv[])
337343}
338344
339345/*
340- * First try to connect to database "postgres", and failing that
346+ * If there was a database specified on the command line, use that,
347+ * otherwise try to connect to database "postgres", and failing that
341348 * "template1". "postgres" is the preferred choice for 8.1 and later
342349 * servers, but it usually will not exist on older ones.
343350 */
344- conn = connectDatabase ("postgres" ,pghost ,pgport ,pguser ,
351+ if (pgdb )
352+ {
353+ conn = connectDatabase (pgdb ,pghost ,pgport ,pguser ,
354+ force_password , false);
355+
356+ if (!conn )
357+ {
358+ fprintf (stderr ,_ ("%s: could not connect to database \"%s\"\n" ),
359+ progname ,pgdb );
360+ exit (1 );
361+ }
362+ }
363+ else
364+ {
365+ conn = connectDatabase ("postgres" ,pghost ,pgport ,pguser ,
345366force_password , false);
346- if (!conn )
347- conn = connectDatabase ("template1" ,pghost ,pgport ,pguser ,
348- force_password , true);
367+ if (!conn )
368+ conn = connectDatabase ("template1" ,pghost ,pgport ,pguser ,
369+ force_password , true);
370+
371+ if (!conn )
372+ {
373+ fprintf (stderr ,_ ("%s: could not connect to databases \"postgres\" or \"template1\". Please specify an alternative database\n" ),
374+ progname );
375+ fprintf (stderr ,_ ("Try \"%s --help\" for more information.\n" ),
376+ progname );
377+ exit (1 );
378+ }
379+ }
349380
350381/*
351382 * Get the active encoding and the standard_conforming_strings setting, so
@@ -444,6 +475,7 @@ help(void)
444475
445476printf (_ ("\nConnection options:\n" ));
446477printf (_ (" -h, --host=HOSTNAME database server host or socket directory\n" ));
478+ printf (_ (" -l, --database=dbname specify an alternate default database\n" ));
447479printf (_ (" -p, --port=PORT database server port number\n" ));
448480printf (_ (" -U, --username=NAME connect as specified database user\n" ));
449481printf (_ (" -W, --password force password prompt (should happen automatically)\n" ));