PostgreSQL Source Code  git master
pg_restore.c File Reference
#include "postgres_fe.h"
#include <ctype.h>
#include "dumputils.h"
#include "fe_utils/option_utils.h"
#include "getopt_long.h"
#include "parallel.h"
#include "pg_backup_utils.h"
Include dependency graph for pg_restore.c:

Go to the source code of this file.

Functions

static void usage (const char *progname)
 
int main (int argc, char **argv)
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 57 of file pg_restore.c.

References _, _restoreOptions::aclsSkip, archCustom, archDirectory, archTar, CloseArchive(), _restoreOptions::cparams, _restoreOptions::createDB, _restoreOptions::dataOnly, _connParams::dbname, disable_triggers, _restoreOptions::disable_triggers, _restoreOptions::dropSchema, _restoreOptions::dumpSections, _restoreOptions::enable_row_security, exit_nicely, _restoreOptions::exit_on_error, Archive::exit_on_error, _restoreOptions::filename, _restoreOptions::format, _restoreOptions::formatName, fprintf, _restoreOptions::functionNames, get_progname(), getopt_long(), if_exists, _restoreOptions::if_exists, _restoreOptions::indexNames, init_parallel_dump_utils(), Archive::n_errors, NewRestoreOptions(), no_argument, no_comments, _restoreOptions::no_comments, no_publications, _restoreOptions::no_publications, no_security_labels, _restoreOptions::no_security_labels, no_subscriptions, _restoreOptions::no_subscriptions, _restoreOptions::noDataForFailedTables, _restoreOptions::noOwner, _restoreOptions::noTablespace, Archive::numWorkers, on_exit_close_archive(), OpenArchive(), optarg, optind, option_parse_int(), opts, pg_log_error, pg_log_warning, PG_LOG_WARNING, pg_logging_increase_verbosity(), pg_logging_init(), pg_logging_set_level(), PG_MAX_JOBS, pg_strdup(), PG_TEXTDOMAIN, _connParams::pghost, _connParams::pgport, PrintTOCSummary(), ProcessArchiveRestoreOptions(), progname, _connParams::promptPassword, required_argument, RestoreArchive(), _restoreOptions::schemaExcludeNames, _restoreOptions::schemaNames, _restoreOptions::schemaOnly, _restoreOptions::selFunction, _restoreOptions::selIndex, _restoreOptions::selTable, _restoreOptions::selTrigger, _restoreOptions::selTypes, set_dump_section(), set_pglocale_pgservice(), SetArchiveOptions(), simple_string_list_append(), _restoreOptions::single_txn, SortTocFromFile(), _restoreOptions::strict_names, strict_names, _restoreOptions::superuser, _restoreOptions::tableNames, _restoreOptions::tocFile, _restoreOptions::tocSummary, TRI_NO, TRI_YES, _restoreOptions::triggerNames, usage(), _restoreOptions::use_role, use_setsessauth, _restoreOptions::use_setsessauth, _restoreOptions::useDB, _connParams::username, _restoreOptions::verbose, and Archive::verbose.

