PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_restore.c File Reference
#include "postgres_fe.h"
#include <ctype.h>
#include "getopt_long.h"
#include "dumputils.h"
#include "parallel.h"
#include "pg_backup_utils.h"
Include dependency graph for pg_restore.c:

Go to the source code of this file.

Typedefs

typedef struct option optType
 

Functions

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

Typedef Documentation

Definition at line 57 of file pg_restore.c.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 60 of file pg_restore.c.

References _, _restoreOptions::aclsSkip, archCustom, archDirectory, archTar, CloseArchive(), _restoreOptions::createDB, _restoreOptions::dataOnly, _restoreOptions::dbname, _restoreOptions::disable_triggers, 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, _restoreOptions::functionNames, get_progname(), getopt_long(), if_exists, _restoreOptions::if_exists, _restoreOptions::include_subscriptions, _restoreOptions::indexNames, init_parallel_dump_utils(), Archive::n_errors, NewRestoreOptions(), no_argument, no_security_labels, _restoreOptions::no_security_labels, _restoreOptions::noDataForFailedTables, _restoreOptions::noOwner, _restoreOptions::noTablespace, NULL, Archive::numWorkers, on_exit_close_archive(), OpenArchive(), optarg, optind, pg_strdup(), PG_TEXTDOMAIN, _restoreOptions::pghost, _restoreOptions::pgport, PrintTOCSummary(), ProcessArchiveRestoreOptions(), progname, _restoreOptions::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, _restoreOptions::use_setsessauth, use_setsessauth, _restoreOptions::useDB, _restoreOptions::username, _restoreOptions::verbose, Archive::verbose, and write_msg().

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

Definition at line 443 of file pg_restore.c.

References _.

Referenced by main().

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