67 bool data_only =
false;
68 bool schema_only =
false;
70 static int enable_row_security = 0;
72 static int no_data_for_failed_tables = 0;
73 static int outputNoTableAm = 0;
74 static int outputNoTablespaces = 0;
88 struct option cmdopts[] = {
89 {
"clean", 0, NULL,
'c'},
90 {
"create", 0, NULL,
'C'},
91 {
"data-only", 0, NULL,
'a'},
92 {
"dbname", 1, NULL,
'd'},
93 {
"exit-on-error", 0, NULL,
'e'},
94 {
"exclude-schema", 1, NULL,
'N'},
95 {
"file", 1, NULL,
'f'},
96 {
"format", 1, NULL,
'F'},
97 {
"function", 1, NULL,
'P'},
98 {
"host", 1, NULL,
'h'},
99 {
"index", 1, NULL,
'I'},
100 {
"jobs", 1, NULL,
'j'},
101 {
"list", 0, NULL,
'l'},
102 {
"no-privileges", 0, NULL,
'x'},
103 {
"no-acl", 0, NULL,
'x'},
104 {
"no-owner", 0, NULL,
'O'},
105 {
"no-reconnect", 0, NULL,
'R'},
106 {
"port", 1, NULL,
'p'},
107 {
"no-password", 0, NULL,
'w'},
108 {
"password", 0, NULL,
'W'},
109 {
"schema", 1, NULL,
'n'},
110 {
"schema-only", 0, NULL,
's'},
111 {
"superuser", 1, NULL,
'S'},
112 {
"table", 1, NULL,
't'},
113 {
"trigger", 1, NULL,
'T'},
114 {
"use-list", 1, NULL,
'L'},
115 {
"username", 1, NULL,
'U'},
116 {
"verbose", 0, NULL,
'v'},
117 {
"single-transaction", 0, NULL,
'1'},
123 {
"enable-row-security",
no_argument, &enable_row_security, 1},
125 {
"no-data-for-failed-tables",
no_argument, &no_data_for_failed_tables, 1},
126 {
"no-table-access-method",
no_argument, &outputNoTableAm, 1},
127 {
"no-tablespaces",
no_argument, &outputNoTablespaces, 1},
161 if (strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-?") == 0)
166 if (strcmp(argv[1],
"--version") == 0 || strcmp(argv[1],
"-V") == 0)
168 puts(
"pg_restore (PostgreSQL) " PG_VERSION);
173 while ((
c =
getopt_long(argc, argv,
"acCd:ef:F:h:I:j:lL:n:N:Op:P:RsS:t:T:U:vwWx1",
174 cmdopts, NULL)) != -1)
182 opts->dropSchema = 1;
191 opts->exit_on_error =
true;
213 opts->tocSummary = 1;
241 opts->selFunction = 1;
251 opts->selTrigger = 1;
289 opts->single_txn =
true;
290 opts->exit_on_error =
true;
317 opts->exit_on_error =
true;
333 inputFileSpec = argv[
optind++];
335 inputFileSpec = NULL;
340 pg_log_error(
"too many command-line arguments (first is \"%s\")",
347 if (!
opts->cparams.dbname && !
opts->filename && !
opts->tocSummary)
348 pg_fatal(
"one of -d/--dbname and -f/--file must be specified");
351 if (
opts->cparams.dbname)
355 pg_log_error(
"options -d/--dbname and -f/--file cannot be used together");
360 if (
opts->restrict_key)
361 pg_fatal(
"options -d/--dbname and --restrict-key cannot be used together");
370 if (!
opts->restrict_key)
372 if (!
opts->restrict_key)
373 pg_fatal(
"could not generate restrict key");
379 if (data_only && schema_only)
380 pg_fatal(
"options -s/--schema-only and -a/--data-only cannot be used together");
382 pg_fatal(
"options -s/--schema-only and --statistics-only cannot be used together");
384 pg_fatal(
"options -a/--data-only and --statistics-only cannot be used together");
388 pg_fatal(
"options -a/--data-only and --no-data cannot be used together");
390 pg_fatal(
"options -s/--schema-only and --no-schema cannot be used together");
392 pg_fatal(
"options --statistics-only and --no-statistics cannot be used together");
396 pg_fatal(
"options --statistics and --no-statistics cannot be used together");
400 pg_fatal(
"options %s and %s cannot be used together",
401 "-a/--data-only",
"--statistics");
403 pg_fatal(
"options %s and %s cannot be used together",
404 "-s/--schema-only",
"--statistics");
406 if (data_only &&
opts->dropSchema)
407 pg_fatal(
"options -c/--clean and -a/--data-only cannot be used together");
409 if (
opts->single_txn &&
opts->txn_size > 0)
410 pg_fatal(
"options -1/--single-transaction and --transaction-size cannot be used together");
416 if (
opts->createDB &&
opts->single_txn)
417 pg_fatal(
"options -C/--create and -1/--single-transaction cannot be used together");
420 if (
opts->single_txn && numWorkers > 1)
421 pg_fatal(
"cannot specify both --single-transaction and multiple jobs");
432 opts->dumpStatistics = ((
opts->dumpStatistics && !schema_only && !data_only) ||
436 opts->enable_row_security = enable_row_security;
437 opts->noDataForFailedTables = no_data_for_failed_tables;
438 opts->noTableAm = outputNoTableAm;
439 opts->noTablespace = outputNoTablespaces;
448 pg_fatal(
"option --if-exists requires option -c/--clean");
452 if (
opts->formatName)
467 pg_fatal(
"archive format \"%s\" is not supported; please use psql",
471 pg_fatal(
"unrecognized archive format \"%s\"; please specify \"c\", \"d\", or \"t\"",
499 if (
opts->tocSummary)
522 printf(
_(
"%s restores a PostgreSQL database from an archive created by pg_dump.\n\n"),
progname);
526 printf(
_(
"\nGeneral options:\n"));
527 printf(
_(
" -d, --dbname=NAME connect to database name\n"));
528 printf(
_(
" -f, --file=FILENAME output file name (- for stdout)\n"));
529 printf(
_(
" -F, --format=c|d|t backup file format (should be automatic)\n"));
530 printf(
_(
" -l, --list print summarized TOC of the archive\n"));
531 printf(
_(
" -v, --verbose verbose mode\n"));
532 printf(
_(
" -V, --version output version information, then exit\n"));
533 printf(
_(
" -?, --help show this help, then exit\n"));
535 printf(
_(
"\nOptions controlling the restore:\n"));
536 printf(
_(
" -a, --data-only restore only the data, no schema\n"));
537 printf(
_(
" -c, --clean clean (drop) database objects before recreating\n"));
538 printf(
_(
" -C, --create create the target database\n"));
539 printf(
_(
" -e, --exit-on-error exit on error, default is to continue\n"));
540 printf(
_(
" -I, --index=NAME restore named index\n"));
541 printf(
_(
" -j, --jobs=NUM use this many parallel jobs to restore\n"));
542 printf(
_(
" -L, --use-list=FILENAME use table of contents from this file for\n"
543 " selecting/ordering output\n"));
544 printf(
_(
" -n, --schema=NAME restore only objects in this schema\n"));
545 printf(
_(
" -N, --exclude-schema=NAME do not restore objects in this schema\n"));
546 printf(
_(
" -O, --no-owner skip restoration of object ownership\n"));
547 printf(
_(
" -P, --function=NAME(args) restore named function\n"));
548 printf(
_(
" -s, --schema-only restore only the schema, no data\n"));
549 printf(
_(
" -S, --superuser=NAME superuser user name to use for disabling triggers\n"));
550 printf(
_(
" -t, --table=NAME restore named relation (table, view, etc.)\n"));
551 printf(
_(
" -T, --trigger=NAME restore named trigger\n"));
552 printf(
_(
" -x, --no-privileges skip restoration of access privileges (grant/revoke)\n"));
553 printf(
_(
" -1, --single-transaction restore as a single transaction\n"));
554 printf(
_(
" --disable-triggers disable triggers during data-only restore\n"));
555 printf(
_(
" --enable-row-security enable row security\n"));
556 printf(
_(
" --filter=FILENAME restore or skip objects based on expressions\n"
558 printf(
_(
" --if-exists use IF EXISTS when dropping objects\n"));
559 printf(
_(
" --no-comments do not restore comment commands\n"));
560 printf(
_(
" --no-data do not restore data\n"));
561 printf(
_(
" --no-data-for-failed-tables do not restore data of tables that could not be\n"
563 printf(
_(
" --no-policies do not restore row security policies\n"));
564 printf(
_(
" --no-publications do not restore publications\n"));
565 printf(
_(
" --no-schema do not restore schema\n"));
566 printf(
_(
" --no-security-labels do not restore security labels\n"));
567 printf(
_(
" --no-statistics do not restore statistics\n"));
568 printf(
_(
" --no-subscriptions do not restore subscriptions\n"));
569 printf(
_(
" --no-table-access-method do not restore table access methods\n"));
570 printf(
_(
" --no-tablespaces do not restore tablespace assignments\n"));
571 printf(
_(
" --restrict-key=RESTRICT_KEY use provided string as psql \\restrict key\n"));
572 printf(
_(
" --section=SECTION restore named section (pre-data, data, or post-data)\n"));
573 printf(
_(
" --statistics restore the statistics\n"));
574 printf(
_(
" --statistics-only restore only the statistics, not schema or data\n"));
575 printf(
_(
" --strict-names require table and/or schema include patterns to\n"
576 " match at least one entity each\n"));
577 printf(
_(
" --transaction-size=N commit after every N objects\n"));
578 printf(
_(
" --use-set-session-authorization\n"
579 " use SET SESSION AUTHORIZATION commands instead of\n"
580 " ALTER OWNER commands to set ownership\n"));
582 printf(
_(
"\nConnection options:\n"));
583 printf(
_(
" -h, --host=HOSTNAME database server host or socket directory\n"));
584 printf(
_(
" -p, --port=PORT database server port number\n"));
585 printf(
_(
" -U, --username=NAME connect as specified database user\n"));
586 printf(
_(
" -w, --no-password never prompt for password\n"));
587 printf(
_(
" -W, --password force password prompt (should happen automatically)\n"));
588 printf(
_(
" --role=ROLENAME do SET ROLE before restore\n"));
591 "The options -I, -n, -N, -P, -t, -T, and --section can be combined and specified\n"
592 "multiple times to select multiple objects.\n"));
593 printf(
_(
"\nIf no input file name is supplied, then standard input is used.\n\n"));
594 printf(
_(
"Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
595 printf(
_(
"%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
636 opts->selFunction = 1;
654 opts->selTrigger = 1;
void on_exit_close_archive(Archive *AHX)
void init_parallel_dump_utils(void)
#define PG_TEXTDOMAIN(domain)
void set_pglocale_pgservice(const char *argv0, const char *app)
char * generate_restrict_key(void)
bool valid_restrict_key(const char *restrict_key)
char * pg_strdup(const char *in)
void filter_init(FilterStateData *fstate, const char *filename, exit_function f_exit)
void filter_free(FilterStateData *fstate)
const char * filter_object_type_name(FilterObjectType fot)
bool filter_read_item(FilterStateData *fstate, char **objname, FilterCommandType *comtype, FilterObjectType *objtype)
void pg_log_filter_error(FilterStateData *fstate, const char *fmt,...)
@ FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN
@ FILTER_OBJECT_TYPE_SCHEMA
@ FILTER_OBJECT_TYPE_INDEX
@ FILTER_OBJECT_TYPE_TRIGGER
@ FILTER_OBJECT_TYPE_FOREIGN_DATA
@ FILTER_OBJECT_TYPE_DATABASE
@ FILTER_OBJECT_TYPE_FUNCTION
@ FILTER_OBJECT_TYPE_TABLE_DATA
@ FILTER_OBJECT_TYPE_NONE
@ FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN
@ FILTER_OBJECT_TYPE_EXTENSION
@ FILTER_OBJECT_TYPE_TABLE
@ FILTER_COMMAND_TYPE_NONE
@ FILTER_COMMAND_TYPE_EXCLUDE
@ FILTER_COMMAND_TYPE_INCLUDE
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
#define required_argument
Assert(PointerIsAligned(start, uint64))
void pg_logging_increase_verbosity(void)
void pg_logging_init(const char *argv0)
void pg_logging_set_level(enum pg_log_level new_level)
#define pg_log_error(...)
#define pg_log_error_hint(...)
bool option_parse_int(const char *optarg, const char *optname, int min_range, int max_range, int *result)
static AmcheckOptions opts
void ProcessArchiveRestoreOptions(Archive *AHX)
RestoreOptions * NewRestoreOptions(void)
Archive * OpenArchive(const char *FileSpec, const ArchiveFormat fmt)
void CloseArchive(Archive *AHX)
void SortTocFromFile(Archive *AHX)
void PrintTOCSummary(Archive *AHX)
void SetArchiveOptions(Archive *AH, DumpOptions *dopt, RestoreOptions *ropt)
void RestoreArchive(Archive *AHX)
void exit_nicely(int code)
void set_dump_section(const char *arg, int *dumpSections)
static int statistics_only
static int disable_triggers
static int no_publications
static int no_security_labels
static int use_setsessauth
static int no_subscriptions
static int with_statistics
PGDLLIMPORT char * optarg
static void usage(const char *progname)
int main(int argc, char **argv)
static void read_restore_filters(const char *filename, RestoreOptions *opts)
#define pg_log_warning(...)
int pg_strcasecmp(const char *s1, const char *s2)
const char * get_progname(const char *argv0)
void simple_string_list_append(SimpleStringList *list, const char *val)