1 <!-- doc/src/sgml/ref/pg_restore.sgml -->
3 <refentry id="APP-PGRESTORE">
4 <indexterm zone="app-pgrestore">
5 <primary>pg_restore</primary>
9 <refentrytitle>pg_restore</refentrytitle>
10 <manvolnum>1</manvolnum>
11 <refmiscinfo>Application</refmiscinfo>
15 <refname>pg_restore</refname>
18 restore a <productname>PostgreSQL</productname> database from an
19 archive file created by <application>pg_dump</application>
25 <command>pg_restore</command>
26 <arg rep="repeat"><replaceable>connection-option</replaceable></arg>
27 <arg rep="repeat"><replaceable>option</replaceable></arg>
28 <arg choice="opt"><replaceable>filename</replaceable></arg>
33 <refsect1 id="app-pgrestore-description">
34 <title>Description</title>
37 <application>pg_restore</application> is a utility for restoring a
38 <productname>PostgreSQL</productname> database from an archive
39 created by <xref linkend="app-pgdump"> in one of the non-plain-text
40 formats. It will issue the commands necessary to reconstruct the
41 database to the state it was in at the time it was saved. The
42 archive files also allow <application>pg_restore</application> to
43 be selective about what is restored, or even to reorder the items
44 prior to being restored. The archive files are designed to be
45 portable across architectures.
49 <application>pg_restore</application> can operate in two modes.
50 If a database name is specified, <application>pg_restore</application>
51 connects to that database and restores archive contents directly into
52 the database. Otherwise, a script containing the SQL
53 commands necessary to rebuild the database is created and written
54 to a file or standard output. This script output is equivalent to
55 the plain text output format of <application>pg_dump</application>.
56 Some of the options controlling the output are therefore analogous to
57 <application>pg_dump</application> options.
61 Obviously, <application>pg_restore</application> cannot restore information
62 that is not present in the archive file. For instance, if the
63 archive was made using the <quote>dump data as
64 <command>INSERT</command> commands</quote> option,
65 <application>pg_restore</application> will not be able to load the data
66 using <command>COPY</command> statements.
70 <refsect1 id="app-pgrestore-options">
71 <title>Options</title>
74 <application>pg_restore</application> accepts the following command
79 <term><replaceable class="parameter">filename</replaceable></term>
82 Specifies the location of the archive file (or directory, for a
83 directory-format archive) to be restored.
84 If not specified, the standard input is used.
90 <term><option>-a</option></term>
91 <term><option>--data-only</option></term>
94 Restore only the data, not the schema (data definitions).
95 Table data, large objects, and sequence values are restored,
96 if present in the archive.
100 This option is similar to, but for historical reasons not identical
101 to, specifying <option>--section=data</>.
107 <term><option>-c</option></term>
108 <term><option>--clean</option></term>
111 Clean (drop) database objects before recreating them.
112 (Unless <option>--if-exists</> is used,
113 this might generate some harmless error messages, if any objects
114 were not present in the destination database.)
120 <term><option>-C</option></term>
121 <term><option>--create</option></term>
124 Create the database before restoring into it.
125 If <option>--clean</option> is also specified, drop and
126 recreate the target database before connecting to it.
130 When this option is used, the database named with <option>-d</option>
131 is used only to issue the initial <command>DROP DATABASE</> and
132 <command>CREATE DATABASE</> commands. All data is restored into the
133 database name that appears in the archive.
139 <term><option>-d <replaceable class="parameter">dbname</replaceable></option></term>
140 <term><option>--dbname=<replaceable class="parameter">dbname</replaceable></option></term>
143 Connect to database <replaceable
144 class="parameter">dbname</replaceable> and restore directly
151 <term><option>-e</option></term>
152 <term><option>--exit-on-error</option></term>
155 Exit if an error is encountered while sending SQL commands to
156 the database. The default is to continue and to display a count of
157 errors at the end of the restoration.
163 <term><option>-f <replaceable>filename</replaceable></option></term>
164 <term><option>--file=<replaceable>filename</replaceable></option></term>
167 Specify output file for generated script, or for the listing
168 when used with <option>-l</option>. Default is the standard
175 <term><option>-F <replaceable class="parameter">format</replaceable></option></term>
176 <term><option>--format=<replaceable class="parameter">format</replaceable></option></term>
179 Specify format of the archive. It is not necessary to specify
180 the format, since <application>pg_restore</application> will
181 determine the format automatically. If specified, it can be
182 one of the following:
186 <term><literal>c</></term>
187 <term><literal>custom</></term>
190 The archive is in the custom format of
191 <application>pg_dump</application>.
197 <term><literal>d</></term>
198 <term><literal>directory</></term>
201 The archive is a directory archive.
207 <term><literal>t</></term>
208 <term><literal>tar</></term>
211 The archive is a <command>tar</command> archive.
215 </variablelist></para>
220 <term><option>-I <replaceable class="parameter">index</replaceable></option></term>
221 <term><option>--index=<replaceable class="parameter">index</replaceable></option></term>
224 Restore definition of named index only. Multiple indexes
225 may be specified with multiple <option>-I</> switches.
231 <term><option>-j <replaceable class="parameter">number-of-jobs</replaceable></option></term>
232 <term><option>--jobs=<replaceable class="parameter">number-of-jobs</replaceable></option></term>
235 Run the most time-consuming parts
236 of <application>pg_restore</> — those which load data,
237 create indexes, or create constraints — using multiple
238 concurrent jobs. This option can dramatically reduce the time
239 to restore a large database to a server running on a
240 multiprocessor machine.
244 Each job is one process or one thread, depending on the
245 operating system, and uses a separate connection to the
250 The optimal value for this option depends on the hardware
251 setup of the server, of the client, and of the network.
252 Factors include the number of CPU cores and the disk setup. A
253 good place to start is the number of CPU cores on the server,
254 but values larger than that can also lead to faster restore
255 times in many cases. Of course, values that are too high will
256 lead to decreased performance because of thrashing.
260 Only the custom and directory archive formats are supported
262 The input must be a regular file or directory (not, for example, a
263 pipe). This option is ignored when emitting a script rather
264 than connecting directly to a database server. Also, multiple
265 jobs cannot be used together with the
266 option <option>--single-transaction</option>.
272 <term><option>-l</option></term>
273 <term><option>--list</option></term>
276 List the contents of the archive. The output of this operation
277 can be used as input to the <option>-L</option> option. Note that
278 if filtering switches such as <option>-n</> or <option>-t</> are
279 used with <option>-l</>, they will restrict the items listed.
285 <term><option>-L <replaceable class="parameter">list-file</replaceable></option></term>
286 <term><option>--use-list=<replaceable class="parameter">list-file</replaceable></option></term>
289 Restore only those archive elements that are listed in <replaceable
290 class="PARAMETER">list-file</replaceable>, and restore them in the
291 order they appear in the file. Note that
292 if filtering switches such as <option>-n</> or <option>-t</> are
293 used with <option>-L</>, they will further restrict the items restored.
295 <para><replaceable class="PARAMETER">list-file</> is normally created by
296 editing the output of a previous <option>-l</> operation.
297 Lines can be moved or removed, and can also
298 be commented out by placing a semicolon (<literal>;</literal>) at the
299 start of the line. See below for examples.
305 <term><option>-n <replaceable class="parameter">schema</replaceable></option></term>
306 <term><option>--schema=<replaceable class="parameter">schema</replaceable></option></term>
309 Restore only objects that are in the named schema. Multiple schemas
310 may be specified with multiple <option>-n</> switches. This can be
311 combined with the <option>-t</option> option to restore just a
318 <term><option>-N <replaceable class="parameter">schema</replaceable></option></term>
319 <term><option>--exclude-schema=<replaceable class="parameter">schema</replaceable></option></term>
322 Do not restore objects that are in the named schema. Multiple schemas
323 to be excluded may be specified with multiple <option>-N</> switches.
327 When both <option>-n</> and <option>-N</> are given for the same
328 schema name, the <option>-N</> switch wins and the schema is excluded.
334 <term><option>-O</option></term>
335 <term><option>--no-owner</option></term>
338 Do not output commands to set
339 ownership of objects to match the original database.
340 By default, <application>pg_restore</application> issues
341 <command>ALTER OWNER</> or
342 <command>SET SESSION AUTHORIZATION</command>
343 statements to set ownership of created schema elements.
344 These statements will fail unless the initial connection to the
345 database is made by a superuser
346 (or the same user that owns all of the objects in the script).
347 With <option>-O</option>, any user name can be used for the
348 initial connection, and this user will own all the created objects.
354 <term><option>-P <replaceable class="parameter">function-name(argtype [, ...])</replaceable></option></term>
355 <term><option>--function=<replaceable class="parameter">function-name(argtype [, ...])</replaceable></option></term>
358 Restore the named function only. Be careful to spell the function
359 name and arguments exactly as they appear in the dump file's table
360 of contents. Multiple functions may be specified with multiple
361 <option>-P</> switches.
367 <term><option>-R</option></term>
368 <term><option>--no-reconnect</option></term>
371 This option is obsolete but still accepted for backwards
378 <term><option>-s</option></term>
379 <term><option>--schema-only</option></term>
382 Restore only the schema (data definitions), not data,
383 to the extent that schema entries are present in the archive.
386 This option is the inverse of <option>--data-only</>.
387 It is similar to, but for historical reasons not identical to,
389 <option>--section=pre-data --section=post-data</>.
392 (Do not confuse this with the <option>--schema</> option, which
393 uses the word <quote>schema</> in a different meaning.)
399 <term><option>-S <replaceable class="parameter">username</replaceable></option></term>
400 <term><option>--superuser=<replaceable class="parameter">username</replaceable></option></term>
403 Specify the superuser user name to use when disabling triggers.
404 This is relevant only if <option>--disable-triggers</> is used.
410 <term><option>-t <replaceable class="parameter">table</replaceable></option></term>
411 <term><option>--table=<replaceable class="parameter">table</replaceable></option></term>
414 Restore definition and/or data of only the named table.
415 For this purpose, <quote>table</> includes views, materialized views,
416 sequences, and foreign tables. Multiple tables
417 can be selected by writing multiple <option>-t</> switches.
418 This option can be combined with the <option>-n</option> option to
419 specify table(s) in a particular schema.
424 When <option>-t</option> is specified, <application>pg_restore</>
425 makes no attempt to restore any other database objects that the
426 selected table(s) might depend upon. Therefore, there is no
427 guarantee that a specific-table restore into a clean database will
434 This flag does not behave identically to the <option>-t</option>
435 flag of <application>pg_dump</application>. There is not currently
436 any provision for wild-card matching in <application>pg_restore</>,
437 nor can you include a schema name within its <option>-t</>.
443 In versions prior to <productname>PostgreSQL</> 9.6, this flag
444 matched only tables, not any other type of relation.
451 <term><option>-T <replaceable class="parameter">trigger</replaceable></option></term>
452 <term><option>--trigger=<replaceable class="parameter">trigger</replaceable></option></term>
455 Restore named trigger only. Multiple triggers may be specified with
456 multiple <option>-T</> switches.
462 <term><option>-v</option></term>
463 <term><option>--verbose</option></term>
466 Specifies verbose mode.
472 <term><option>-V</></term>
473 <term><option>--version</></term>
476 Print the <application>pg_restore</application> version and exit.
482 <term><option>-x</option></term>
483 <term><option>--no-privileges</option></term>
484 <term><option>--no-acl</option></term>
487 Prevent restoration of access privileges (grant/revoke commands).
493 <term><option>-1</option></term>
494 <term><option>--single-transaction</option></term>
497 Execute the restore as a single transaction (that is, wrap the
498 emitted commands in <command>BEGIN</>/<command>COMMIT</>). This
499 ensures that either all the commands complete successfully, or no
500 changes are applied. This option implies
501 <option>--exit-on-error</>.
507 <term><option>--disable-triggers</></term>
510 This option is relevant only when performing a data-only restore.
511 It instructs <application>pg_restore</application> to execute commands
512 to temporarily disable triggers on the target tables while
513 the data is reloaded. Use this if you have referential
514 integrity checks or other triggers on the tables that you
515 do not want to invoke during data reload.
519 Presently, the commands emitted for
520 <option>--disable-triggers</> must be done as superuser. So you
521 should also specify a superuser name with <option>-S</> or,
522 preferably, run <application>pg_restore</application> as a
523 <productname>PostgreSQL</> superuser.
529 <term><option>--enable-row-security</></term>
532 This option is relevant only when restoring the contents of a table
533 which has row security. By default, <application>pg_restore</application> will set
534 <xref linkend="guc-row-security"> to off, to ensure
535 that all data is restored in to the table. If the user does not have
536 sufficient privileges to bypass row security, then an error is thrown.
537 This parameter instructs <application>pg_restore</application> to set
538 <xref linkend="guc-row-security"> to on instead, allowing the user to attempt to restore
539 the contents of the table with row security enabled. This might still
540 fail if the user does not have the right to insert the rows from the
545 Note that this option currently also requires the dump be in <command>INSERT</command>
546 format, as <command>COPY FROM</command> does not support row security.
552 <term><option>--if-exists</option></term>
555 Use conditional commands (i.e. add an <literal>IF EXISTS</literal>
556 clause) when cleaning database objects. This option is not valid
557 unless <option>--clean</> is also specified.
563 <term><option>--no-data-for-failed-tables</option></term>
566 By default, table data is restored even if the creation command
567 for the table failed (e.g., because it already exists).
568 With this option, data for such a table is skipped.
569 This behavior is useful if the target database already
570 contains the desired table contents. For example,
571 auxiliary tables for <productname>PostgreSQL</> extensions
572 such as <productname>PostGIS</> might already be loaded in
573 the target database; specifying this option prevents duplicate
574 or obsolete data from being loaded into them.
578 This option is effective only when restoring directly into a
579 database, not when producing SQL script output.
585 <term><option>--no-publications</option></term>
588 Do not output commands to restore publications, even if the archive
595 <term><option>--no-security-labels</option></term>
598 Do not output commands to restore security labels,
599 even if the archive contains them.
605 <term><option>--no-subscriptions</option></term>
608 Do not output commands to restore subscriptions, even if the archive
615 <term><option>--no-tablespaces</option></term>
618 Do not output commands to select tablespaces.
619 With this option, all objects will be created in whichever
620 tablespace is the default during restore.
626 <term><option>--section=<replaceable class="parameter">sectionname</replaceable></option></term>
629 Only restore the named section. The section name can be
630 <option>pre-data</>, <option>data</>, or <option>post-data</>.
631 This option can be specified more than once to select multiple
632 sections. The default is to restore all sections.
635 The data section contains actual table data as well as large-object
637 Post-data items consist of definitions of indexes, triggers, rules
638 and constraints other than validated check constraints.
639 Pre-data items consist of all other data definition items.
645 <term><option>--strict-names</></term>
648 Require that each schema
649 (<option>-n</option>/<option>--schema</option>) and table
650 (<option>-t</option>/<option>--table</option>) qualifier match at
651 least one schema/table in the backup file.
657 <term><option>--use-set-session-authorization</option></term>
660 Output SQL-standard <command>SET SESSION AUTHORIZATION</> commands
661 instead of <command>ALTER OWNER</> commands to determine object
662 ownership. This makes the dump more standards-compatible, but
663 depending on the history of the objects in the dump, might not restore
670 <term><option>-?</></term>
671 <term><option>--help</></term>
674 Show help about <application>pg_restore</application> command line
684 <application>pg_restore</application> also accepts
685 the following command line arguments for connection parameters:
689 <term><option>-h <replaceable class="parameter">host</replaceable></option></term>
690 <term><option>--host=<replaceable class="parameter">host</replaceable></option></term>
693 Specifies the host name of the machine on which the server is
694 running. If the value begins with a slash, it is used as the
695 directory for the Unix domain socket. The default is taken
696 from the <envar>PGHOST</envar> environment variable, if set,
697 else a Unix domain socket connection is attempted.
703 <term><option>-p <replaceable class="parameter">port</replaceable></option></term>
704 <term><option>--port=<replaceable class="parameter">port</replaceable></option></term>
707 Specifies the TCP port or local Unix domain socket file
708 extension on which the server is listening for connections.
709 Defaults to the <envar>PGPORT</envar> environment variable, if
710 set, or a compiled-in default.
716 <term><option>-U <replaceable>username</replaceable></option></term>
717 <term><option>--username=<replaceable class="parameter">username</replaceable></option></term>
720 User name to connect as.
726 <term><option>-w</></term>
727 <term><option>--no-password</></term>
730 Never issue a password prompt. If the server requires
731 password authentication and a password is not available by
732 other means such as a <filename>.pgpass</filename> file, the
733 connection attempt will fail. This option can be useful in
734 batch jobs and scripts where no user is present to enter a
741 <term><option>-W</option></term>
742 <term><option>--password</option></term>
745 Force <application>pg_restore</application> to prompt for a
746 password before connecting to a database.
750 This option is never essential, since
751 <application>pg_restore</application> will automatically prompt
752 for a password if the server demands password authentication.
753 However, <application>pg_restore</application> will waste a
754 connection attempt finding out that the server wants a password.
755 In some cases it is worth typing <option>-W</> to avoid the extra
762 <term><option>--role=<replaceable class="parameter">rolename</replaceable></option></term>
765 Specifies a role name to be used to perform the restore.
766 This option causes <application>pg_restore</> to issue a
767 <command>SET ROLE</> <replaceable class="parameter">rolename</>
768 command after connecting to the database. It is useful when the
769 authenticated user (specified by <option>-U</>) lacks privileges
770 needed by <application>pg_restore</>, but can switch to a role with
771 the required rights. Some installations have a policy against
772 logging in directly as a superuser, and use of this option allows
773 restores to be performed without violating the policy.
784 <title>Environment</title>
788 <term><envar>PGHOST</envar></term>
789 <term><envar>PGOPTIONS</envar></term>
790 <term><envar>PGPORT</envar></term>
791 <term><envar>PGUSER</envar></term>
795 Default connection parameters
802 This utility, like most other <productname>PostgreSQL</> utilities,
803 also uses the environment variables supported by <application>libpq</>
804 (see <xref linkend="libpq-envars">). However, it does not read
805 <envar>PGDATABASE</envar> when a database name is not supplied.
811 <refsect1 id="app-pgrestore-diagnostics">
812 <title>Diagnostics</title>
815 When a direct database connection is specified using the
816 <option>-d</option> option, <application>pg_restore</application>
817 internally executes <acronym>SQL</acronym> statements. If you have
818 problems running <application>pg_restore</application>, make sure
819 you are able to select information from the database using, for
820 example, <xref linkend="app-psql">. Also, any default connection
821 settings and environment variables used by the
822 <application>libpq</application> front-end library will apply.
827 <refsect1 id="app-pgrestore-notes">
831 If your installation has any local additions to the
832 <literal>template1</> database, be careful to load the output of
833 <application>pg_restore</application> into a truly empty database;
834 otherwise you are likely to get errors due to duplicate definitions
835 of the added objects. To make an empty database without any local
836 additions, copy from <literal>template0</> not <literal>template1</>, for example:
838 CREATE DATABASE foo WITH TEMPLATE template0;
843 The limitations of <application>pg_restore</application> are detailed below.
848 When restoring data to a pre-existing table and the option
849 <option>--disable-triggers</> is used,
850 <application>pg_restore</application> emits commands
851 to disable triggers on user tables before inserting the data, then emits commands to
852 re-enable them after the data has been inserted. If the restore is stopped in the
853 middle, the system catalogs might be left in the wrong state.
858 <para><application>pg_restore</application> cannot restore large objects
859 selectively; for instance, only those for a specific table. If
860 an archive contains large objects, then all large objects will be
861 restored, or none of them if they are excluded via <option>-L</option>,
862 <option>-t</option>, or other options.
870 See also the <xref linkend="app-pgdump"> documentation for details on
871 limitations of <application>pg_dump</application>.
875 Once restored, it is wise to run <command>ANALYZE</> on each
876 restored table so the optimizer has useful statistics; see
877 <xref linkend="vacuum-for-statistics"> and
878 <xref linkend="autovacuum"> for more information.
884 <refsect1 id="app-pgrestore-examples">
885 <title>Examples</title>
888 Assume we have dumped a database called <literal>mydb</> into a
889 custom-format dump file:
892 <prompt>$</prompt> <userinput>pg_dump -Fc mydb > db.dump</userinput>
897 To drop the database and recreate it from the dump:
900 <prompt>$</prompt> <userinput>dropdb mydb</userinput>
901 <prompt>$</prompt> <userinput>pg_restore -C -d postgres db.dump</userinput>
904 The database named in the <option>-d</> switch can be any database existing
905 in the cluster; <application>pg_restore</> only uses it to issue the
906 <command>CREATE DATABASE</> command for <literal>mydb</>. With
907 <option>-C</>, data is always restored into the database name that appears
912 To reload the dump into a new database called <literal>newdb</>:
915 <prompt>$</prompt> <userinput>createdb -T template0 newdb</userinput>
916 <prompt>$</prompt> <userinput>pg_restore -d newdb db.dump</userinput>
919 Notice we don't use <option>-C</>, and instead connect directly to the
920 database to be restored into. Also note that we clone the new database
921 from <literal>template0</> not <literal>template1</>, to ensure it is
926 To reorder database items, it is first necessary to dump the table of
927 contents of the archive:
929 <prompt>$</prompt> <userinput>pg_restore -l db.dump > db.list</userinput>
931 The listing file consists of a header and one line for each item, e.g.:
934 ; Archive created at Mon Sep 14 13:55:39 2009
938 ; Dump Version: 1.10-0
942 ; Dumped from database version: 8.3.5
943 ; Dumped by pg_dump version: 8.3.8
946 ; Selected TOC Entries:
948 3; 2615 2200 SCHEMA - public pasha
949 1861; 0 0 COMMENT - SCHEMA public pasha
950 1862; 0 0 ACL - public pasha
951 317; 1247 17715 TYPE public composite pasha
952 319; 1247 25899 DOMAIN public domain0 pasha
954 Semicolons start a comment, and the numbers at the start of lines refer to the
955 internal archive ID assigned to each item.
959 Lines in the file can be commented out, deleted, and reordered. For example:
961 10; 145433 TABLE map_resolutions postgres
962 ;2; 145344 TABLE species postgres
963 ;4; 145359 TABLE nt_header postgres
964 6; 145402 TABLE species_records postgres
965 ;8; 145416 TABLE ss_old postgres
967 could be used as input to <application>pg_restore</application> and would only restore
968 items 10 and 6, in that order:
970 <prompt>$</prompt> <userinput>pg_restore -L db.list db.dump</userinput>
976 <title>See Also</title>
978 <simplelist type="inline">
979 <member><xref linkend="app-pgdump"></member>
980 <member><xref linkend="app-pg-dumpall"></member>
981 <member><xref linkend="app-psql"></member>