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.44 2004/07/12 14:35:45 momjian Exp $
9
+ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.45 2004/07/19 21:39:48 momjian Exp $
10
10
*
11
11
*-------------------------------------------------------------------------
12
12
*/
@@ -44,7 +44,7 @@ static const char *progname;
44
44
45
45
static void help (void );
46
46
47
- static void dumpUsers (PGconn * conn );
47
+ static void dumpUsers (PGconn * conn , bool initdbonly );
48
48
static void dumpGroups (PGconn * conn );
49
49
static void dumpTablespaces (PGconn * conn );
50
50
static void dumpCreateDB (PGconn * conn );
@@ -257,7 +257,7 @@ main(int argc, char *argv[])
257
257
if (disable_triggers )
258
258
appendPQExpBuffer (pgdumpopts , " -X disable-triggers" );
259
259
if (use_setsessauth )
260
- /* no-op, still allowed for compatibility */ ;
260
+ appendPQExpBuffer ( pgdumpopts , " -X use-set-session-authorization" ) ;
261
261
262
262
if (optind < argc )
263
263
{
@@ -279,18 +279,19 @@ main(int argc, char *argv[])
279
279
280
280
if (!data_only )
281
281
{
282
- dumpUsers (conn );
282
+ /* Dump all users excluding the initdb user */
283
+ dumpUsers (conn , false);
283
284
dumpGroups (conn );
284
285
if (server_version >= 70500 )
285
286
dumpTablespaces (conn );
287
+ if (!globals_only )
288
+ dumpCreateDB (conn );
289
+ /* Dump alter command for initdb user */
290
+ dumpUsers (conn , true);
286
291
}
287
292
288
293
if (!globals_only )
289
- {
290
- if (!data_only )
291
- dumpCreateDB (conn );
292
294
dumpDatabases (conn );
293
- }
294
295
295
296
PQfinish (conn );
296
297
@@ -310,26 +311,29 @@ help(void)
310
311
printf (_ ("Usage:\n" ));
311
312
printf (_ (" %s [OPTION]...\n" ), progname );
312
313
313
- printf (_ ("\nOptions:\n" ));
314
+ printf (_ ("\nGeneral options:\n" ));
315
+ printf (_ (" -i, --ignore-version proceed even when server version mismatches\n"
316
+ " pg_dumpall version\n" ));
317
+ printf (_ (" --help show this help, then exit\n" ));
318
+ printf (_ (" --version output version information, then exit\n" ));
319
+ printf (_ ("\nOptions controlling the output content:\n" ));
314
320
printf (_ (" -a, --data-only dump only the data, not the schema\n" ));
315
321
printf (_ (" -c, --clean clean (drop) databases prior to create\n" ));
316
322
printf (_ (" -d, --inserts dump data as INSERT, rather than COPY, commands\n" ));
317
323
printf (_ (" -D, --column-inserts dump data as INSERT commands with column names\n" ));
318
324
printf (_ (" -g, --globals-only dump only global objects, no databases\n" ));
319
- printf (_ (" -i, --ignore-version proceed even when server version mismatches\n"
320
- " pg_dumpall version\n" ));
321
- printf (_ (" -s, --schema-only dump only the schema, no data\n" ));
322
- printf (_ (" -S, --superuser=NAME specify the superuser user name to use in the dump\n" ));
323
325
printf (_ (" -o, --oids include OIDs in dump\n" ));
324
326
printf (_ (" -O, --no-owner do not output commands to set object ownership\n" ));
325
- printf (_ (" -v, --verbose verbose mode\n" ));
327
+ printf (_ (" -s, --schema-only dump only the schema, no data\n" ));
328
+ printf (_ (" -S, --superuser=NAME specify the superuser user name to use in the dump\n" ));
326
329
printf (_ (" -x, --no-privileges do not dump privileges (grant/revoke)\n" ));
327
330
printf (_ (" -X disable-dollar-quoting, --disable-dollar-quoting\n"
328
331
" disable dollar quoting, use SQL standard quoting\n" ));
329
332
printf (_ (" -X disable-triggers, --disable-triggers\n"
330
- " disable triggers during data-only restore\n" ));
331
- printf (_ (" --help show this help, then exit\n" ));
332
- printf (_ (" --version output version information, then exit\n" ));
333
+ " disable triggers during data-only restore\n" ));
334
+ printf (_ (" -X use-set-session-authorization, --use-set-session-authorization\n"
335
+ " use SESSION AUTHORIZATION commands instead of\n"
336
+ " OWNER TO commands\n" ));
333
337
334
338
printf (_ ("\nConnection options:\n" ));
335
339
printf (_ (" -h, --host=HOSTNAME database server host or socket directory\n" ));
@@ -344,39 +348,52 @@ help(void)
344
348
345
349
346
350
/*
347
- * Dump users (but not the user created by initdb).
351
+ * Dump users
352
+ * Is able to dump all non initdb users or just the initdb user.
348
353
*/
349
354
static void
350
- dumpUsers (PGconn * conn )
355
+ dumpUsers (PGconn * conn , bool initdbonly )
351
356
{
352
357
PGresult * res ;
353
358
int i ;
354
359
355
- printf ("--\n-- Users\n--\n\n" );
356
- printf ("DELETE FROM pg_shadow WHERE usesysid <> (SELECT datdba FROM pg_database WHERE datname = 'template0');\n\n" );
357
-
358
360
if (server_version >= 70100 )
359
361
res = executeQuery (conn ,
360
362
"SELECT usename, usesysid, passwd, usecreatedb, "
361
- "usesuper, valuntil "
362
- "FROM pg_shadow "
363
- "WHERE usesysid <> (SELECT datdba FROM pg_database WHERE datname = 'template0') " );
363
+ "usesuper, valuntil, "
364
+ "(usesysid = (SELECT datdba FROM pg_database WHERE datname = 'template0')) AS clusterowner "
365
+ "FROM pg_shadow " );
364
366
else
365
367
res = executeQuery (conn ,
366
368
"SELECT usename, usesysid, passwd, usecreatedb, "
367
- "usesuper, valuntil "
368
- "FROM pg_shadow "
369
- "WHERE usesysid <> (SELECT datdba FROM pg_database WHERE datname = 'template1')" );
369
+ "usesuper, valuntil, "
370
+ "(usesysid = (SELECT datdba FROM pg_database WHERE datname = 'template1')) AS clusterowner "
371
+ "FROM pg_shadow" );
372
+
373
+ if (PQntuples (res ) > 0 || (!initdbonly && output_clean ))
374
+ printf ("--\n-- Users\n--\n\n" );
375
+ if (!initdbonly && output_clean )
376
+ printf ("DELETE FROM pg_shadow WHERE usesysid <> (SELECT datdba FROM pg_database WHERE datname = 'template0');\n\n" );
370
377
371
378
for (i = 0 ; i < PQntuples (res ); i ++ )
372
379
{
373
- PQExpBuffer buf = createPQExpBuffer ();
374
380
const char * username ;
375
-
381
+ bool clusterowner ;
382
+ PQExpBuffer buf = createPQExpBuffer ();
376
383
username = PQgetvalue (res , i , 0 );
377
- appendPQExpBuffer (buf , "CREATE USER %s WITH SYSID %s" ,
378
- fmtId (username ),
379
- PQgetvalue (res , i , 1 ));
384
+ clusterowner = (strcmp (PQgetvalue (res , i , 6 ), "t" ) == 0 );
385
+
386
+ /* Check which pass we're on */
387
+ if ((initdbonly && !clusterowner ) || (!initdbonly && clusterowner )) continue ;
388
+
389
+ /* Dump ALTER USER for the cluster owner and CREATE USER for all other users */
390
+ if (!clusterowner )
391
+ appendPQExpBuffer (buf , "CREATE USER %s WITH SYSID %s" ,
392
+ fmtId (username ),
393
+ PQgetvalue (res , i , 1 ));
394
+ else
395
+ appendPQExpBuffer (buf , "ALTER USER %s WITH" ,
396
+ fmtId (username ));
380
397
381
398
if (!PQgetisnull (res , i , 2 ))
382
399
{
@@ -422,11 +439,13 @@ dumpGroups(PGconn *conn)
422
439
PGresult * res ;
423
440
int i ;
424
441
425
- printf ("--\n-- Groups\n--\n\n" );
426
- printf ("DELETE FROM pg_group;\n\n" );
427
-
428
442
res = executeQuery (conn , "SELECT groname, grosysid, grolist FROM pg_group" );
429
443
444
+ if (PQntuples (res ) > 0 || output_clean )
445
+ printf ("--\n-- Groups\n--\n\n" );
446
+ if (output_clean )
447
+ printf ("DELETE FROM pg_group;\n\n" );
448
+
430
449
for (i = 0 ; i < PQntuples (res ); i ++ )
431
450
{
432
451
PQExpBuffer buf = createPQExpBuffer ();
@@ -478,8 +497,6 @@ dumpTablespaces(PGconn *conn)
478
497
PGresult * res ;
479
498
int i ;
480
499
481
- printf ("--\n-- Tablespaces\n--\n\n" );
482
-
483
500
/*
484
501
* Get all tablespaces except built-in ones (which we assume are named
485
502
* pg_xxx)
@@ -489,6 +506,9 @@ dumpTablespaces(PGconn *conn)
489
506
"spclocation, spcacl "
490
507
"FROM pg_catalog.pg_tablespace "
491
508
"WHERE spcname NOT LIKE 'pg\\_%'" );
509
+
510
+ if (PQntuples (res ) > 0 )
511
+ printf ("--\n-- Tablespaces\n--\n\n" );
492
512
493
513
for (i = 0 ; i < PQntuples (res ); i ++ )
494
514
{
@@ -758,7 +778,12 @@ makeAlterConfigCommand(const char *arrayitem, const char *type, const char *name
758
778
* pos = 0 ;
759
779
appendPQExpBuffer (buf , "ALTER %s %s " , type , fmtId (name ));
760
780
appendPQExpBuffer (buf , "SET %s TO " , fmtId (mine ));
761
- appendStringLiteral (buf , pos + 1 , false);
781
+ /* Some GUC variable names are 'LIST' type and hence must not be quoted. */
782
+ if (strcasecmp (mine , "DateStyle" ) == 0
783
+ || strcasecmp (mine , "search_path" ) == 0 )
784
+ appendPQExpBuffer (buf , "%s" , pos + 1 );
785
+ else
786
+ appendStringLiteral (buf , pos + 1 , false);
762
787
appendPQExpBuffer (buf , ";\n" );
763
788
764
789
printf ("%s" , buf -> data );
0 commit comments