82 bool data_only =
false;
83 bool schema_only =
false;
85 bool globals_only =
false;
88 static int enable_row_security = 0;
90 static int no_data_for_failed_tables = 0;
91 static int outputNoTableAm = 0;
92 static int outputNoTablespaces = 0;
108 struct option cmdopts[] = {
109 {
"clean", 0, NULL,
'c'},
110 {
"create", 0, NULL,
'C'},
111 {
"data-only", 0, NULL,
'a'},
112 {
"globals-only", 0, NULL,
'g'},
113 {
"dbname", 1, NULL,
'd'},
114 {
"exit-on-error", 0, NULL,
'e'},
115 {
"exclude-schema", 1, NULL,
'N'},
116 {
"file", 1, NULL,
'f'},
117 {
"format", 1, NULL,
'F'},
118 {
"function", 1, NULL,
'P'},
119 {
"host", 1, NULL,
'h'},
120 {
"index", 1, NULL,
'I'},
121 {
"jobs", 1, NULL,
'j'},
122 {
"list", 0, NULL,
'l'},
123 {
"no-privileges", 0, NULL,
'x'},
124 {
"no-acl", 0, NULL,
'x'},
125 {
"no-owner", 0, NULL,
'O'},
126 {
"no-reconnect", 0, NULL,
'R'},
127 {
"port", 1, NULL,
'p'},
128 {
"no-password", 0, NULL,
'w'},
129 {
"password", 0, NULL,
'W'},
130 {
"schema", 1, NULL,
'n'},
131 {
"schema-only", 0, NULL,
's'},
132 {
"superuser", 1, NULL,
'S'},
133 {
"table", 1, NULL,
't'},
134 {
"trigger", 1, NULL,
'T'},
135 {
"use-list", 1, NULL,
'L'},
136 {
"username", 1, NULL,
'U'},
137 {
"verbose", 0, NULL,
'v'},
138 {
"single-transaction", 0, NULL,
'1'},
144 {
"enable-row-security",
no_argument, &enable_row_security, 1},
146 {
"no-data-for-failed-tables",
no_argument, &no_data_for_failed_tables, 1},
147 {
"no-table-access-method",
no_argument, &outputNoTableAm, 1},
148 {
"no-tablespaces",
no_argument, &outputNoTablespaces, 1},
184 if (strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-?") == 0)
189 if (strcmp(argv[1],
"--version") == 0 || strcmp(argv[1],
"-V") == 0)
191 puts(
"pg_restore (PostgreSQL) " PG_VERSION);
196 while ((
c =
getopt_long(argc, argv,
"acCd:ef:F:gh:I:j:lL:n:N:Op:P:RsS:t:T:U:vwWx1",
197 cmdopts, NULL)) != -1)
205 opts->dropSchema = 1;
214 opts->exit_on_error =
true;
239 opts->tocSummary = 1;
267 opts->selFunction = 1;
277 opts->selTrigger = 1;
315 opts->single_txn =
true;
316 opts->exit_on_error =
true;
343 opts->exit_on_error =
true;
358 inputFileSpec = argv[
optind++];
360 inputFileSpec = NULL;
365 pg_log_error(
"too many command-line arguments (first is \"%s\")",
372 if (!
opts->cparams.dbname && !
opts->filename && !
opts->tocSummary)
373 pg_fatal(
"one of -d/--dbname and -f/--file must be specified");
375 if (db_exclude_patterns.
head != NULL && globals_only)
377 pg_log_error(
"option --exclude-database cannot be used together with -g/--globals-only");
383 if (
opts->cparams.dbname)
387 pg_log_error(
"options -d/--dbname and -f/--file cannot be used together");
395 if (data_only && schema_only)
396 pg_fatal(
"options -s/--schema-only and -a/--data-only cannot be used together");
398 pg_fatal(
"options -s/--schema-only and --statistics-only cannot be used together");
400 pg_fatal(
"options -a/--data-only and --statistics-only cannot be used together");
404 pg_fatal(
"options -a/--data-only and --no-data cannot be used together");
406 pg_fatal(
"options -s/--schema-only and --no-schema cannot be used together");
408 pg_fatal(
"options --statistics-only and --no-statistics cannot be used together");
412 pg_fatal(
"options --with-data and --no-data cannot be used together");
414 pg_fatal(
"options --with-schema and --no-schema cannot be used together");
416 pg_fatal(
"options --with-statistics and --no-statistics cannot be used together");
418 if (data_only &&
opts->dropSchema)
419 pg_fatal(
"options -c/--clean and -a/--data-only cannot be used together");
421 if (
opts->single_txn &&
opts->txn_size > 0)
422 pg_fatal(
"options -1/--single-transaction and --transaction-size cannot be used together");
428 if (
opts->createDB &&
opts->single_txn)
429 pg_fatal(
"options -C/--create and -1/--single-transaction cannot be used together");
432 if (
opts->single_txn && numWorkers > 1)
433 pg_fatal(
"cannot specify both --single-transaction and multiple jobs");
446 opts->dumpStatistics = ((
opts->dumpStatistics && !schema_only && !data_only) ||
450 opts->enable_row_security = enable_row_security;
451 opts->noDataForFailedTables = no_data_for_failed_tables;
452 opts->noTableAm = outputNoTableAm;
453 opts->noTablespace = outputNoTablespaces;
462 pg_fatal(
"option --if-exists requires option -c/--clean");
466 if (
opts->formatName)
481 pg_fatal(
"archive format \"%s\" is not supported; please use psql",
485 pg_fatal(
"unrecognized archive format \"%s\"; please specify \"c\", \"d\", or \"t\"",
505 if (
opts->tocSummary)
506 pg_fatal(
"option -l/--list cannot be used when restoring an archive created by pg_dumpall");
507 else if (
opts->tocFile)
508 pg_fatal(
"option -L/--use-list cannot be used when restoring an archive created by pg_dumpall");
514 if (!globals_only &&
opts->createDB != 1)
516 pg_log_error(
"-C/--create option should be specified when restoring an archive created by pg_dumpall");
518 pg_log_error_hint(
"Individual databases can be restored using their specific archives.");
526 if (
opts->cparams.dbname)
530 false,
progname, NULL, NULL, NULL, NULL);
534 pg_fatal(
"could not connect to database \"%s\"",
opts->cparams.dbname);
550 pg_log_info(
"database restoring skipped as -g/--globals-only option was specified");
564 if (db_exclude_patterns.
head != NULL)
565 pg_fatal(
"option --exclude-database can be used only when restoring an archive created by pg_dumpall");
568 pg_fatal(
"option -g/--globals-only can be used only when restoring an archive created by pg_dumpall");
627 if (
opts->tocSummary)
646 printf(
_(
"%s restores a PostgreSQL database from an archive created by pg_dump or pg_dumpall.\n"
647 "If the archive is created by pg_dumpall, then restores multiple databases also.\n\n"),
progname);
651 printf(
_(
"\nGeneral options:\n"));
652 printf(
_(
" -d, --dbname=NAME connect to database name\n"));
653 printf(
_(
" -f, --file=FILENAME output file name (- for stdout)\n"));
654 printf(
_(
" -F, --format=c|d|t backup file format (should be automatic)\n"));
655 printf(
_(
" -l, --list print summarized TOC of the archive\n"));
656 printf(
_(
" -v, --verbose verbose mode\n"));
657 printf(
_(
" -V, --version output version information, then exit\n"));
658 printf(
_(
" -?, --help show this help, then exit\n"));
660 printf(
_(
"\nOptions controlling the restore:\n"));
661 printf(
_(
" -a, --data-only restore only the data, no schema\n"));
662 printf(
_(
" -c, --clean clean (drop) database objects before recreating\n"));
663 printf(
_(
" -C, --create create the target database\n"));
664 printf(
_(
" -e, --exit-on-error exit on error, default is to continue\n"));
665 printf(
_(
" -g, --globals-only restore only global objects, no databases\n"));
666 printf(
_(
" -I, --index=NAME restore named index\n"));
667 printf(
_(
" -j, --jobs=NUM use this many parallel jobs to restore\n"));
668 printf(
_(
" -L, --use-list=FILENAME use table of contents from this file for\n"
669 " selecting/ordering output\n"));
670 printf(
_(
" -n, --schema=NAME restore only objects in this schema\n"));
671 printf(
_(
" -N, --exclude-schema=NAME do not restore objects in this schema\n"));
672 printf(
_(
" -O, --no-owner skip restoration of object ownership\n"));
673 printf(
_(
" -P, --function=NAME(args) restore named function\n"));
674 printf(
_(
" -s, --schema-only restore only the schema, no data\n"));
675 printf(
_(
" -S, --superuser=NAME superuser user name to use for disabling triggers\n"));
676 printf(
_(
" -t, --table=NAME restore named relation (table, view, etc.)\n"));
677 printf(
_(
" -T, --trigger=NAME restore named trigger\n"));
678 printf(
_(
" --exclude-database=PATTERN exclude databases whose name matches with pattern\n"));
679 printf(
_(
" -x, --no-privileges skip restoration of access privileges (grant/revoke)\n"));
680 printf(
_(
" -1, --single-transaction restore as a single transaction\n"));
681 printf(
_(
" --disable-triggers disable triggers during data-only restore\n"));
682 printf(
_(
" --enable-row-security enable row security\n"));
683 printf(
_(
" --filter=FILENAME restore or skip objects based on expressions\n"
685 printf(
_(
" --if-exists use IF EXISTS when dropping objects\n"));
686 printf(
_(
" --no-comments do not restore comment commands\n"));
687 printf(
_(
" --no-data do not restore data\n"));
688 printf(
_(
" --no-data-for-failed-tables do not restore data of tables that could not be\n"
690 printf(
_(
" --no-policies do not restore row security policies\n"));
691 printf(
_(
" --no-publications do not restore publications\n"));
692 printf(
_(
" --no-schema do not restore schema\n"));
693 printf(
_(
" --no-security-labels do not restore security labels\n"));
694 printf(
_(
" --no-statistics do not restore statistics\n"));
695 printf(
_(
" --no-subscriptions do not restore subscriptions\n"));
696 printf(
_(
" --no-table-access-method do not restore table access methods\n"));
697 printf(
_(
" --no-tablespaces do not restore tablespace assignments\n"));
698 printf(
_(
" --section=SECTION restore named section (pre-data, data, or post-data)\n"));
699 printf(
_(
" --statistics-only restore only the statistics, not schema or data\n"));
700 printf(
_(
" --strict-names require table and/or schema include patterns to\n"
701 " match at least one entity each\n"));
702 printf(
_(
" --transaction-size=N commit after every N objects\n"));
703 printf(
_(
" --use-set-session-authorization\n"
704 " use SET SESSION AUTHORIZATION commands instead of\n"
705 " ALTER OWNER commands to set ownership\n"));
706 printf(
_(
" --with-data dump the data\n"));
707 printf(
_(
" --with-schema dump the schema\n"));
708 printf(
_(
" --with-statistics dump the statistics\n"));
710 printf(
_(
"\nConnection options:\n"));
711 printf(
_(
" -h, --host=HOSTNAME database server host or socket directory\n"));
712 printf(
_(
" -p, --port=PORT database server port number\n"));
713 printf(
_(
" -U, --username=NAME connect as specified database user\n"));
714 printf(
_(
" -w, --no-password never prompt for password\n"));
715 printf(
_(
" -W, --password force password prompt (should happen automatically)\n"));
716 printf(
_(
" --role=ROLENAME do SET ROLE before restore\n"));
719 "The options -I, -n, -N, -P, -t, -T, --section, and --exclude-database can be combined\n"
720 "and specified multiple times to select multiple objects.\n"));
721 printf(
_(
"\nIf no input file name is supplied, then standard input is used.\n\n"));
722 printf(
_(
"Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
723 printf(
_(
"%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
764 opts->selFunction = 1;
782 opts->selTrigger = 1;
838 pg_fatal(
"directory name too long: \"%s\"", dir);
867 while ((
c = fgetc(pfile)) != EOF)
869 if (
c !=
'\'' &&
c !=
'"' &&
c !=
'\n' &&
c !=
';')
872 while ((
c = fgetc(pfile)) != EOF)
874 if (
c !=
'\'' &&
c !=
'"' &&
c !=
';' &&
c !=
'\n')
881 if (
c ==
'\'' ||
c ==
'"')
886 while ((
c = fgetc(pfile)) != EOF)
912 if (
c == EOF && inBuf->
len == 0)
940 pg_log_info(
"considering PATTERN as NAME for --exclude-database option as no db connection while doing pg_restore.");
947 db_cell; db_cell = db_cell->
next)
949 bool skip_db_restore =
false;
961 skip_db_restore =
true;
969 false, NULL, db_lit->
data,
970 NULL, NULL, NULL, &dotcnt);
974 pg_log_error(
"improper qualified name (too many dotted names): %s",
984 skip_db_restore =
true;
985 pg_log_info(
"database \"%s\" matches exclude pattern: \"%s\"", db_cell->str, pat_cell->val);
1002 if (skip_db_restore)
1004 pg_log_info(
"excluding database \"%s\"", db_cell->str);
1040 pg_log_info(
"database restoring is skipped as \"map.dat\" is not present in \"%s\"", dumpdirpath);
1050 pg_fatal(
"could not open \"%s\": %m", map_file_path);
1053 while ((fgets(line,
MAXPGPATH, pfile)) != NULL)
1060 sscanf(line,
"%u", &db_oid);
1061 sscanf(line,
"%20s", db_oid_str);
1064 dbname = line + strlen(db_oid_str) + 1;
1069 pg_log_info(
"found database \"%s\" (OID: %u) in \"%s\"",
1070 dbname, db_oid, map_file_path);
1074 pg_fatal(
"invalid entry in \"%s\" at line : %d", map_file_path,
1104 int num_db_restore = 0;
1108 char *connected_db = NULL;
1109 bool dumpData =
opts->dumpData;
1110 bool dumpSchema =
opts->dumpSchema;
1111 bool dumpStatistics =
opts->dumpSchema;
1114 if (
opts->cparams.dbname)
1115 connected_db =
opts->cparams.dbname;
1120 if (dbname_oid_list.
head == NULL)
1123 pg_log_info(
"found %d database names in \"map.dat\"", num_total_db);
1127 pg_log_info(
"trying to connect database \"postgres\"");
1131 false,
progname, NULL, NULL, NULL, NULL);
1136 pg_log_info(
"trying to connect database \"template1\"");
1140 false,
progname, NULL, NULL, NULL, NULL);
1148 db_exclude_patterns);
1158 if (dbname_oid_list.
head == NULL || num_db_restore == 0)
1160 pg_log_info(
"no database needs to restore out of %d databases", num_total_db);
1161 return n_errors_total;
1164 pg_log_info(
"need to restore %d databases out of %d databases", num_db_restore, num_total_db);
1172 db_cell; db_cell = db_cell->
next)
1187 if (
opts->cparams.override_dbname)
1190 opts->cparams.override_dbname = NULL;
1202 snprintf(subdirpath,
MAXPGPATH,
"%s/databases/%u.tar", dumpdirpath, db_cell->oid);
1208 snprintf(subdirpath,
MAXPGPATH,
"%s/databases/%u.dmp", dumpdirpath, db_cell->oid);
1213 pg_log_info(
"restoring database \"%s\"", db_cell->str);
1216 if (
opts->cparams.dbname)
1222 false,
progname, NULL, NULL, NULL, NULL);
1229 opts->cparams.dbname = db_cell->str;
1235 opts->cparams.dbname = connected_db;
1243 opts->dumpData = dumpData;
1244 opts->dumpSchema = dumpSchema;
1245 opts->dumpStatistics = dumpStatistics;
1253 n_errors_total += n_errors;
1254 pg_log_warning(
"errors ignored on database \"%s\" restore: %d", db_cell->str, n_errors);
1261 pg_log_info(
"number of restored databases is %d", num_db_restore);
1266 return n_errors_total;
1295 pg_fatal(
"could not open \"%s\": %m", global_file_path);
1321 if (strstr(sqlstatement.
data, user_create.
data))
1342 pg_log_warning(
"ignored %d errors in \"%s\"", n_errors, global_file_path);
1363 if (strcmp(
outfile,
"-") == 0)
1378 while ((
c = fgetc(pfile)) != EOF)
1384 if (strcmp(
outfile,
"-") != 0)
void replace_on_exit_close_archive(Archive *AHX)
void on_exit_close_archive(Archive *AHX)
void init_parallel_dump_utils(void)
#define PG_TEXTDOMAIN(domain)
#define OidIsValid(objectId)
void set_pglocale_pgservice(const char *argv0, const char *app)
PGresult * executeQuery(PGconn *conn, const char *query)
PGconn * ConnectDatabase(const char *dbname, const char *connection_string, const char *pghost, const char *pgport, const char *pguser, trivalue prompt_password, bool fail_on_error, const char *progname, const char **connstr, int *server_version, char *password, char *override_dbname)
static size_t append_data(char *buf, size_t size, size_t nmemb, void *userdata)
void PQfinish(PGconn *conn)
char * PQuser(const PGconn *conn)
char * PQerrorMessage(const PGconn *conn)
ExecStatusType PQresultStatus(const PGresult *res)
void PQclear(PGresult *res)
int PQntuples(const PGresult *res)
PGresult * PQexec(PGconn *conn, const char *query)
char * pg_strdup(const char *in)
void filter_init(FilterStateData *fstate, const char *filename, exit_function f_exit)
void filter_free(FilterStateData *fstate)
const char * filter_object_type_name(FilterObjectType fot)
bool filter_read_item(FilterStateData *fstate, char **objname, FilterCommandType *comtype, FilterObjectType *objtype)
void pg_log_filter_error(FilterStateData *fstate, const char *fmt,...)
@ FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN
@ FILTER_OBJECT_TYPE_SCHEMA
@ FILTER_OBJECT_TYPE_INDEX
@ FILTER_OBJECT_TYPE_TRIGGER
@ FILTER_OBJECT_TYPE_FOREIGN_DATA
@ FILTER_OBJECT_TYPE_DATABASE
@ FILTER_OBJECT_TYPE_FUNCTION
@ FILTER_OBJECT_TYPE_TABLE_DATA
@ FILTER_OBJECT_TYPE_NONE
@ FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN
@ FILTER_OBJECT_TYPE_EXTENSION
@ FILTER_OBJECT_TYPE_TABLE
@ FILTER_COMMAND_TYPE_NONE
@ FILTER_COMMAND_TYPE_EXCLUDE
@ FILTER_COMMAND_TYPE_INCLUDE
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
#define required_argument
Assert(PointerIsAligned(start, uint64))
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(...)
void pfree(void *pointer)
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 RestoreArchive(Archive *AHX, bool append_data)
void CloseArchive(Archive *AHX)
void SortTocFromFile(Archive *AHX)
void PrintTOCSummary(Archive *AHX)
void SetArchiveOptions(Archive *AH, DumpOptions *dopt, RestoreOptions *ropt)
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 bool file_exists_in_directory(const char *dir, const char *filename)
static int get_dbnames_list_to_restore(PGconn *conn, SimpleOidStringList *dbname_oid_list, SimpleStringList db_exclude_patterns)
int main(int argc, char **argv)
static int restore_all_databases(PGconn *conn, const char *dumpdirpath, SimpleStringList db_exclude_patterns, RestoreOptions *opts, int numWorkers)
static void copy_or_print_global_file(const char *outfile, FILE *pfile)
static int get_dbname_oid_list_from_mfile(const char *dumpdirpath, SimpleOidStringList *dbname_oid_list)
static void read_restore_filters(const char *filename, RestoreOptions *opts)
static int process_global_sql_commands(PGconn *conn, const char *dumpdirpath, const char *outfile)
static int read_one_statement(StringInfo inBuf, FILE *pfile)
static int restore_one_database(const char *inputFileSpec, RestoreOptions *opts, int numWorkers, bool append_data, int num)
#define pg_log_warning(...)
int pg_strcasecmp(const char *s1, const char *s2)
const char * get_progname(const char *argv0)
PQExpBuffer createPQExpBuffer(void)
void resetPQExpBuffer(PQExpBuffer str)
void destroyPQExpBuffer(PQExpBuffer str)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
void simple_oid_string_list_append(SimpleOidStringList *list, Oid oid, const char *str)
void simple_oid_string_list_destroy(SimpleOidStringList *list)
void simple_string_list_append(SimpleStringList *list, const char *val)
void simple_string_list_destroy(SimpleStringList *list)
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
bool processSQLNamePattern(PGconn *conn, PQExpBuffer buf, const char *pattern, bool have_where, bool force_escape, const char *schemavar, const char *namevar, const char *altnamevar, const char *visibilityrule, PQExpBuffer dbnamebuf, int *dotcnt)
void resetStringInfo(StringInfo str)
void appendStringInfoString(StringInfo str, const char *s)
void appendStringInfoChar(StringInfo str, char ch)
void initStringInfo(StringInfo str)
struct SimpleOidStringListCell * next
SimpleOidStringListCell * head
struct SimpleStringListCell * next
SimpleStringListCell * head