60{
63 int exit_code;
64 int numWorkers = 1;
66 char *inputFileSpec;
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;
87
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'},
118
119 /*
120 * the following options don't have an equivalent short option letter
121 */
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},
145
146 {NULL, 0, NULL, 0}
147 };
148
152
154
156
158
159 if (argc > 1)
160 {
161 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
162 {
165 }
166 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
167 {
168 puts("pg_restore (PostgreSQL) " PG_VERSION);
170 }
171 }
172
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)
175 {
177 {
178 case 'a': /* Dump data only */
179 data_only = true;
180 break;
181 case 'c': /* clean (i.e., drop) schema prior to create */
182 opts->dropSchema = 1;
183 break;
184 case 'C':
186 break;
187 case 'd':
189 break;
190 case 'e':
191 opts->exit_on_error =
true;
192 break;
193 case 'f': /* output file name */
195 break;
196 case 'F':
199 break;
200 case 'h':
203 break;
204
205 case 'j': /* number of restore jobs */
208 &numWorkers))
209 exit(1);
210 break;
211
212 case 'l': /* Dump the TOC summary */
213 opts->tocSummary = 1;
214 break;
215
216 case 'L': /* input TOC summary file name */
218 break;
219
220 case 'n': /* Dump data for this schema only */
222 break;
223
224 case 'N': /* Do not dump data for this schema */
226 break;
227
228 case 'O':
230 break;
231
232 case 'p':
235 break;
236 case 'R':
237 /* no-op, still accepted for backwards compatibility */
238 break;
239 case 'P': /* Function */
241 opts->selFunction = 1;
243 break;
244 case 'I': /* Index */
248 break;
249 case 'T': /* Trigger */
251 opts->selTrigger = 1;
253 break;
254 case 's': /* dump schema only */
255 schema_only = true;
256 break;
257 case 'S': /* Superuser username */
260 break;
261 case 't': /* Dump specified table(s) only */
265 break;
266
267 case 'U':
269 break;
270
271 case 'v': /* verbose */
274 break;
275
276 case 'w':
278 break;
279
280 case 'W':
282 break;
283
284 case 'x': /* skip ACL dump */
286 break;
287
288 case '1': /* Restore data in a single transaction */
289 opts->single_txn =
true;
290 opts->exit_on_error =
true;
291 break;
292
293 case 0:
294
295 /*
296 * This covers the long options without a short equivalent.
297 */
298 break;
299
300 case 2: /* SET ROLE */
302 break;
303
304 case 3: /* section */
306 break;
307
308 case 4: /* filter */
310 break;
311
312 case 5: /* transaction-size */
314 1, INT_MAX,
316 exit(1);
317 opts->exit_on_error =
true;
318 break;
319
320 case 6:
322 break;
323
324 default:
325 /* getopt_long already emitted a complaint */
328 }
329 }
330
331 /* Get file name from command line */
333 inputFileSpec = argv[
optind++];
334 else
335 inputFileSpec = NULL;
336
337 /* Complain if any arguments remain */
339 {
340 pg_log_error(
"too many command-line arguments (first is \"%s\")",
344 }
345
346 /* Complain if neither -f nor -d was specified (except if dumping TOC) */
347 if (!
opts->cparams.dbname && !
opts->filename && !
opts->tocSummary)
348 pg_fatal(
"one of -d/--dbname and -f/--file must be specified");
349
350 /* Should get at most one of -d and -f, else user is confused */
351 if (
opts->cparams.dbname)
352 {
354 {
355 pg_log_error(
"options -d/--dbname and -f/--file cannot be used together");
358 }
359
360 if (
opts->restrict_key)
361 pg_fatal(
"options -d/--dbname and --restrict-key cannot be used together");
362
364 }
365 else
366 {
367 /*
368 * If you don't provide a restrict key, one will be appointed for you.
369 */
370 if (!
opts->restrict_key)
372 if (!
opts->restrict_key)
373 pg_fatal(
"could not generate restrict key");
376 }
377
378 /* reject conflicting "-only" options */
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");
385
386 /* reject conflicting "-only" and "no-" options */
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");
393
394 /* reject conflicting "no-" options */
396 pg_fatal(
"options --statistics and --no-statistics cannot be used together");
397
398 /* reject conflicting "only-" options */
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");
405
406 if (data_only &&
opts->dropSchema)
407 pg_fatal(
"options -c/--clean and -a/--data-only cannot be used together");
408
409 if (
opts->single_txn &&
opts->txn_size > 0)
410 pg_fatal(
"options -1/--single-transaction and --transaction-size cannot be used together");
411
412 /*
413 * -C is not compatible with -1, because we can't create a database inside
414 * a transaction block.
415 */
416 if (
opts->createDB &&
opts->single_txn)
417 pg_fatal(
"options -C/--create and -1/--single-transaction cannot be used together");
418
419 /* Can't do single-txn mode with multiple connections */
420 if (
opts->single_txn && numWorkers > 1)
421 pg_fatal(
"cannot specify both --single-transaction and multiple jobs");
422
423 /*
424 * Set derivative flags. Ambiguous or nonsensical combinations, e.g.
425 * "--schema-only --no-schema", will have already caused an error in one
426 * of the checks above.
427 */
432 opts->dumpStatistics = ((
opts->dumpStatistics && !schema_only && !data_only) ||
434
436 opts->enable_row_security = enable_row_security;
437 opts->noDataForFailedTables = no_data_for_failed_tables;
438 opts->noTableAm = outputNoTableAm;
439 opts->noTablespace = outputNoTablespaces;
446
448 pg_fatal(
"option --if-exists requires option -c/--clean");
451
452 if (
opts->formatName)
453 {
465 {
466 /* recognize this for consistency with pg_dump */
467 pg_fatal(
"archive format \"%s\" is not supported; please use psql",
469 }
470 else
471 pg_fatal(
"unrecognized archive format \"%s\"; please specify \"c\", \"d\", or \"t\"",
473 }
474
476
478
479 /*
480 * We don't have a connection yet but that doesn't matter. The connection
481 * is initialized to NULL and if we terminate through exit_nicely() while
482 * it's still NULL, the cleanup function will just be a no-op.
483 */
485
486 /* Let the archiver know how noisy to be */
488
489 /*
490 * Whether to keep submitting sql commands as "pg_restore ... | psql ... "
491 */
493
496
498
499 if (
opts->tocSummary)
501 else
502 {
505 }
506
507 /* done, print a summary of ignored errors */
510
511 /* AH may be freed in CloseArchive? */
513
515
516 return exit_code;
517}
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)
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
#define required_argument
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)
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)