@@ -52,6 +52,8 @@ static PGconn *connectDatabase(const char *dbname, const char *connstr, const ch
52
52
static char * constructConnStr (const char * * keywords , const char * * values );
53
53
static PGresult * executeQuery (PGconn * conn , const char * query );
54
54
static void executeCommand (PGconn * conn , const char * query );
55
+ static void expand_dbname_patterns (PGconn * conn , SimpleStringList * patterns ,
56
+ SimpleStringList * names );
55
57
56
58
static char pg_dump_bin [MAXPGPATH ];
57
59
static const char * progname ;
@@ -87,6 +89,9 @@ static char role_catalog[10];
87
89
static FILE * OPF ;
88
90
static char * filename = NULL ;
89
91
92
+ static SimpleStringList database_exclude_patterns = {NULL , NULL };
93
+ static SimpleStringList database_exclude_names = {NULL , NULL };
94
+
90
95
#define exit_nicely (code ) exit(code)
91
96
92
97
int
@@ -123,6 +128,7 @@ main(int argc, char *argv[])
123
128
{"column-inserts" , no_argument , & column_inserts , 1 },
124
129
{"disable-dollar-quoting" , no_argument , & disable_dollar_quoting , 1 },
125
130
{"disable-triggers" , no_argument , & disable_triggers , 1 },
131
+ {"exclude-database" , required_argument , NULL , 6 },
126
132
{"extra-float-digits" , required_argument , NULL , 5 },
127
133
{"if-exists" , no_argument , & if_exists , 1 },
128
134
{"inserts" , no_argument , & inserts , 1 },
@@ -324,6 +330,10 @@ main(int argc, char *argv[])
324
330
appendShellString (pgdumpopts , optarg );
325
331
break ;
326
332
333
+ case 6 :
334
+ simple_string_list_append (& database_exclude_patterns , optarg );
335
+ break ;
336
+
327
337
default :
328
338
fprintf (stderr , _ ("Try \"%s --help\" for more information.\n" ), progname );
329
339
exit_nicely (1 );
@@ -340,6 +350,16 @@ main(int argc, char *argv[])
340
350
exit_nicely (1 );
341
351
}
342
352
353
+ if (database_exclude_patterns .head != NULL &&
354
+ (globals_only || roles_only || tablespaces_only ))
355
+ {
356
+ fprintf (stderr , _ ("%s: option --exclude-database cannot be used together with -g/--globals-only, -r/--roles-only or -t/--tablespaces-only\n" ),
357
+ progname );
358
+ fprintf (stderr , _ ("Try \"%s --help\" for more information.\n" ),
359
+ progname );
360
+ exit_nicely (1 );
361
+ }
362
+
343
363
/* Make sure the user hasn't specified a mix of globals-only options */
344
364
if (globals_only && roles_only )
345
365
{
@@ -454,6 +474,12 @@ main(int argc, char *argv[])
454
474
}
455
475
}
456
476
477
+ /*
478
+ * Get a list of database names that match the exclude patterns
479
+ */
480
+ expand_dbname_patterns (conn , & database_exclude_patterns ,
481
+ & database_exclude_names );
482
+
457
483
/*
458
484
* Open the output file if required, otherwise use stdout
459
485
*/
@@ -620,6 +646,7 @@ help(void)
620
646
printf (_ (" --column-inserts dump data as INSERT commands with column names\n" ));
621
647
printf (_ (" --disable-dollar-quoting disable dollar quoting, use SQL standard quoting\n" ));
622
648
printf (_ (" --disable-triggers disable triggers during data-only restore\n" ));
649
+ printf (_ (" --exclude-database=PATTERN exclude databases whose name matches PATTERN\n" ));
623
650
printf (_ (" --extra-float-digits=NUM override default setting for extra_float_digits\n" ));
624
651
printf (_ (" --if-exists use IF EXISTS when dropping objects\n" ));
625
652
printf (_ (" --inserts dump data as INSERT commands, rather than COPY\n" ));
@@ -1358,6 +1385,48 @@ dumpUserConfig(PGconn *conn, const char *username)
1358
1385
destroyPQExpBuffer (buf );
1359
1386
}
1360
1387
1388
+ /*
1389
+ * Find a list of database names that match the given patterns.
1390
+ * See also expand_table_name_patterns() in pg_dump.c
1391
+ */
1392
+ static void
1393
+ expand_dbname_patterns (PGconn * conn ,
1394
+ SimpleStringList * patterns ,
1395
+ SimpleStringList * names )
1396
+ {
1397
+ PQExpBuffer query ;
1398
+ PGresult * res ;
1399
+
1400
+ if (patterns -> head == NULL )
1401
+ return ; /* nothing to do */
1402
+
1403
+ query = createPQExpBuffer ();
1404
+
1405
+ /*
1406
+ * The loop below runs multiple SELECTs, which might sometimes result in
1407
+ * duplicate entries in the name list, but we don't care, since all
1408
+ * we're going to do is test membership of the list.
1409
+ */
1410
+
1411
+ for (SimpleStringListCell * cell = patterns -> head ; cell ; cell = cell -> next )
1412
+ {
1413
+ appendPQExpBuffer (query ,
1414
+ "SELECT datname FROM pg_catalog.pg_database n\n" );
1415
+ processSQLNamePattern (conn , query , cell -> val , false,
1416
+ false, NULL , "datname" , NULL , NULL );
1417
+
1418
+ res = executeQuery (conn , query -> data );
1419
+ for (int i = 0 ; i < PQntuples (res ); i ++ )
1420
+ {
1421
+ simple_string_list_append (names , PQgetvalue (res , i , 0 ));
1422
+ }
1423
+
1424
+ PQclear (res );
1425
+ resetPQExpBuffer (query );
1426
+ }
1427
+
1428
+ destroyPQExpBuffer (query );
1429
+ }
1361
1430
1362
1431
/*
1363
1432
* Dump contents of databases.
@@ -1395,6 +1464,15 @@ dumpDatabases(PGconn *conn)
1395
1464
if (strcmp (dbname , "template0" ) == 0 )
1396
1465
continue ;
1397
1466
1467
+ /* Skip any explicitly excluded database */
1468
+ if (simple_string_list_member (& database_exclude_names , dbname ))
1469
+ {
1470
+ if (verbose )
1471
+ fprintf (stderr , _ ("%s: excluding database \"%s\"...\n" ),
1472
+ progname , dbname );
1473
+ continue ;
1474
+ }
1475
+
1398
1476
if (verbose )
1399
1477
fprintf (stderr , _ ("%s: dumping database \"%s\"...\n" ), progname , dbname );
1400
1478
0 commit comments