58 {
60  int c;
61  int exit_code;
62  int numWorkers = 1;
63  Archive *AH;
64  char *inputFileSpec;
65  static int disable_triggers = 0;
66  static int enable_row_security = 0;
67  static int if_exists = 0;
68  static int no_data_for_failed_tables = 0;
69  static int outputNoTablespaces = 0;
70  static int use_setsessauth = 0;
71  static int no_comments = 0;
72  static int no_publications = 0;
73  static int no_security_labels = 0;
74  static int no_subscriptions = 0;
75  static int strict_names = 0;
76 
77  struct option cmdopts[] = {
78  {"clean", 0, NULL, 'c'},
79  {"create", 0, NULL, 'C'},
80  {"data-only", 0, NULL, 'a'},
81  {"dbname", 1, NULL, 'd'},
82  {"exit-on-error", 0, NULL, 'e'},
83  {"exclude-schema", 1, NULL, 'N'},
84  {"file", 1, NULL, 'f'},
85  {"format", 1, NULL, 'F'},
86  {"function", 1, NULL, 'P'},
87  {"host", 1, NULL, 'h'},
88  {"index", 1, NULL, 'I'},
89  {"jobs", 1, NULL, 'j'},
90  {"list", 0, NULL, 'l'},
91  {"no-privileges", 0, NULL, 'x'},
92  {"no-acl", 0, NULL, 'x'},
93  {"no-owner", 0, NULL, 'O'},
94  {"no-reconnect", 0, NULL, 'R'},
95  {"port", 1, NULL, 'p'},
96  {"no-password", 0, NULL, 'w'},
97  {"password", 0, NULL, 'W'},
98  {"schema", 1, NULL, 'n'},
99  {"schema-only", 0, NULL, 's'},
100  {"superuser", 1, NULL, 'S'},
101  {"table", 1, NULL, 't'},
102  {"trigger", 1, NULL, 'T'},
103  {"use-list", 1, NULL, 'L'},
104  {"username", 1, NULL, 'U'},
105  {"verbose", 0, NULL, 'v'},
106  {"single-transaction", 0, NULL, '1'},
107 
108  /*
109  * the following options don't have an equivalent short option letter
110  */
111  {"disable-triggers", no_argument, &disable_triggers, 1},
112  {"enable-row-security", no_argument, &enable_row_security, 1},
113  {"if-exists", no_argument, &if_exists, 1},
114  {"no-data-for-failed-tables", no_argument, &no_data_for_failed_tables, 1},
115  {"no-tablespaces", no_argument, &outputNoTablespaces, 1},
116  {"role", required_argument, NULL, 2},
117  {"section", required_argument, NULL, 3},
118  {"strict-names", no_argument, &strict_names, 1},
119  {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
120  {"no-comments", no_argument, &no_comments, 1},
121  {"no-publications", no_argument, &no_publications, 1},
122  {"no-security-labels", no_argument, &no_security_labels, 1},
123  {"no-subscriptions", no_argument, &no_subscriptions, 1},
124 
125  {NULL, 0, NULL, 0}
126  };
127 
128  pg_logging_init(argv[0]);
130  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
131 
133 
134  opts = NewRestoreOptions();
135 
136  progname = get_progname(argv[0]);
137 
138  if (argc > 1)
139  {
140  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
141  {
142  usage(progname);
143  exit_nicely(0);
144  }
145  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
146  {
147  puts("pg_restore (PostgreSQL) " PG_VERSION);
148  exit_nicely(0);
149  }
150  }
151 
152  while ((c = getopt_long(argc, argv, "acCd:ef:F:h:I:j:lL:n:N:Op:P:RsS:t:T:U:vwWx1",
153  cmdopts, NULL)) != -1)
154  {
155  switch (c)
156  {
157  case 'a': /* Dump data only */
158  opts->dataOnly = 1;
159  break;
160  case 'c': /* clean (i.e., drop) schema prior to create */
161  opts->dropSchema = 1;
162  break;
163  case 'C':
164  opts->createDB = 1;
165  break;
166  case 'd':
167  opts->cparams.dbname = pg_strdup(optarg);
168  break;
169  case 'e':
170  opts->exit_on_error = true;
171  break;
172  case 'f': /* output file name */
173  opts->filename = pg_strdup(optarg);
174  break;
175  case 'F':
176  if (strlen(optarg) != 0)
177  opts->formatName = pg_strdup(optarg);
178  break;
179  case 'h':
180  if (strlen(optarg) != 0)
181  opts->cparams.pghost = pg_strdup(optarg);
182  break;
183 
184  case 'j': /* number of restore jobs */
185  if (!option_parse_int(optarg, "-j/--jobs", 1,
186  PG_MAX_JOBS,
187  &numWorkers))
188  exit(1);
189  break;
190 
191  case 'l': /* Dump the TOC summary */
192  opts->tocSummary = 1;
193  break;
194 
195  case 'L': /* input TOC summary file name */
196  opts->tocFile = pg_strdup(optarg);
197  break;
198 
199  case 'n': /* Dump data for this schema only */
201  break;
202 
203  case 'N': /* Do not dump data for this schema */
205  break;
206 
207  case 'O':
208  opts->noOwner = 1;
209  break;
210 
211  case 'p':
212  if (strlen(optarg) != 0)
213  opts->cparams.pgport = pg_strdup(optarg);
214  break;
215  case 'R':
216  /* no-op, still accepted for backwards compatibility */
217  break;
218  case 'P': /* Function */
219  opts->selTypes = 1;
220  opts->selFunction = 1;
222  break;
223  case 'I': /* Index */
224  opts->selTypes = 1;
225  opts->selIndex = 1;
227  break;
228  case 'T': /* Trigger */
229  opts->selTypes = 1;
230  opts->selTrigger = 1;
232  break;
233  case 's': /* dump schema only */
234  opts->schemaOnly = 1;
235  break;
236  case 'S': /* Superuser username */
237  if (strlen(optarg) != 0)
238  opts->superuser = pg_strdup(optarg);
239  break;
240  case 't': /* Dump specified table(s) only */
241  opts->selTypes = 1;
242  opts->selTable = 1;
244  break;
245 
246  case 'U':
247  opts->cparams.username = pg_strdup(optarg);
248  break;
249 
250  case 'v': /* verbose */
251  opts->verbose = 1;
253  break;
254 
255  case 'w':
256  opts->cparams.promptPassword = TRI_NO;
257  break;
258 
259  case 'W':
261  break;
262 
263  case 'x': /* skip ACL dump */
264  opts->aclsSkip = 1;
265  break;
266 
267  case '1': /* Restore data in a single transaction */
268  opts->single_txn = true;
269  opts->exit_on_error = true;
270  break;
271 
272  case 0:
273 
274  /*
275  * This covers the long options without a short equivalent.
276  */
277  break;
278 
279  case 2: /* SET ROLE */
280  opts->use_role = pg_strdup(optarg);
281  break;
282 
283  case 3: /* section */
285  break;
286 
287  default:
288  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
289  exit_nicely(1);
290  }
291  }
292 
293  /* Get file name from command line */
294  if (optind < argc)
295  inputFileSpec = argv[optind++];
296  else
297  inputFileSpec = NULL;
298 
299  /* Complain if any arguments remain */
300  if (optind < argc)
301  {
302  pg_log_error("too many command-line arguments (first is \"%s\")",
303  argv[optind]);
304  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
305  progname);
306  exit_nicely(1);
307  }
308 
309  /* Complain if neither -f nor -d was specified (except if dumping TOC) */
310  if (!opts->cparams.dbname && !opts->filename && !opts->tocSummary)
311  {
312  pg_log_error("one of -d/--dbname and -f/--file must be specified");
313  exit_nicely(1);
314  }
315 
316  /* Should get at most one of -d and -f, else user is confused */
317  if (opts->cparams.dbname)
318  {
319  if (opts->filename)
320  {
321  pg_log_error("options -d/--dbname and -f/--file cannot be used together");
322  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
323  progname);
324  exit_nicely(1);
325  }
326  opts->useDB = 1;
327  }
328 
329  if (opts->dataOnly && opts->schemaOnly)
330  {
331  pg_log_error("options -s/--schema-only and -a/--data-only cannot be used together");
332  exit_nicely(1);
333  }
334 
335  if (opts->dataOnly && opts->dropSchema)
336  {
337  pg_log_error("options -c/--clean and -a/--data-only cannot be used together");
338  exit_nicely(1);
339  }
340 
341  /*
342  * -C is not compatible with -1, because we can't create a database inside
343  * a transaction block.
344  */
345  if (opts->createDB && opts->single_txn)
346  {
347  pg_log_error("options -C/--create and -1/--single-transaction cannot be used together");
348  exit_nicely(1);
349  }
350 
351  /* Can't do single-txn mode with multiple connections */
352  if (opts->single_txn && numWorkers > 1)
353  {
354  pg_log_error("cannot specify both --single-transaction and multiple jobs");
355  exit_nicely(1);
356  }
357 
359  opts->enable_row_security = enable_row_security;
360  opts->noDataForFailedTables = no_data_for_failed_tables;
361  opts->noTablespace = outputNoTablespaces;
363  opts->no_comments = no_comments;
367 
368  if (if_exists && !opts->dropSchema)
369  {
370  pg_log_error("option --if-exists requires option -c/--clean");
371  exit_nicely(1);
372  }
373  opts->if_exists = if_exists;
374  opts->strict_names = strict_names;
375 
376  if (opts->formatName)
377  {
378  switch (opts->formatName[0])
379  {
380  case 'c':
381  case 'C':
382  opts->format = archCustom;
383  break;
384 
385  case 'd':
386  case 'D':
387  opts->format = archDirectory;
388  break;
389 
390  case 't':
391  case 'T':
392  opts->format = archTar;
393  break;
394 
395  default:
396  pg_log_error("unrecognized archive format \"%s\"; please specify \"c\", \"d\", or \"t\"",
397  opts->formatName);
398  exit_nicely(1);
399  }
400  }
401 
402  AH = OpenArchive(inputFileSpec, opts->format);
403 
404  SetArchiveOptions(AH, NULL, opts);
405 
406  /*
407  * We don't have a connection yet but that doesn't matter. The connection
408  * is initialized to NULL and if we terminate through exit_nicely() while
409  * it's still NULL, the cleanup function will just be a no-op.
410  */
412 
413  /* Let the archiver know how noisy to be */
414  AH->verbose = opts->verbose;
415 
416  /*
417  * Whether to keep submitting sql commands as "pg_restore ... | psql ... "
418  */
419  AH->exit_on_error = opts->exit_on_error;
420 
421  if (opts->tocFile)
422  SortTocFromFile(AH);
423 
424  AH->numWorkers = numWorkers;
425 
426  if (opts->tocSummary)
427  PrintTOCSummary(AH);
428  else
429  {
431  RestoreArchive(AH);
432  }
433 
434  /* done, print a summary of ignored errors */
435  if (AH->n_errors)
436  pg_log_warning("errors ignored on restore: %d", AH->n_errors);
437 
438  /* AH may be freed in CloseArchive? */
439  exit_code = AH->n_errors ? 1 : 0;
440 
441  CloseArchive(AH);
442 
443  return exit_code;
444 }
int noDataForFailedTables
Definition: pg_backup.h:126
const char * progname
Definition: main.c:46
char * pgport
Definition: pg_backup.h:66
SimpleStringList triggerNames
Definition: pg_backup.h:120
static int no_publications
Definition: pg_dumpall.c:77
bool option_parse_int(const char *optarg, const char *optname, int min_range, int max_range, int *result)
Definition: option_utils.c:50
const char * get_progname(const char *argv0)
Definition: path.c:453
#define pg_log_error(...)
Definition: logging.h:80
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
void pg_logging_init(const char *argv0)
Definition: logging.c:81
Archive * OpenArchive(const char *FileSpec, const ArchiveFormat fmt)
char * use_role
Definition: pg_backup.h:85
SimpleStringList schemaNames
Definition: pg_backup.h:118
int n_errors
Definition: pg_backup.h:215
void on_exit_close_archive(Archive *AHX)
Definition: parallel.c:333
#define fprintf
Definition: port.h:221
char * formatName
Definition: pg_backup.h:109
RestoreOptions * NewRestoreOptions(void)
trivalue promptPassword
Definition: pg_backup.h:69
char * dbname
Definition: pg_backup.h:65
static int disable_triggers
Definition: pg_dumpall.c:71
const char * filename
Definition: pg_backup.h:97
static int strict_names
Definition: pg_dump.c:106
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:50
SimpleStringList tableNames
Definition: pg_backup.h:121
char * c
static void usage(const char *progname)
Definition: pg_restore.c:447
SimpleStringList functionNames
Definition: pg_backup.h:117
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void PrintTOCSummary(Archive *AH)
void CloseArchive(Archive *AH)
int no_security_labels
Definition: pg_backup.h:93
int enable_row_security
Definition: pg_backup.h:134
ConnParams cparams
Definition: pg_backup.h:124
static AmcheckOptions opts
Definition: pg_amcheck.c:110
char * pghost
Definition: pg_backup.h:67
char * username
Definition: pg_backup.h:68
int no_subscriptions
Definition: pg_backup.h:94
char * superuser
Definition: pg_backup.h:84
#define no_argument
Definition: getopt_long.h:24
#define exit_nicely(code)
Definition: pg_dumpall.c:97
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1215
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
void set_dump_section(const char *arg, int *dumpSections)
int use_setsessauth
Definition: pg_backup.h:82
int verbose
Definition: pg_backup.h:194
#define PG_MAX_JOBS
Definition: parallel.h:48
static int no_subscriptions
Definition: pg_dumpall.c:79
void init_parallel_dump_utils(void)
Definition: parallel.c:238
static int use_setsessauth
Definition: pg_dumpall.c:75
static int no_comments
Definition: pg_dumpall.c:76
static int if_exists
Definition: pg_dumpall.c:72
void pg_logging_increase_verbosity(void)
Definition: logging.c:174
int numWorkers
Definition: pg_backup.h:202
void SortTocFromFile(Archive *AHX)
SimpleStringList indexNames
Definition: pg_backup.h:116
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:433
int no_publications
Definition: pg_backup.h:92
char * optarg
Definition: getopt.c:52
void RestoreArchive(Archive *AH)
bool exit_on_error
Definition: pg_backup.h:214
void pg_logging_set_level(enum pg_log_level new_level)
Definition: logging.c:165
void ProcessArchiveRestoreOptions(Archive *AH)
SimpleStringList schemaExcludeNames
Definition: pg_backup.h:119
static int no_security_labels
Definition: pg_dumpall.c:78
#define pg_log_warning(...)
Definition: pgfnames.c:24
#define _(x)
Definition: elog.c:89
char * tocFile
Definition: pg_backup.h:107
int disable_triggers
Definition: pg_backup.h:80
void SetArchiveOptions(Archive *AH, DumpOptions *dopt, RestoreOptions *ropt)

