1 /*-------------------------------------------------------------------------
4 * pg_restore is an utility extracting postgres database definitions
5 * from a backup archive created by pg_dump using the archiver
8 * pg_restore will read the backup archive and
9 * dump out a script that reproduces
10 * the schema of the database in terms of
12 * user-defined functions
19 * the output script is SQL that is understood by PostgreSQL
21 * Basic process in a restore operation is:
23 * Open the Archive and read the TOC.
24 * Set flags in TOC entries, and *maybe* reorder them.
25 * Generate script to stdout
28 * Copyright (c) 2000, Philip Warner
29 * Rights are granted to use this software in any way so long
30 * as this notice is not removed.
32 * The author is not responsible for loss or damages that may
33 * result from its use.
37 * src/bin/pg_dump/pg_restore.c
39 *-------------------------------------------------------------------------
41 #include "postgres_fe.h"
43 #include "getopt_long.h"
45 #include "dumputils.h"
47 #include "pg_backup_utils.h"
60 static void usage(const char *progname);
62 typedef struct option optType;
65 main(int argc, char **argv)
73 static int disable_triggers = 0;
74 static int enable_row_security = 0;
75 static int if_exists = 0;
76 static int no_data_for_failed_tables = 0;
77 static int outputNoTablespaces = 0;
78 static int use_setsessauth = 0;
79 static int no_security_labels = 0;
80 static int strict_names = 0;
82 struct option cmdopts[] = {
83 {"clean", 0, NULL, 'c'},
84 {"create", 0, NULL, 'C'},
85 {"data-only", 0, NULL, 'a'},
86 {"dbname", 1, NULL, 'd'},
87 {"exit-on-error", 0, NULL, 'e'},
88 {"file", 1, NULL, 'f'},
89 {"format", 1, NULL, 'F'},
90 {"function", 1, NULL, 'P'},
91 {"host", 1, NULL, 'h'},
92 {"index", 1, NULL, 'I'},
93 {"jobs", 1, NULL, 'j'},
94 {"list", 0, NULL, 'l'},
95 {"no-privileges", 0, NULL, 'x'},
96 {"no-acl", 0, NULL, 'x'},
97 {"no-owner", 0, NULL, 'O'},
98 {"no-reconnect", 0, NULL, 'R'},
99 {"port", 1, NULL, 'p'},
100 {"no-password", 0, NULL, 'w'},
101 {"password", 0, NULL, 'W'},
102 {"schema", 1, NULL, 'n'},
103 {"schema-only", 0, NULL, 's'},
104 {"superuser", 1, NULL, 'S'},
105 {"table", 1, NULL, 't'},
106 {"trigger", 1, NULL, 'T'},
107 {"use-list", 1, NULL, 'L'},
108 {"username", 1, NULL, 'U'},
109 {"verbose", 0, NULL, 'v'},
110 {"single-transaction", 0, NULL, '1'},
113 * the following options don't have an equivalent short option letter
115 {"disable-triggers", no_argument, &disable_triggers, 1},
116 {"enable-row-security", no_argument, &enable_row_security, 1},
117 {"if-exists", no_argument, &if_exists, 1},
118 {"no-data-for-failed-tables", no_argument, &no_data_for_failed_tables, 1},
119 {"no-tablespaces", no_argument, &outputNoTablespaces, 1},
120 {"role", required_argument, NULL, 2},
121 {"section", required_argument, NULL, 3},
122 {"strict-names", no_argument, &strict_names, 1},
123 {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
124 {"no-security-labels", no_argument, &no_security_labels, 1},
129 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
131 init_parallel_dump_utils();
133 opts = NewRestoreOptions();
135 progname = get_progname(argv[0]);
139 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
144 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
146 puts("pg_restore (PostgreSQL) " PG_VERSION);
151 while ((c = getopt_long(argc, argv, "acCd:ef:F:h:I:j:lL:n:Op:P:RsS:t:T:U:vwWx1",
152 cmdopts, NULL)) != -1)
156 case 'a': /* Dump data only */
159 case 'c': /* clean (i.e., drop) schema prior to create */
160 opts->dropSchema = 1;
166 opts->dbname = pg_strdup(optarg);
169 opts->exit_on_error = true;
171 case 'f': /* output file name */
172 opts->filename = pg_strdup(optarg);
175 if (strlen(optarg) != 0)
176 opts->formatName = pg_strdup(optarg);
179 if (strlen(optarg) != 0)
180 opts->pghost = pg_strdup(optarg);
183 case 'j': /* number of restore jobs */
184 numWorkers = atoi(optarg);
187 case 'l': /* Dump the TOC summary */
188 opts->tocSummary = 1;
191 case 'L': /* input TOC summary file name */
192 opts->tocFile = pg_strdup(optarg);
195 case 'n': /* Dump data for this schema only */
196 simple_string_list_append(&opts->schemaNames, optarg);
204 if (strlen(optarg) != 0)
205 opts->pgport = pg_strdup(optarg);
208 /* no-op, still accepted for backwards compatibility */
210 case 'P': /* Function */
212 opts->selFunction = 1;
213 simple_string_list_append(&opts->functionNames, optarg);
215 case 'I': /* Index */
218 simple_string_list_append(&opts->indexNames, optarg);
220 case 'T': /* Trigger */
222 opts->selTrigger = 1;
223 simple_string_list_append(&opts->triggerNames, optarg);
225 case 's': /* dump schema only */
226 opts->schemaOnly = 1;
228 case 'S': /* Superuser username */
229 if (strlen(optarg) != 0)
230 opts->superuser = pg_strdup(optarg);
232 case 't': /* Dump specified table(s) only */
235 simple_string_list_append(&opts->tableNames, optarg);
239 opts->username = pg_strdup(optarg);
242 case 'v': /* verbose */
247 opts->promptPassword = TRI_NO;
251 opts->promptPassword = TRI_YES;
254 case 'x': /* skip ACL dump */
258 case '1': /* Restore data in a single transaction */
259 opts->single_txn = true;
260 opts->exit_on_error = true;
266 * This covers the long options without a short equivalent.
270 case 2: /* SET ROLE */
271 opts->use_role = pg_strdup(optarg);
274 case 3: /* section */
275 set_dump_section(optarg, &(opts->dumpSections));
279 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
284 /* Get file name from command line */
286 inputFileSpec = argv[optind++];
288 inputFileSpec = NULL;
290 /* Complain if any arguments remain */
293 fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
294 progname, argv[optind]);
295 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
300 /* Should get at most one of -d and -f, else user is confused */
305 fprintf(stderr, _("%s: options -d/--dbname and -f/--file cannot be used together\n"),
307 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
314 if (opts->dataOnly && opts->schemaOnly)
316 fprintf(stderr, _("%s: options -s/--schema-only and -a/--data-only cannot be used together\n"),
321 if (opts->dataOnly && opts->dropSchema)
323 fprintf(stderr, _("%s: options -c/--clean and -a/--data-only cannot be used together\n"),
328 /* Can't do single-txn mode with multiple connections */
329 if (opts->single_txn && numWorkers > 1)
331 fprintf(stderr, _("%s: cannot specify both --single-transaction and multiple jobs\n"),
336 opts->disable_triggers = disable_triggers;
337 opts->enable_row_security = enable_row_security;
338 opts->noDataForFailedTables = no_data_for_failed_tables;
339 opts->noTablespace = outputNoTablespaces;
340 opts->use_setsessauth = use_setsessauth;
341 opts->no_security_labels = no_security_labels;
343 if (if_exists && !opts->dropSchema)
345 fprintf(stderr, _("%s: option --if-exists requires option -c/--clean\n"),
349 opts->if_exists = if_exists;
350 opts->strict_names = strict_names;
352 if (opts->formatName)
354 switch (opts->formatName[0])
358 opts->format = archCustom;
363 opts->format = archDirectory;
368 opts->format = archTar;
372 write_msg(NULL, "unrecognized archive format \"%s\"; please specify \"c\", \"d\", or \"t\"\n",
378 AH = OpenArchive(inputFileSpec, opts->format);
380 SetArchiveOptions(AH, NULL, opts);
383 * We don't have a connection yet but that doesn't matter. The connection
384 * is initialized to NULL and if we terminate through exit_nicely() while
385 * it's still NULL, the cleanup function will just be a no-op.
387 on_exit_close_archive(AH);
389 /* Let the archiver know how noisy to be */
390 AH->verbose = opts->verbose;
393 * Whether to keep submitting sql commands as "pg_restore ... | psql ... "
395 AH->exit_on_error = opts->exit_on_error;
400 /* See comments in pg_dump.c */
402 if (numWorkers > MAXIMUM_WAIT_OBJECTS)
404 fprintf(stderr, _("%s: maximum number of parallel jobs is %d\n"),
405 progname, MAXIMUM_WAIT_OBJECTS);
410 AH->numWorkers = numWorkers;
412 if (opts->tocSummary)
416 ProcessArchiveRestoreOptions(AH);
420 /* done, print a summary of ignored errors */
422 fprintf(stderr, _("WARNING: errors ignored on restore: %d\n"),
425 /* AH may be freed in CloseArchive? */
426 exit_code = AH->n_errors ? 1 : 0;
434 usage(const char *progname)
436 printf(_("%s restores a PostgreSQL database from an archive created by pg_dump.\n\n"), progname);
437 printf(_("Usage:\n"));
438 printf(_(" %s [OPTION]... [FILE]\n"), progname);
440 printf(_("\nGeneral options:\n"));
441 printf(_(" -d, --dbname=NAME connect to database name\n"));
442 printf(_(" -f, --file=FILENAME output file name\n"));
443 printf(_(" -F, --format=c|d|t backup file format (should be automatic)\n"));
444 printf(_(" -l, --list print summarized TOC of the archive\n"));
445 printf(_(" -v, --verbose verbose mode\n"));
446 printf(_(" -V, --version output version information, then exit\n"));
447 printf(_(" -?, --help show this help, then exit\n"));
449 printf(_("\nOptions controlling the restore:\n"));
450 printf(_(" -a, --data-only restore only the data, no schema\n"));
451 printf(_(" -c, --clean clean (drop) database objects before recreating\n"));
452 printf(_(" -C, --create create the target database\n"));
453 printf(_(" -e, --exit-on-error exit on error, default is to continue\n"));
454 printf(_(" -I, --index=NAME restore named index\n"));
455 printf(_(" -j, --jobs=NUM use this many parallel jobs to restore\n"));
456 printf(_(" -L, --use-list=FILENAME use table of contents from this file for\n"
457 " selecting/ordering output\n"));
458 printf(_(" -n, --schema=NAME restore only objects in this schema\n"));
459 printf(_(" -O, --no-owner skip restoration of object ownership\n"));
460 printf(_(" -P, --function=NAME(args) restore named function\n"));
461 printf(_(" -s, --schema-only restore only the schema, no data\n"));
462 printf(_(" -S, --superuser=NAME superuser user name to use for disabling triggers\n"));
463 printf(_(" -t, --table=NAME restore named relation (table, view, etc)\n"));
464 printf(_(" -T, --trigger=NAME restore named trigger\n"));
465 printf(_(" -x, --no-privileges skip restoration of access privileges (grant/revoke)\n"));
466 printf(_(" -1, --single-transaction restore as a single transaction\n"));
467 printf(_(" --disable-triggers disable triggers during data-only restore\n"));
468 printf(_(" --enable-row-security enable row security\n"));
469 printf(_(" --if-exists use IF EXISTS when dropping objects\n"));
470 printf(_(" --no-data-for-failed-tables do not restore data of tables that could not be\n"
472 printf(_(" --no-security-labels do not restore security labels\n"));
473 printf(_(" --no-tablespaces do not restore tablespace assignments\n"));
474 printf(_(" --section=SECTION restore named section (pre-data, data, or post-data)\n"));
475 printf(_(" --strict-names require table and/or schema include patterns to\n"
476 " match at least one entity each\n"));
477 printf(_(" --use-set-session-authorization\n"
478 " use SET SESSION AUTHORIZATION commands instead of\n"
479 " ALTER OWNER commands to set ownership\n"));
481 printf(_("\nConnection options:\n"));
482 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
483 printf(_(" -p, --port=PORT database server port number\n"));
484 printf(_(" -U, --username=NAME connect as specified database user\n"));
485 printf(_(" -w, --no-password never prompt for password\n"));
486 printf(_(" -W, --password force password prompt (should happen automatically)\n"));
487 printf(_(" --role=ROLENAME do SET ROLE before restore\n"));
490 "The options -I, -n, -P, -t, -T, and --section can be combined and specified\n"
491 "multiple times to select multiple objects.\n"));
492 printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
493 printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));