PostgreSQL Source Code  git master
pg_restore.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pg_restore.c
4  * pg_restore is an utility extracting postgres database definitions
5  * from a backup archive created by pg_dump using the archiver
6  * interface.
7  *
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
11  * user-defined types
12  * user-defined functions
13  * tables
14  * indexes
15  * aggregates
16  * operators
17  * ACL - grant/revoke
18  *
19  * the output script is SQL that is understood by PostgreSQL
20  *
21  * Basic process in a restore operation is:
22  *
23  * Open the Archive and read the TOC.
24  * Set flags in TOC entries, and *maybe* reorder them.
25  * Generate script to stdout
26  * Exit
27  *
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.
31  *
32  * The author is not responsible for loss or damages that may
33  * result from its use.
34  *
35  *
36  * IDENTIFICATION
37  * src/bin/pg_dump/pg_restore.c
38  *
39  *-------------------------------------------------------------------------
40  */
41 #include "postgres_fe.h"
42 
43 #include <ctype.h>
44 #ifdef HAVE_TERMIOS_H
45 #include <termios.h>
46 #endif
47 
48 #include "getopt_long.h"
49 
50 #include "dumputils.h"
51 #include "parallel.h"
52 #include "pg_backup_utils.h"
53 
54 
55 static void usage(const char *progname);
56 
57 typedef struct option optType;
58 
59 int
60 main(int argc, char **argv)
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 if_exists = 0;
71  static int no_data_for_failed_tables = 0;
72  static int outputNoTablespaces = 0;
73  static int use_setsessauth = 0;
74  static int no_comments = 0;
75  static int no_publications = 0;
76  static int no_security_labels = 0;
77  static int no_subscriptions = 0;
78  static int strict_names = 0;
79 
80  struct option cmdopts[] = {
81  {"clean", 0, NULL, 'c'},
82  {"create", 0, NULL, 'C'},
83  {"data-only", 0, NULL, 'a'},
84  {"dbname", 1, NULL, 'd'},
85  {"exit-on-error", 0, NULL, 'e'},
86  {"exclude-schema", 1, NULL, 'N'},
87  {"file", 1, NULL, 'f'},
88  {"format", 1, NULL, 'F'},
89  {"function", 1, NULL, 'P'},
90  {"host", 1, NULL, 'h'},
91  {"index", 1, NULL, 'I'},
92  {"jobs", 1, NULL, 'j'},
93  {"list", 0, NULL, 'l'},
94  {"no-privileges", 0, NULL, 'x'},
95  {"no-acl", 0, NULL, 'x'},
96  {"no-owner", 0, NULL, 'O'},
97  {"no-reconnect", 0, NULL, 'R'},
98  {"port", 1, NULL, 'p'},
99  {"no-password", 0, NULL, 'w'},
100  {"password", 0, NULL, 'W'},
101  {"schema", 1, NULL, 'n'},
102  {"schema-only", 0, NULL, 's'},
103  {"superuser", 1, NULL, 'S'},
104  {"table", 1, NULL, 't'},
105  {"trigger", 1, NULL, 'T'},
106  {"use-list", 1, NULL, 'L'},
107  {"username", 1, NULL, 'U'},
108  {"verbose", 0, NULL, 'v'},
109  {"single-transaction", 0, NULL, '1'},
110 
111  /*
112  * the following options don't have an equivalent short option letter
113  */
114  {"disable-triggers", no_argument, &disable_triggers, 1},
115  {"enable-row-security", no_argument, &enable_row_security, 1},
116  {"if-exists", no_argument, &if_exists, 1},
117  {"no-data-for-failed-tables", no_argument, &no_data_for_failed_tables, 1},
118  {"no-tablespaces", no_argument, &outputNoTablespaces, 1},
119  {"role", required_argument, NULL, 2},
120  {"section", required_argument, NULL, 3},
121  {"strict-names", no_argument, &strict_names, 1},
122  {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
123  {"no-comments", no_argument, &no_comments, 1},
124  {"no-publications", no_argument, &no_publications, 1},
125  {"no-security-labels", no_argument, &no_security_labels, 1},
126  {"no-subscriptions", no_argument, &no_subscriptions, 1},
127 
128  {NULL, 0, NULL, 0}
129  };
130 
131  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
132 
134 
135  opts = NewRestoreOptions();
136 
137  progname = get_progname(argv[0]);
138 
139  if (argc > 1)
140  {
141  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
142  {
143  usage(progname);
144  exit_nicely(0);
145  }
146  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
147  {
148  puts("pg_restore (PostgreSQL) " PG_VERSION);
149  exit_nicely(0);
150  }
151  }
152 
153  while ((c = getopt_long(argc, argv, "acCd:ef:F:h:I:j:lL:n:N:Op:P:RsS:t:T:U:vwWx1",
154  cmdopts, NULL)) != -1)
155  {
156  switch (c)
157  {
158  case 'a': /* Dump data only */
159  opts->dataOnly = 1;
160  break;
161  case 'c': /* clean (i.e., drop) schema prior to create */
162  opts->dropSchema = 1;
163  break;
164  case 'C':
165  opts->createDB = 1;
166  break;
167  case 'd':
168  opts->dbname = pg_strdup(optarg);
169  break;
170  case 'e':
171  opts->exit_on_error = true;
172  break;
173  case 'f': /* output file name */
174  opts->filename = pg_strdup(optarg);
175  break;
176  case 'F':
177  if (strlen(optarg) != 0)
178  opts->formatName = pg_strdup(optarg);
179  break;
180  case 'h':
181  if (strlen(optarg) != 0)
182  opts->pghost = pg_strdup(optarg);
183  break;
184 
185  case 'j': /* number of restore jobs */
186  numWorkers = atoi(optarg);
187  break;
188 
189  case 'l': /* Dump the TOC summary */
190  opts->tocSummary = 1;
191  break;
192 
193  case 'L': /* input TOC summary file name */
194  opts->tocFile = pg_strdup(optarg);
195  break;
196 
197  case 'n': /* Dump data for this schema only */
199  break;
200 
201  case 'N': /* Do not dump data for this schema */
203  break;
204 
205  case 'O':
206  opts->noOwner = 1;
207  break;
208 
209  case 'p':
210  if (strlen(optarg) != 0)
211  opts->pgport = pg_strdup(optarg);
212  break;
213  case 'R':
214  /* no-op, still accepted for backwards compatibility */
215  break;
216  case 'P': /* Function */
217  opts->selTypes = 1;
218  opts->selFunction = 1;
220  break;
221  case 'I': /* Index */
222  opts->selTypes = 1;
223  opts->selIndex = 1;
225  break;
226  case 'T': /* Trigger */
227  opts->selTypes = 1;
228  opts->selTrigger = 1;
230  break;
231  case 's': /* dump schema only */
232  opts->schemaOnly = 1;
233  break;
234  case 'S': /* Superuser username */
235  if (strlen(optarg) != 0)
236  opts->superuser = pg_strdup(optarg);
237  break;
238  case 't': /* Dump specified table(s) only */
239  opts->selTypes = 1;
240  opts->selTable = 1;
242  break;
243 
244  case 'U':
245  opts->username = pg_strdup(optarg);
246  break;
247 
248  case 'v': /* verbose */
249  opts->verbose = 1;
250  break;
251 
252  case 'w':
253  opts->promptPassword = TRI_NO;
254  break;
255 
256  case 'W':
257  opts->promptPassword = TRI_YES;
258  break;
259 
260  case 'x': /* skip ACL dump */
261  opts->aclsSkip = 1;
262  break;
263 
264  case '1': /* Restore data in a single transaction */
265  opts->single_txn = true;
266  opts->exit_on_error = true;
267  break;
268 
269  case 0:
270 
271  /*
272  * This covers the long options without a short equivalent.
273  */
274  break;
275 
276  case 2: /* SET ROLE */
277  opts->use_role = pg_strdup(optarg);
278  break;
279 
280  case 3: /* section */
282  break;
283 
284  default:
285  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
286  exit_nicely(1);
287  }
288  }
289 
290  /* Get file name from command line */
291  if (optind < argc)
292  inputFileSpec = argv[optind++];
293  else
294  inputFileSpec = NULL;
295 
296  /* Complain if any arguments remain */
297  if (optind < argc)
298  {
299  fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
300  progname, argv[optind]);
301  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
302  progname);
303  exit_nicely(1);
304  }
305 
306  /* Should get at most one of -d and -f, else user is confused */
307  if (opts->dbname)
308  {
309  if (opts->filename)
310  {
311  fprintf(stderr, _("%s: options -d/--dbname and -f/--file cannot be used together\n"),
312  progname);
313  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
314  progname);
315  exit_nicely(1);
316  }
317  opts->useDB = 1;
318  }
319 
320  if (opts->dataOnly && opts->schemaOnly)
321  {
322  fprintf(stderr, _("%s: options -s/--schema-only and -a/--data-only cannot be used together\n"),
323  progname);
324  exit_nicely(1);
325  }
326 
327  if (opts->dataOnly && opts->dropSchema)
328  {
329  fprintf(stderr, _("%s: options -c/--clean and -a/--data-only cannot be used together\n"),
330  progname);
331  exit_nicely(1);
332  }
333 
334  if (numWorkers <= 0)
335  {
336  fprintf(stderr, _("%s: invalid number of parallel jobs\n"), progname);
337  exit(1);
338  }
339 
340  /* See comments in pg_dump.c */
341 #ifdef WIN32
342  if (numWorkers > MAXIMUM_WAIT_OBJECTS)
343  {
344  fprintf(stderr, _("%s: maximum number of parallel jobs is %d\n"),
345  progname, MAXIMUM_WAIT_OBJECTS);
346  exit(1);
347  }
348 #endif
349 
350  /* Can't do single-txn mode with multiple connections */
351  if (opts->single_txn && numWorkers > 1)
352  {
353  fprintf(stderr, _("%s: cannot specify both --single-transaction and multiple jobs\n"),
354  progname);
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  fprintf(stderr, _("%s: option --if-exists requires option -c/--clean\n"),
371  progname);
372  exit_nicely(1);
373  }
374  opts->if_exists = if_exists;
375  opts->strict_names = strict_names;
376 
377  if (opts->formatName)
378  {
379  switch (opts->formatName[0])
380  {
381  case 'c':
382  case 'C':
383  opts->format = archCustom;
384  break;
385 
386  case 'd':
387  case 'D':
388  opts->format = archDirectory;
389  break;
390 
391  case 't':
392  case 'T':
393  opts->format = archTar;
394  break;
395 
396  default:
397  write_msg(NULL, "unrecognized archive format \"%s\"; please specify \"c\", \"d\", or \"t\"\n",
398  opts->formatName);
399  exit_nicely(1);
400  }
401  }
402 
403  AH = OpenArchive(inputFileSpec, opts->format);
404 
405  SetArchiveOptions(AH, NULL, opts);
406 
407  /*
408  * We don't have a connection yet but that doesn't matter. The connection
409  * is initialized to NULL and if we terminate through exit_nicely() while
410  * it's still NULL, the cleanup function will just be a no-op.
411  */
413 
414  /* Let the archiver know how noisy to be */
415  AH->verbose = opts->verbose;
416 
417  /*
418  * Whether to keep submitting sql commands as "pg_restore ... | psql ... "
419  */
420  AH->exit_on_error = opts->exit_on_error;
421 
422  if (opts->tocFile)
423  SortTocFromFile(AH);
424 
425  AH->numWorkers = numWorkers;
426 
427  if (opts->tocSummary)
428  PrintTOCSummary(AH);
429  else
430  {
432  RestoreArchive(AH);
433  }
434 
435  /* done, print a summary of ignored errors */
436  if (AH->n_errors)
437  fprintf(stderr, _("WARNING: errors ignored on restore: %d\n"),
438  AH->n_errors);
439 
440  /* AH may be freed in CloseArchive? */
441  exit_code = AH->n_errors ? 1 : 0;
442 
443  CloseArchive(AH);
444 
445  return exit_code;
446 }
447 
448 static void
449 usage(const char *progname)
450 {
451  printf(_("%s restores a PostgreSQL database from an archive created by pg_dump.\n\n"), progname);
452  printf(_("Usage:\n"));
453  printf(_(" %s [OPTION]... [FILE]\n"), progname);
454 
455  printf(_("\nGeneral options:\n"));
456  printf(_(" -d, --dbname=NAME connect to database name\n"));
457  printf(_(" -f, --file=FILENAME output file name\n"));
458  printf(_(" -F, --format=c|d|t backup file format (should be automatic)\n"));
459  printf(_(" -l, --list print summarized TOC of the archive\n"));
460  printf(_(" -v, --verbose verbose mode\n"));
461  printf(_(" -V, --version output version information, then exit\n"));
462  printf(_(" -?, --help show this help, then exit\n"));
463 
464  printf(_("\nOptions controlling the restore:\n"));
465  printf(_(" -a, --data-only restore only the data, no schema\n"));
466  printf(_(" -c, --clean clean (drop) database objects before recreating\n"));
467  printf(_(" -C, --create create the target database\n"));
468  printf(_(" -e, --exit-on-error exit on error, default is to continue\n"));
469  printf(_(" -I, --index=NAME restore named index\n"));
470  printf(_(" -j, --jobs=NUM use this many parallel jobs to restore\n"));
471  printf(_(" -L, --use-list=FILENAME use table of contents from this file for\n"
472  " selecting/ordering output\n"));
473  printf(_(" -n, --schema=NAME restore only objects in this schema\n"));
474  printf(_(" -N, --exclude-schema=NAME do not restore objects in this schema\n"));
475  printf(_(" -O, --no-owner skip restoration of object ownership\n"));
476  printf(_(" -P, --function=NAME(args) restore named function\n"));
477  printf(_(" -s, --schema-only restore only the schema, no data\n"));
478  printf(_(" -S, --superuser=NAME superuser user name to use for disabling triggers\n"));
479  printf(_(" -t, --table=NAME restore named relation (table, view, etc.)\n"));
480  printf(_(" -T, --trigger=NAME restore named trigger\n"));
481  printf(_(" -x, --no-privileges skip restoration of access privileges (grant/revoke)\n"));
482  printf(_(" -1, --single-transaction restore as a single transaction\n"));
483  printf(_(" --disable-triggers disable triggers during data-only restore\n"));
484  printf(_(" --enable-row-security enable row security\n"));
485  printf(_(" --if-exists use IF EXISTS when dropping objects\n"));
486  printf(_(" --no-data-for-failed-tables do not restore data of tables that could not be\n"
487  " created\n"));
488  printf(_(" --no-comments do not dump comments\n"));
489  printf(_(" --no-publications do not restore publications\n"));
490  printf(_(" --no-security-labels do not restore security labels\n"));
491  printf(_(" --no-subscriptions do not restore subscriptions\n"));
492  printf(_(" --no-tablespaces do not restore tablespace assignments\n"));
493  printf(_(" --section=SECTION restore named section (pre-data, data, or post-data)\n"));
494  printf(_(" --strict-names require table and/or schema include patterns to\n"
495  " match at least one entity each\n"));
496  printf(_(" --use-set-session-authorization\n"
497  " use SET SESSION AUTHORIZATION commands instead of\n"
498  " ALTER OWNER commands to set ownership\n"));
499 
500  printf(_("\nConnection options:\n"));
501  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
502  printf(_(" -p, --port=PORT database server port number\n"));
503  printf(_(" -U, --username=NAME connect as specified database user\n"));
504  printf(_(" -w, --no-password never prompt for password\n"));
505  printf(_(" -W, --password force password prompt (should happen automatically)\n"));
506  printf(_(" --role=ROLENAME do SET ROLE before restore\n"));
507 
508  printf(_("\n"
509  "The options -I, -n, -P, -t, -T, and --section can be combined and specified\n"
510  "multiple times to select multiple objects.\n"));
511  printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
512  printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
513 }
int noDataForFailedTables
Definition: pg_backup.h:114
SimpleStringList triggerNames
Definition: pg_backup.h:106
static int no_publications
Definition: pg_dumpall.c:74
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:113
Archive * OpenArchive(const char *FileSpec, const ArchiveFormat fmt)
char * use_role
Definition: pg_backup.h:71
SimpleStringList schemaNames
Definition: pg_backup.h:104
int n_errors
Definition: pg_backup.h:207
void on_exit_close_archive(Archive *AHX)
Definition: parallel.c:347
const char * progname
Definition: pg_standby.c:37
char * formatName
Definition: pg_backup.h:95
RestoreOptions * NewRestoreOptions(void)
static int disable_triggers
Definition: pg_dumpall.c:68
const char * filename
Definition: pg_backup.h:83
static int strict_names
Definition: pg_dump.c:107
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:51
SimpleStringList tableNames
Definition: pg_backup.h:107
char * c
static void usage(const char *progname)
Definition: pg_restore.c:449
SimpleStringList functionNames
Definition: pg_backup.h:103
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:79
int enable_row_security
Definition: pg_backup.h:123
int no_subscriptions
Definition: pg_backup.h:80
char * superuser
Definition: pg_backup.h:70
#define no_argument
Definition: getopt_long.h:24
#define exit_nicely(code)
Definition: pg_dumpall.c:89
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1054
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:186
int main(int argc, char **argv)
Definition: pg_restore.c:60
static int no_subscriptions
Definition: pg_dumpall.c:76
void init_parallel_dump_utils(void)
Definition: parallel.c:251
void write_msg(const char *modulename, const char *fmt,...)
static int use_setsessauth
Definition: pg_dumpall.c:72
static int no_comments
Definition: pg_dumpall.c:73
static int if_exists
Definition: pg_dumpall.c:69
int numWorkers
Definition: pg_backup.h:194
void SortTocFromFile(Archive *AHX)
SimpleStringList indexNames
Definition: pg_backup.h:102
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:550
int no_publications
Definition: pg_backup.h:78
char * optarg
Definition: getopt.c:53
void RestoreArchive(Archive *AH)
bool exit_on_error
Definition: pg_backup.h:206
void ProcessArchiveRestoreOptions(Archive *AH)
SimpleStringList schemaExcludeNames
Definition: pg_backup.h:105
static int no_security_labels
Definition: pg_dumpall.c:75
#define _(x)
Definition: elog.c:84
char * tocFile
Definition: pg_backup.h:93
int disable_triggers
Definition: pg_backup.h:66
void SetArchiveOptions(Archive *AH, DumpOptions *dopt, RestoreOptions *ropt)
trivalue promptPassword
Definition: pg_backup.h:115