◆ usage()

static void usage ( const char *  progname)
static

Definition at line 447 of file pg_restore.c.

References _, and printf.

Referenced by main().

448 {
449  printf(_("%s restores a PostgreSQL database from an archive created by pg_dump.\n\n"), progname);
450  printf(_("Usage:\n"));
451  printf(_(" %s [OPTION]... [FILE]\n"), progname);
452 
453  printf(_("\nGeneral options:\n"));
454  printf(_(" -d, --dbname=NAME connect to database name\n"));
455  printf(_(" -f, --file=FILENAME output file name (- for stdout)\n"));
456  printf(_(" -F, --format=c|d|t backup file format (should be automatic)\n"));
457  printf(_(" -l, --list print summarized TOC of the archive\n"));
458  printf(_(" -v, --verbose verbose mode\n"));
459  printf(_(" -V, --version output version information, then exit\n"));
460  printf(_(" -?, --help show this help, then exit\n"));
461 
462  printf(_("\nOptions controlling the restore:\n"));
463  printf(_(" -a, --data-only restore only the data, no schema\n"));
464  printf(_(" -c, --clean clean (drop) database objects before recreating\n"));
465  printf(_(" -C, --create create the target database\n"));
466  printf(_(" -e, --exit-on-error exit on error, default is to continue\n"));
467  printf(_(" -I, --index=NAME restore named index\n"));
468  printf(_(" -j, --jobs=NUM use this many parallel jobs to restore\n"));
469  printf(_(" -L, --use-list=FILENAME use table of contents from this file for\n"
470  " selecting/ordering output\n"));
471  printf(_(" -n, --schema=NAME restore only objects in this schema\n"));
472  printf(_(" -N, --exclude-schema=NAME do not restore objects in this schema\n"));
473  printf(_(" -O, --no-owner skip restoration of object ownership\n"));
474  printf(_(" -P, --function=NAME(args) restore named function\n"));
475  printf(_(" -s, --schema-only restore only the schema, no data\n"));
476  printf(_(" -S, --superuser=NAME superuser user name to use for disabling triggers\n"));
477  printf(_(" -t, --table=NAME restore named relation (table, view, etc.)\n"));
478  printf(_(" -T, --trigger=NAME restore named trigger\n"));
479  printf(_(" -x, --no-privileges skip restoration of access privileges (grant/revoke)\n"));
480  printf(_(" -1, --single-transaction restore as a single transaction\n"));
481  printf(_(" --disable-triggers disable triggers during data-only restore\n"));
482  printf(_(" --enable-row-security enable row security\n"));
483  printf(_(" --if-exists use IF EXISTS when dropping objects\n"));
484  printf(_(" --no-comments do not restore comments\n"));
485  printf(_(" --no-data-for-failed-tables do not restore data of tables that could not be\n"
486  " created\n"));
487  printf(_(" --no-publications do not restore publications\n"));
488  printf(_(" --no-security-labels do not restore security labels\n"));
489  printf(_(" --no-subscriptions do not restore subscriptions\n"));
490  printf(_(" --no-tablespaces do not restore tablespace assignments\n"));
491  printf(_(" --section=SECTION restore named section (pre-data, data, or post-data)\n"));
492  printf(_(" --strict-names require table and/or schema include patterns to\n"
493  " match at least one entity each\n"));
494  printf(_(" --use-set-session-authorization\n"
495  " use SET SESSION AUTHORIZATION commands instead of\n"
496  " ALTER OWNER commands to set ownership\n"));
497 
498  printf(_("\nConnection options:\n"));
499  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
500  printf(_(" -p, --port=PORT database server port number\n"));
501  printf(_(" -U, --username=NAME connect as specified database user\n"));
502  printf(_(" -w, --no-password never prompt for password\n"));
503  printf(_(" -W, --password force password prompt (should happen automatically)\n"));
504  printf(_(" --role=ROLENAME do SET ROLE before restore\n"));
505 
506  printf(_("\n"
507  "The options -I, -n, -N, -P, -t, -T, and --section can be combined and specified\n"
508  "multiple times to select multiple objects.\n"));
509  printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
510  printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
511  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
512 }
const char * progname
Definition: main.c:46
#define printf(...)
Definition: port.h:223
#define _(x)
Definition: elog.c:89