3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "PG_RESTORE" "1" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 pg_restore \- restore a PostgreSQL database from an archive file created by pg_dump
33 .HP \w'\fBpg_restore\fR\ 'u
34 \fBpg_restore\fR [\fIconnection\-option\fR...] [\fIoption\fR...] [\fIfilename\fR]
38 is a utility for restoring a
40 database from an archive created by
42 in one of the non\-plain\-text formats\&. It will issue the commands necessary to reconstruct the database to the state it was in at the time it was saved\&. The archive files also allow
44 to be selective about what is restored, or even to reorder the items prior to being restored\&. The archive files are designed to be portable across architectures\&.
47 can operate in two modes\&. If a database name is specified,
49 connects to that database and restores archive contents directly into the database\&. Otherwise, a script containing the SQL commands necessary to rebuild the database is created and written to a file or standard output\&. This script output is equivalent to the plain text output format of
50 pg_dump\&. Some of the options controlling the output are therefore analogous to
56 cannot restore information that is not present in the archive file\&. For instance, if the archive was made using the
57 \(lqdump data as \fBINSERT\fR commands\(rq
60 will not be able to load the data using
68 .nr an-no-space-flag 1
76 Restoring a dump causes the destination to execute arbitrary code of the source superusers\*(Aq choice\&. Partial dumps and partial restores do not limit that\&. If the source superusers are not trusted, the dumped SQL statements must be inspected before restoring\&. Non\-plain\-text dumps can be inspected by using
79 option\&. Note that the client running the dump and restore need not trust the source or destination superusers\&.
85 accepts the following command line arguments\&.
89 Specifies the location of the archive file (or directory, for a directory\-format archive) to be restored\&. If not specified, the standard input is used\&.
96 Restore only the data, not the schema (data definitions) or statistics\&. Table data, large objects, and sequence values are restored, if present in the archive\&.
98 This option is similar to, but for historical reasons not identical to, specifying
99 \fB\-\-section=data\fR\&.
106 Before restoring database objects, issue commands to
108 all the objects that will be restored\&. This option is useful for overwriting an existing database\&. If any of the objects do not exist in the destination database, ignorable error messages will be reported, unless
117 Create the database before restoring into it\&. If
119 is also specified, drop and recreate the target database before connecting to it\&.
124 also restores the database\*(Aqs comment if any, and any configuration variable settings that are specific to this database, that is, any
125 \fBALTER DATABASE \&.\&.\&. SET \&.\&.\&.\fR
127 \fBALTER ROLE \&.\&.\&. IN DATABASE \&.\&.\&. SET \&.\&.\&.\fR
128 commands that mention this database\&. Access privileges for the database itself are also restored, unless
132 When this option is used, the database named with
134 is used only to issue the initial
137 \fBCREATE DATABASE\fR
138 commands\&. All data is restored into the database name that appears in the archive\&.
141 \fB\-d \fR\fB\fIdbname\fR\fR
143 \fB\-\-dbname=\fR\fB\fIdbname\fR\fR
147 and restore directly into the database\&. The
150 connection string\&. If so, connection string parameters will override any conflicting command line options\&.
155 \fB\-\-exit\-on\-error\fR
157 Exit if an error is encountered while sending SQL commands to the database\&. The default is to continue and to display a count of errors at the end of the restoration\&.
160 \fB\-f \fR\fB\fIfilename\fR\fR
162 \fB\-\-file=\fR\fB\fIfilename\fR\fR
164 Specify output file for generated script, or for the listing when used with
171 \fB\-F \fR\fB\fIformat\fR\fR
173 \fB\-\-format=\fR\fB\fIformat\fR\fR
175 Specify format of the archive\&. It is not necessary to specify the format, since
177 will determine the format automatically\&. If specified, it can be one of the following:
183 The archive is in the custom format of
191 The archive is a directory archive\&.
204 \fB\-I \fR\fB\fIindex\fR\fR
206 \fB\-\-index=\fR\fB\fIindex\fR\fR
208 Restore definition of named index only\&. Multiple indexes may be specified with multiple
213 \fB\-j \fR\fB\fInumber\-of\-jobs\fR\fR
215 \fB\-\-jobs=\fR\fB\fInumber\-of\-jobs\fR\fR
217 Run the most time\-consuming steps of
219 \(em those that load data, create indexes, or create constraints \(em concurrently, using up to
220 \fInumber\-of\-jobs\fR
221 concurrent sessions\&. This option can dramatically reduce the time to restore a large database to a server running on a multiprocessor machine\&. This option is ignored when emitting a script rather than connecting directly to a database server\&.
223 Each job is one process or one thread, depending on the operating system, and uses a separate connection to the server\&.
225 The optimal value for this option depends on the hardware setup of the server, of the client, and of the network\&. Factors include the number of CPU cores and the disk setup\&. A good place to start is the number of CPU cores on the server, but values larger than that can also lead to faster restore times in many cases\&. Of course, values that are too high will lead to decreased performance because of thrashing\&.
227 Only the custom and directory archive formats are supported with this option\&. The input must be a regular file or directory (not, for example, a pipe or standard input)\&. Also, multiple jobs cannot be used together with the option
228 \fB\-\-single\-transaction\fR\&.
235 List the table of contents of the archive\&. The output of this operation can be used as input to the
237 option\&. Note that if filtering switches such as
242 \fB\-l\fR, they will restrict the items listed\&.
245 \fB\-L \fR\fB\fIlist\-file\fR\fR
247 \fB\-\-use\-list=\fR\fB\fIlist\-file\fR\fR
249 Restore only those archive elements that are listed in
250 \fIlist\-file\fR, and restore them in the order they appear in the file\&. Note that if filtering switches such as
255 \fB\-L\fR, they will further restrict the items restored\&.
258 is normally created by editing the output of a previous
260 operation\&. Lines can be moved or removed, and can also be commented out by placing a semicolon (;) at the start of the line\&. See below for examples\&.
263 \fB\-n \fR\fB\fIschema\fR\fR
265 \fB\-\-schema=\fR\fB\fIschema\fR\fR
267 Restore only objects that are in the named schema\&. Multiple schemas may be specified with multiple
269 switches\&. This can be combined with the
271 option to restore just a specific table\&.
274 \fB\-N \fR\fB\fIschema\fR\fR
276 \fB\-\-exclude\-schema=\fR\fB\fIschema\fR\fR
278 Do not restore objects that are in the named schema\&. Multiple schemas to be excluded may be specified with multiple
286 are given for the same schema name, the
288 switch wins and the schema is excluded\&.
295 Do not output commands to set ownership of objects to match the original database\&. By default,
300 \fBSET SESSION AUTHORIZATION\fR
301 statements to set ownership of created schema elements\&. These statements will fail unless the initial connection to the database is made by a superuser (or the same user that owns all of the objects in the script)\&. With
302 \fB\-O\fR, any user name can be used for the initial connection, and this user will own all the created objects\&.
305 \fB\-P \fR\fB\fIfunction\-name(argtype [, \&.\&.\&.])\fR\fR
307 \fB\-\-function=\fR\fB\fIfunction\-name(argtype [, \&.\&.\&.])\fR\fR
309 Restore the named function only\&. Be careful to spell the function name and arguments exactly as they appear in the dump file\*(Aqs table of contents\&. Multiple functions may be specified with multiple
316 \fB\-\-no\-reconnect\fR
318 This option is obsolete but still accepted for backwards compatibility\&.
323 \fB\-\-schema\-only\fR
325 Restore only the schema (data definitions), not data, to the extent that schema entries are present in the archive\&.
327 This option cannot be used with
330 \fB\-\-statistics\-only\fR\&. It is similar to, but for historical reasons not identical to, specifying
331 \fB\-\-section=pre\-data \-\-section=post\-data \-\-no\-statistics\fR\&.
333 (Do not confuse this with the
335 option, which uses the word
337 in a different meaning\&.)
340 \fB\-S \fR\fB\fIusername\fR\fR
342 \fB\-\-superuser=\fR\fB\fIusername\fR\fR
344 Specify the superuser user name to use when disabling triggers\&. This is relevant only if
345 \fB\-\-disable\-triggers\fR
349 \fB\-t \fR\fB\fItable\fR\fR
351 \fB\-\-table=\fR\fB\fItable\fR\fR
353 Restore definition and/or data of only the named table\&. For this purpose,
355 includes views, materialized views, sequences, and foreign tables\&. Multiple tables can be selected by writing multiple
357 switches\&. This option can be combined with the
359 option to specify table(s) in a particular schema\&.
365 .nr an-no-space-flag 1
376 makes no attempt to restore any other database objects that the selected table(s) might depend upon\&. Therefore, there is no guarantee that a specific\-table restore into a clean database will succeed\&.
384 .nr an-no-space-flag 1
391 This flag does not behave identically to the
394 pg_dump\&. There is not currently any provision for wild\-card matching in
395 pg_restore, nor can you include a schema name within its
396 \fB\-t\fR\&. And, while
399 flag will also dump subsidiary objects (such as indexes) of the selected table(s),
402 flag does not include such subsidiary objects\&.
410 .nr an-no-space-flag 1
419 9\&.6, this flag matched only tables, not any other type of relation\&.
424 \fB\-T \fR\fB\fItrigger\fR\fR
426 \fB\-\-trigger=\fR\fB\fItrigger\fR\fR
428 Restore named trigger only\&. Multiple triggers may be specified with multiple
437 Specifies verbose mode\&. This will cause
439 to output detailed object comments and start/stop times to the output file, and progress messages to standard error\&. Repeating the option causes additional debug\-level messages to appear on standard error\&.
453 \fB\-\-no\-privileges\fR
457 Prevent restoration of access privileges (grant/revoke commands)\&.
462 \fB\-\-single\-transaction\fR
464 Execute the restore as a single transaction (that is, wrap the emitted commands in
465 \fBBEGIN\fR/\fBCOMMIT\fR)\&. This ensures that either all the commands complete successfully, or no changes are applied\&. This option implies
466 \fB\-\-exit\-on\-error\fR\&.
469 \fB\-\-disable\-triggers\fR
471 This option is relevant only when performing a restore without schema\&. It instructs
473 to execute commands to temporarily disable triggers on the target tables while the data is restored\&. Use this if you have referential integrity checks or other triggers on the tables that you do not want to invoke during data restore\&.
475 Presently, the commands emitted for
476 \fB\-\-disable\-triggers\fR
477 must be done as superuser\&. So you should also specify a superuser name with
486 \fB\-\-enable\-row\-security\fR
488 This option is relevant only when restoring the contents of a table which has row security\&. By default,
492 to off, to ensure that all data is restored in to the table\&. If the user does not have sufficient privileges to bypass row security, then an error is thrown\&. This parameter instructs
496 to on instead, allowing the user to attempt to restore the contents of the table with row security enabled\&. This might still fail if the user does not have the right to insert the rows from the dump into the table\&.
498 Note that this option currently also requires the dump be in
502 does not support row security\&.
505 \fB\-\-filter=\fR\fB\fIfilename\fR\fR
507 Specify a filename from which to read patterns for objects excluded or included from restore\&. The patterns are interpreted according to the same rules as
508 \fB\-n\fR/\fB\-\-schema\fR
509 for including objects in schemas,
510 \fB\-N\fR/\fB\-\-exclude\-schema\fR
511 for excluding objects in schemas,
512 \fB\-P\fR/\fB\-\-function\fR
513 for restoring named functions,
514 \fB\-I\fR/\fB\-\-index\fR
515 for restoring named indexes,
516 \fB\-t\fR/\fB\-\-table\fR
517 for restoring named tables or
518 \fB\-T\fR/\fB\-\-trigger\fR
519 for restoring triggers\&. To read from
522 as the filename\&. The
524 option can be specified in conjunction with the above listed options for including or excluding objects, and can also be specified more than once for multiple filter files\&.
526 The file lists one database pattern per row, with the following format:
532 { include | exclude } { function | index | schema | table | trigger } \fIPATTERN\fR
538 The first keyword specifies whether the objects matched by the pattern are to be included or excluded\&. The second keyword specifies the type of object to be filtered using the pattern:
548 function: functions, works like the
549 \fB\-P\fR/\fB\-\-function\fR
550 option\&. This keyword can only be used with the
563 index: indexes, works like the
564 \fB\-I\fR/\fB\-\-indexes\fR
565 option\&. This keyword can only be used with the
578 schema: schemas, works like the
579 \fB\-n\fR/\fB\-\-schema\fR
581 \fB\-N\fR/\fB\-\-exclude\-schema\fR
593 table: tables, works like the
594 \fB\-t\fR/\fB\-\-table\fR
595 option\&. This keyword can only be used with the
608 trigger: triggers, works like the
609 \fB\-T\fR/\fB\-\-trigger\fR
610 option\&. This keyword can only be used with the
617 are considered comments and ignored\&. Comments can be placed after an object pattern row as well\&. Blank lines are also ignored\&. See
619 for how to perform quoting in patterns\&.
625 DROP \&.\&.\&. IF EXISTS
626 commands to drop objects in
628 mode\&. This suppresses
629 \(lqdoes not exist\(rq
630 errors that might otherwise be reported\&. This option is not valid unless
635 \fB\-\-no\-comments\fR
637 Do not output commands to restore comments, even if the archive contains them\&.
642 Do not output commands to restore data, even if the archive contains them\&.
645 \fB\-\-no\-data\-for\-failed\-tables\fR
647 By default, table data is restored even if the creation command for the table failed (e\&.g\&., because it already exists)\&. With this option, data for such a table is skipped\&. This behavior is useful if the target database already contains the desired table contents\&. For example, auxiliary tables for
651 might already be loaded in the target database; specifying this option prevents duplicate or obsolete data from being loaded into them\&.
653 This option is effective only when restoring directly into a database, not when producing SQL script output\&.
656 \fB\-\-no\-policies\fR
658 Do not output commands to restore row security policies, even if the archive contains them\&.
661 \fB\-\-no\-publications\fR
663 Do not output commands to restore publications, even if the archive contains them\&.
668 Do not output commands to restore schema (data definitions), even if the archive contains them\&.
671 \fB\-\-no\-security\-labels\fR
673 Do not output commands to restore security labels, even if the archive contains them\&.
676 \fB\-\-no\-statistics\fR
678 Do not output commands to restore statistics, even if the archive contains them\&.
681 \fB\-\-no\-subscriptions\fR
683 Do not output commands to restore subscriptions, even if the archive contains them\&.
686 \fB\-\-no\-table\-access\-method\fR
688 Do not output commands to select table access methods\&. With this option, all objects will be created with whichever table access method is the default during restore\&.
691 \fB\-\-no\-tablespaces\fR
693 Do not output commands to select tablespaces\&. With this option, all objects will be created in whichever tablespace is the default during restore\&.
696 \fB\-\-restrict\-key=\fR\fB\fIrestrict_key\fR\fR
698 Use the provided string as the
701 key in the dump output\&. This can only be specified for SQL script output, i\&.e\&., when the
703 option is used\&. If no restrict key is specified,
705 will generate a random one as needed\&. Keys may contain only alphanumeric characters\&.
707 This option is primarily intended for testing purposes and other scenarios that require repeatable output (e\&.g\&., comparing dump files)\&. It is not recommended for general use, as a malicious server with advance knowledge of the key may be able to inject arbitrary code that will be executed on the machine that runs
709 with the dump output\&.
712 \fB\-\-section=\fR\fB\fIsectionname\fR\fR
714 Only restore the named section\&. The section name can be
717 \fBpost\-data\fR\&. This option can be specified more than once to select multiple sections\&. The default is to restore all sections\&.
719 The data section contains actual table data as well as large\-object definitions\&. Post\-data items consist of definitions of indexes, triggers, rules and constraints other than validated check constraints\&. Pre\-data items consist of all other data definition items\&.
724 Output commands to restore statistics, if the archive contains them\&. This is the default\&.
727 \fB\-\-statistics\-only\fR
729 Restore only the statistics, not schema (data definitions) or data\&.
732 \fB\-\-strict\-names\fR
734 Require that each schema (\fB\-n\fR/\fB\-\-schema\fR) and table (\fB\-t\fR/\fB\-\-table\fR) qualifier match at least one schema/table in the file to be restored\&.
737 \fB\-\-transaction\-size=\fR\fB\fIN\fR\fR
739 Execute the restore as a series of transactions, each processing up to
741 database objects\&. This option implies
742 \fB\-\-exit\-on\-error\fR\&.
744 \fB\-\-transaction\-size\fR
745 offers an intermediate choice between the default behavior (one transaction per SQL command) and
746 \fB\-1\fR/\fB\-\-single\-transaction\fR
747 (one transaction for all restored objects)\&. While
748 \fB\-\-single\-transaction\fR
749 has the least overhead, it may be impractical for large databases because the transaction will take a lock on each restored object, possibly exhausting the server\*(Aqs lock table space\&. Using
750 \fB\-\-transaction\-size\fR
751 with a size of a few thousand objects offers nearly the same performance benefits while capping the amount of lock table space needed\&.
754 \fB\-\-use\-set\-session\-authorization\fR
757 \fBSET SESSION AUTHORIZATION\fR
760 commands to determine object ownership\&. This makes the dump more standards\-compatible, but depending on the history of the objects in the dump, might not restore properly\&.
769 command line arguments, and exit\&.
773 also accepts the following command line arguments for connection parameters:
775 \fB\-h \fR\fB\fIhost\fR\fR
777 \fB\-\-host=\fR\fB\fIhost\fR\fR
779 Specifies the host name of the machine on which the server is running\&. If the value begins with a slash, it is used as the directory for the Unix domain socket\&. The default is taken from the
781 environment variable, if set, else a Unix domain socket connection is attempted\&.
784 \fB\-p \fR\fB\fIport\fR\fR
786 \fB\-\-port=\fR\fB\fIport\fR\fR
788 Specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections\&. Defaults to the
790 environment variable, if set, or a compiled\-in default\&.
793 \fB\-U \fR\fB\fIusername\fR\fR
795 \fB\-\-username=\fR\fB\fIusername\fR\fR
797 User name to connect as\&.
802 \fB\-\-no\-password\fR
804 Never issue a password prompt\&. If the server requires password authentication and a password is not available by other means such as a
806 file, the connection attempt will fail\&. This option can be useful in batch jobs and scripts where no user is present to enter a password\&.
815 to prompt for a password before connecting to a database\&.
817 This option is never essential, since
819 will automatically prompt for a password if the server demands password authentication\&. However,
821 will waste a connection attempt finding out that the server wants a password\&. In some cases it is worth typing
823 to avoid the extra connection attempt\&.
826 \fB\-\-role=\fR\fB\fIrolename\fR\fR
828 Specifies a role name to be used to perform the restore\&. This option causes
833 command after connecting to the database\&. It is useful when the authenticated user (specified by
834 \fB\-U\fR) lacks privileges needed by
835 pg_restore, but can switch to a role with the required rights\&. Some installations have a policy against logging in directly as a superuser, and use of this option allows restores to be performed without violating the policy\&.
847 Default connection parameters
852 Specifies whether to use color in diagnostic messages\&. Possible values are
859 This utility, like most other
861 utilities, also uses the environment variables supported by
864 Section\ \&32.15)\&. However, it does not read
866 when a database name is not supplied\&.
869 When a direct database connection is specified using the
875 statements\&. If you have problems running
876 pg_restore, make sure you are able to select information from the database using, for example,
877 \fBpsql\fR(1)\&. Also, any default connection settings and environment variables used by the
879 front\-end library will apply\&.
882 If your installation has any local additions to the
884 database, be careful to load the output of
886 into a truly empty database; otherwise you are likely to get errors due to duplicate definitions of the added objects\&. To make an empty database without any local additions, copy from
889 template1, for example:
895 CREATE DATABASE foo WITH TEMPLATE template0;
903 are detailed below\&.
913 When restoring data to a pre\-existing table and the option
914 \fB\-\-disable\-triggers\fR
917 emits commands to disable triggers on user tables before inserting the data, then emits commands to re\-enable them after the data has been inserted\&. If the restore is stopped in the middle, the system catalogs might be left in the wrong state\&.
929 cannot restore large objects selectively; for instance, only those for a specific table\&. If an archive contains large objects, then all large objects will be restored, or none of them if they are excluded via
931 \fB\-t\fR, or other options\&.
936 documentation for details on limitations of
941 will restore optimizer statistics if included in the dump file\&. If not all statistics were restored, it may be useful to run
943 on each restored table so the optimizer has useful statistics; see
947 for more information\&.
950 Assume we have dumped a database called
952 into a custom\-format dump file:
958 $ \fBpg_dump \-Fc mydb > db\&.dump\fR
964 To drop the database and recreate it from the dump:
971 $ \fBpg_restore \-C \-d postgres db\&.dump\fR
977 The database named in the
979 switch can be any database existing in the cluster;
981 only uses it to issue the
982 \fBCREATE DATABASE\fR
985 \fB\-C\fR, data is always restored into the database name that appears in the dump file\&.
987 To restore the dump into a new database called
994 $ \fBcreatedb \-T template0 newdb\fR
995 $ \fBpg_restore \-d newdb db\&.dump\fR
1001 Notice we don\*(Aqt use
1002 \fB\-C\fR, and instead connect directly to the database to be restored into\&. Also note that we clone the new database from
1005 template1, to ensure it is initially empty\&.
1007 To reorder database items, it is first necessary to dump the table of contents of the archive:
1013 $ \fBpg_restore \-l db\&.dump > db\&.list\fR
1019 The listing file consists of a header and one line for each item, e\&.g\&.:
1026 ; Archive created at Mon Sep 14 13:55:39 2009
1030 ; Dump Version: 1\&.10\-0
1034 ; Dumped from database version: 8\&.3\&.5
1035 ; Dumped by pg_dump version: 8\&.3\&.8
1038 ; Selected TOC Entries:
1040 3; 2615 2200 SCHEMA \- public pasha
1041 1861; 0 0 COMMENT \- SCHEMA public pasha
1042 1862; 0 0 ACL \- public pasha
1043 317; 1247 17715 TYPE public composite pasha
1044 319; 1247 25899 DOMAIN public domain0 pasha
1050 Semicolons start a comment, and the numbers at the start of lines refer to the internal archive ID assigned to each item\&.
1052 Lines in the file can be commented out, deleted, and reordered\&. For example:
1058 10; 145433 TABLE map_resolutions postgres
1059 ;2; 145344 TABLE species postgres
1060 ;4; 145359 TABLE nt_header postgres
1061 6; 145402 TABLE species_records postgres
1062 ;8; 145416 TABLE ss_old postgres
1068 could be used as input to
1070 and would only restore items 10 and 6, in that order:
1076 $ \fBpg_restore \-L db\&.list db\&.dump\fR
1082 \fBpg_dump\fR(1), \fBpg_dumpall\fR(1), \fBpsql\fR(1)