6
6
* Portions Copyright (c) 1994, Regents of the University of California
7
7
*
8
8
*
9
- * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.62 2005/06/26 03:03:48 momjian Exp $
9
+ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.63 2005/07/08 16:51:30 tgl Exp $
10
10
*
11
11
*-------------------------------------------------------------------------
12
12
*/
@@ -55,7 +55,7 @@ static void dumpTimestamp(char *msg);
55
55
56
56
static int runPgDump (const char * dbname );
57
57
static PGconn * connectDatabase (const char * dbname , const char * pghost , const char * pgport ,
58
- const char * pguser , bool require_password );
58
+ const char * pguser , bool require_password , bool fail_on_error );
59
59
static PGresult * executeQuery (PGconn * conn , const char * query );
60
60
61
61
char pg_dump_bin [MAXPGPATH ];
@@ -296,8 +296,16 @@ main(int argc, char *argv[])
296
296
exit (1 );
297
297
}
298
298
299
-
300
- conn = connectDatabase ("postgres" , pghost , pgport , pguser , force_password );
299
+ /*
300
+ * First try to connect to database "postgres", and failing that
301
+ * "template1". "postgres" is the preferred choice for 8.1 and later
302
+ * servers, but it usually will not exist on older ones.
303
+ */
304
+ conn = connectDatabase ("postgres" , pghost , pgport , pguser ,
305
+ force_password , false);
306
+ if (!conn )
307
+ conn = connectDatabase ("template1" , pghost , pgport , pguser ,
308
+ force_password , true);
301
309
302
310
printf ("--\n-- PostgreSQL database cluster dump\n--\n\n" );
303
311
if (verbose )
@@ -382,6 +390,7 @@ help(void)
382
390
static void
383
391
dumpUsers (PGconn * conn , bool initdbonly )
384
392
{
393
+ PQExpBuffer buf = createPQExpBuffer ();
385
394
PGresult * res ;
386
395
int i ;
387
396
@@ -407,7 +416,6 @@ dumpUsers(PGconn *conn, bool initdbonly)
407
416
{
408
417
const char * username ;
409
418
bool clusterowner ;
410
- PQExpBuffer buf = createPQExpBuffer ();
411
419
412
420
username = PQgetvalue (res , i , 0 );
413
421
clusterowner = (strcmp (PQgetvalue (res , i , 6 ), "t" ) == 0 );
@@ -421,12 +429,9 @@ dumpUsers(PGconn *conn, bool initdbonly)
421
429
* other users
422
430
*/
423
431
if (!clusterowner )
424
- appendPQExpBuffer (buf , "CREATE USER %s WITH SYSID %s" ,
425
- fmtId (username ),
426
- PQgetvalue (res , i , 1 ));
432
+ printfPQExpBuffer (buf , "CREATE USER %s WITH" , fmtId (username ));
427
433
else
428
- appendPQExpBuffer (buf , "ALTER USER %s WITH" ,
429
- fmtId (username ));
434
+ printfPQExpBuffer (buf , "ALTER USER %s WITH" , fmtId (username ));
430
435
431
436
if (!PQgetisnull (res , i , 2 ))
432
437
{
@@ -451,14 +456,16 @@ dumpUsers(PGconn *conn, bool initdbonly)
451
456
appendPQExpBuffer (buf , ";\n" );
452
457
453
458
printf ("%s" , buf -> data );
454
- destroyPQExpBuffer (buf );
455
459
456
460
if (server_version >= 70300 )
457
461
dumpUserConfig (conn , username );
458
462
}
459
463
460
464
PQclear (res );
465
+
461
466
printf ("\n\n" );
467
+
468
+ destroyPQExpBuffer (buf );
462
469
}
463
470
464
471
@@ -472,7 +479,7 @@ dumpGroups(PGconn *conn)
472
479
PGresult * res ;
473
480
int i ;
474
481
475
- res = executeQuery (conn , "SELECT groname, grosysid, grolist FROM pg_group" );
482
+ res = executeQuery (conn , "SELECT groname, grolist FROM pg_group" );
476
483
477
484
if (PQntuples (res ) > 0 || output_clean )
478
485
printf ("--\n-- Groups\n--\n\n" );
@@ -485,11 +492,10 @@ dumpGroups(PGconn *conn)
485
492
char * val ;
486
493
char * tok ;
487
494
488
- appendPQExpBuffer (buf , "CREATE GROUP %s WITH SYSID %s;\n" ,
489
- fmtId (PQgetvalue (res , i , 0 )),
490
- PQgetvalue (res , i , 1 ));
495
+ appendPQExpBuffer (buf , "CREATE GROUP %s;\n" ,
496
+ fmtId (PQgetvalue (res , i , 0 )));
491
497
492
- val = strdup (PQgetvalue (res , i , 2 ));
498
+ val = strdup (PQgetvalue (res , i , 1 ));
493
499
tok = strtok (val , ",{}" );
494
500
while (tok )
495
501
{
@@ -503,8 +509,10 @@ dumpGroups(PGconn *conn)
503
509
504
510
for (j = 0 ; j < PQntuples (res2 ); j ++ )
505
511
{
506
- appendPQExpBuffer (buf , "ALTER GROUP %s " , fmtId (PQgetvalue (res , i , 0 )));
507
- appendPQExpBuffer (buf , "ADD USER %s;\n" , fmtId (PQgetvalue (res2 , j , 0 )));
512
+ appendPQExpBuffer (buf , "ALTER GROUP %s " ,
513
+ fmtId (PQgetvalue (res , i , 0 )));
514
+ appendPQExpBuffer (buf , "ADD USER %s;\n" ,
515
+ fmtId (PQgetvalue (res2 , j , 0 )));
508
516
}
509
517
510
518
PQclear (res2 );
@@ -933,18 +941,21 @@ runPgDump(const char *dbname)
933
941
/*
934
942
* Make a database connection with the given parameters. An
935
943
* interactive password prompt is automatically issued if required.
944
+ *
945
+ * If fail_on_error is false, we return NULL without printing any message
946
+ * on failure, but preserve any prompted password for the next try.
936
947
*/
937
948
static PGconn *
938
949
connectDatabase (const char * dbname , const char * pghost , const char * pgport ,
939
- const char * pguser , bool require_password )
950
+ const char * pguser , bool require_password , bool fail_on_error )
940
951
{
941
952
PGconn * conn ;
942
- char * password = NULL ;
943
953
bool need_pass = false;
944
954
const char * remoteversion_str ;
945
955
int my_version ;
956
+ static char * password = NULL ;
946
957
947
- if (require_password )
958
+ if (require_password && ! password )
948
959
password = simple_prompt ("Password: " , 100 , false);
949
960
950
961
/*
@@ -969,21 +980,28 @@ connectDatabase(const char *dbname, const char *pghost, const char *pgport,
969
980
{
970
981
PQfinish (conn );
971
982
need_pass = true;
972
- free (password );
983
+ if (password )
984
+ free (password );
973
985
password = NULL ;
974
986
password = simple_prompt ("Password: " , 100 , false);
975
987
}
976
988
} while (need_pass );
977
989
978
- if (password )
979
- free (password );
980
-
981
990
/* check to see that the backend connection was successfully made */
982
991
if (PQstatus (conn ) == CONNECTION_BAD )
983
992
{
984
- fprintf (stderr , _ ("%s: could not connect to database \"%s\": %s\n" ),
985
- progname , dbname , PQerrorMessage (conn ));
986
- exit (1 );
993
+ if (fail_on_error )
994
+ {
995
+ fprintf (stderr ,
996
+ _ ("%s: could not connect to database \"%s\": %s\n" ),
997
+ progname , dbname , PQerrorMessage (conn ));
998
+ exit (1 );
999
+ }
1000
+ else
1001
+ {
1002
+ PQfinish (conn );
1003
+ return NULL ;
1004
+ }
987
1005
}
988
1006
989
1007
remoteversion_str = PQparameterStatus (conn , "server_version" );
0 commit comments