1 <!-- doc/src/sgml/ref/pg_restore.sgml -->
3 <refentry id="APP-PGRESTORE">
5 <refentrytitle>pg_restore</refentrytitle>
6 <manvolnum>1</manvolnum>
7 <refmiscinfo>Application</refmiscinfo>
11 <refname>pg_restore</refname>
14 restore a <productname>PostgreSQL</productname> database from an
15 archive file created by <application>pg_dump</application>
19 <indexterm zone="app-pgrestore">
20 <primary>pg_restore</primary>
25 <command>pg_restore</command>
26 <arg rep="repeat"><replaceable>connection-option</replaceable></arg>
27 <arg rep="repeat"><replaceable>option</replaceable></arg>
28 <arg><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).
100 <term><option>-c</option></term>
101 <term><option>--clean</option></term>
104 Clean (drop) database objects before recreating them.
110 <term><option>-C</option></term>
111 <term><option>--create</option></term>
114 Create the database before restoring into it. (When this
115 option is used, the database named with <option>-d</option> is
116 used only to issue the initial <command>CREATE DATABASE</>
117 command. All data is restored into the database name that
118 appears in the archive.)
124 <term><option>-d <replaceable class="parameter">dbname</replaceable></option></term>
125 <term><option>--dbname=<replaceable class="parameter">dbname</replaceable></option></term>
128 Connect to database <replaceable
129 class="parameter">dbname</replaceable> and restore directly
136 <term><option>-e</option></term>
137 <term><option>--exit-on-error</option></term>
140 Exit if an error is encountered while sending SQL commands to
141 the database. The default is to continue and to display a count of
142 errors at the end of the restoration.
148 <term><option>-f <replaceable>filename</replaceable></option></term>
149 <term><option>--file=<replaceable>filename</replaceable></option></term>
152 Specify output file for generated script, or for the listing
153 when used with <option>-l</option>. Default is the standard
160 <term><option>-F <replaceable class="parameter">format</replaceable></option></term>
161 <term><option>--format=<replaceable class="parameter">format</replaceable></option></term>
164 Specify format of the archive. It is not necessary to specify
165 the format, since <application>pg_restore</application> will
166 determine the format automatically. If specified, it can be
167 one of the following:
171 <term><literal>c</></term>
172 <term><literal>custom</></term>
175 The archive is in the custom format of
176 <application>pg_dump</application>.
182 <term><literal>d</></term>
183 <term><literal>directory</></term>
186 The archive is a directory archive.
192 <term><literal>t</></term>
193 <term><literal>tar</></term>
196 The archive is a <command>tar</command> archive.
200 </variablelist></para>
205 <term><option>-i</option></term>
206 <term><option>--ignore-version</option></term>
209 A deprecated option that is now ignored.
215 <term><option>-I <replaceable class="parameter">index</replaceable></option></term>
216 <term><option>--index=<replaceable class="parameter">index</replaceable></option></term>
219 Restore definition of named index only.
225 <term><option>-j <replaceable class="parameter">number-of-jobs</replaceable></option></term>
226 <term><option>--jobs=<replaceable class="parameter">number-of-jobs</replaceable></option></term>
229 Run the most time-consuming parts
230 of <application>pg_restore</> — those which load data,
231 create indexes, or create constraints — using multiple
232 concurrent jobs. This option can dramatically reduce the time
233 to restore a large database to a server running on a
234 multiprocessor machine.
238 Each job is one process or one thread, depending on the
239 operating system, and uses a separate connection to the
244 The optimal value for this option depends on the hardware
245 setup of the server, of the client, and of the network.
246 Factors include the number of CPU cores and the disk setup. A
247 good place to start is the number of CPU cores on the server,
248 but values larger than that can also lead to faster restore
249 times in many cases. Of course, values that are too high will
250 lead to decreased performance because of thrashing.
254 Only the custom archive format is supported with this option.
255 The input file must be a regular file (not, for example, a
256 pipe). This option is ignored when emitting a script rather
257 than connecting directly to a database server. Also, multiple
258 jobs cannot be used together with the
259 option <option>--single-transaction</option>.
265 <term><option>-l</option></term>
266 <term><option>--list</option></term>
269 List the contents of the archive. The output of this operation
270 can be used as input to the <option>-L</option> option. Note that
271 if filtering switches such as <option>-n</> or <option>-t</> are
272 used with <option>-l</>, they will restrict the items listed.
278 <term><option>-L <replaceable class="parameter">list-file</replaceable></option></term>
279 <term><option>--use-list=<replaceable class="parameter">list-file</replaceable></option></term>
282 Restore only those archive elements that are listed in <replaceable
283 class="PARAMETER">list-file</replaceable>, and restore them in the
284 order they appear in the file. Note that
285 if filtering switches such as <option>-n</> or <option>-t</> are
286 used with <option>-L</>, they will further restrict the items restored.
288 <para><replaceable class="PARAMETER">list-file</> is normally created by
289 editing the output of a previous <option>-l</> operation.
290 Lines can be moved or removed, and can also
291 be commented out by placing a semicolon (<literal>;</literal>) at the
292 start of the line. See below for examples.
298 <term><option>-n <replaceable class="parameter">namespace</replaceable></option></term>
299 <term><option>--schema=<replaceable class="parameter">schema</replaceable></option></term>
302 Restore only objects that are in the named schema. This can be
303 combined with the <option>-t</option> option to restore just a
310 <term><option>-O</option></term>
311 <term><option>--no-owner</option></term>
314 Do not output commands to set
315 ownership of objects to match the original database.
316 By default, <application>pg_restore</application> issues
317 <command>ALTER OWNER</> or
318 <command>SET SESSION AUTHORIZATION</command>
319 statements to set ownership of created schema elements.
320 These statements will fail unless the initial connection to the
321 database is made by a superuser
322 (or the same user that owns all of the objects in the script).
323 With <option>-O</option>, any user name can be used for the
324 initial connection, and this user will own all the created objects.
330 <term><option>-P <replaceable class="parameter">function-name(argtype [, ...])</replaceable></option></term>
331 <term><option>--function=<replaceable class="parameter">function-name(argtype [, ...])</replaceable></option></term>
334 Restore the named function only. Be careful to spell the function
335 name and arguments exactly as they appear in the dump file's table
342 <term><option>-R</option></term>
343 <term><option>--no-reconnect</option></term>
346 This option is obsolete but still accepted for backwards
353 <term><option>-s</option></term>
354 <term><option>--schema-only</option></term>
357 Restore only the schema (data definitions), not the data (table
358 contents). Current sequence values will not be restored, either.
359 (Do not confuse this with the <option>--schema</> option, which
360 uses the word <quote>schema</> in a different meaning.)
366 <term><option>-S <replaceable class="parameter">username</replaceable></option></term>
367 <term><option>--superuser=<replaceable class="parameter">username</replaceable></option></term>
370 Specify the superuser user name to use when disabling triggers.
371 This is only relevant if <option>--disable-triggers</> is used.
377 <term><option>-t <replaceable class="parameter">table</replaceable></option></term>
378 <term><option>--table=<replaceable class="parameter">table</replaceable></option></term>
381 Restore definition and/or data of named table only. This can be
382 combined with the <option>-n</option> option to specify a schema.
388 <term><option>-T <replaceable class="parameter">trigger</replaceable></option></term>
389 <term><option>--trigger=<replaceable class="parameter">trigger</replaceable></option></term>
392 Restore named trigger only.
398 <term><option>-v</option></term>
399 <term><option>--verbose</option></term>
402 Specifies verbose mode.
408 <term><option>-V</></term>
409 <term><option>--version</></term>
412 Print the <application>pg_restore</application> version and exit.
418 <term><option>-x</option></term>
419 <term><option>--no-privileges</option></term>
420 <term><option>--no-acl</option></term>
423 Prevent restoration of access privileges (grant/revoke commands).
429 <term><option>-1</option></term>
430 <term><option>--single-transaction</option></term>
433 Execute the restore as a single transaction (that is, wrap the
434 emitted commands in <command>BEGIN</>/<command>COMMIT</>). This
435 ensures that either all the commands complete successfully, or no
436 changes are applied. This option implies
437 <option>--exit-on-error</>.
443 <term><option>--disable-triggers</></term>
446 This option is only relevant when performing a data-only restore.
447 It instructs <application>pg_restore</application> to execute commands
448 to temporarily disable triggers on the target tables while
449 the data is reloaded. Use this if you have referential
450 integrity checks or other triggers on the tables that you
451 do not want to invoke during data reload.
455 Presently, the commands emitted for
456 <option>--disable-triggers</> must be done as superuser. So, you
457 should also specify a superuser name with <option>-S</>, or
458 preferably run <application>pg_restore</application> as a
459 <productname>PostgreSQL</> superuser.
465 <term><option>--no-data-for-failed-tables</option></term>
468 By default, table data is restored even if the creation command
469 for the table failed (e.g., because it already exists).
470 With this option, data for such a table is skipped.
471 This behavior is useful if the target database already
472 contains the desired table contents. For example,
473 auxiliary tables for <productname>PostgreSQL</> extensions
474 such as <productname>PostGIS</> might already be loaded in
475 the target database; specifying this option prevents duplicate
476 or obsolete data from being loaded into them.
480 This option is effective only when restoring directly into a
481 database, not when producing SQL script output.
487 <term><option>--no-security-labels</option></term>
490 Do not output commands to restore security labels,
491 even if the archive contains them.
497 <term><option>--no-tablespaces</option></term>
500 Do not output commands to select tablespaces.
501 With this option, all objects will be created in whichever
502 tablespace is the default during restore.
508 <term><option>--use-set-session-authorization</option></term>
511 Output SQL-standard <command>SET SESSION AUTHORIZATION</> commands
512 instead of <command>ALTER OWNER</> commands to determine object
513 ownership. This makes the dump more standards-compatible, but
514 depending on the history of the objects in the dump, might not restore
521 <term><option>-?</></term>
522 <term><option>--help</></term>
525 Show help about <application>pg_restore</application> command line
535 <application>pg_restore</application> also accepts
536 the following command line arguments for connection parameters:
540 <term><option>-h <replaceable class="parameter">host</replaceable></option></term>
541 <term><option>--host=<replaceable class="parameter">host</replaceable></option></term>
544 Specifies the host name of the machine on which the server is
545 running. If the value begins with a slash, it is used as the
546 directory for the Unix domain socket. The default is taken
547 from the <envar>PGHOST</envar> environment variable, if set,
548 else a Unix domain socket connection is attempted.
554 <term><option>-p <replaceable class="parameter">port</replaceable></option></term>
555 <term><option>--port=<replaceable class="parameter">port</replaceable></option></term>
558 Specifies the TCP port or local Unix domain socket file
559 extension on which the server is listening for connections.
560 Defaults to the <envar>PGPORT</envar> environment variable, if
561 set, or a compiled-in default.
567 <term><option>-U <replaceable>username</replaceable></option></term>
568 <term><option>--username=<replaceable class="parameter">username</replaceable></option></term>
571 User name to connect as.
577 <term><option>-w</></term>
578 <term><option>--no-password</></term>
581 Never issue a password prompt. If the server requires
582 password authentication and a password is not available by
583 other means such as a <filename>.pgpass</filename> file, the
584 connection attempt will fail. This option can be useful in
585 batch jobs and scripts where no user is present to enter a
592 <term><option>-W</option></term>
593 <term><option>--password</option></term>
596 Force <application>pg_restore</application> to prompt for a
597 password before connecting to a database.
601 This option is never essential, since
602 <application>pg_restore</application> will automatically prompt
603 for a password if the server demands password authentication.
604 However, <application>pg_restore</application> will waste a
605 connection attempt finding out that the server wants a password.
606 In some cases it is worth typing <option>-W</> to avoid the extra
613 <term><option>--role=<replaceable class="parameter">rolename</replaceable></option></term>
616 Specifies a role name to be used to perform the restore.
617 This option causes <application>pg_restore</> to issue a
618 <command>SET ROLE</> <replaceable class="parameter">rolename</>
619 command after connecting to the database. It is useful when the
620 authenticated user (specified by <option>-U</>) lacks privileges
621 needed by <application>pg_restore</>, but can switch to a role with
622 the required rights. Some installations have a policy against
623 logging in directly as a superuser, and use of this option allows
624 restores to be performed without violating the policy.
635 <title>Environment</title>
639 <term><envar>PGHOST</envar></term>
640 <term><envar>PGOPTIONS</envar></term>
641 <term><envar>PGPORT</envar></term>
642 <term><envar>PGUSER</envar></term>
646 Default connection parameters
653 This utility, like most other <productname>PostgreSQL</> utilities,
654 also uses the environment variables supported by <application>libpq</>
655 (see <xref linkend="libpq-envars">).
661 <refsect1 id="app-pgrestore-diagnostics">
662 <title>Diagnostics</title>
665 When a direct database connection is specified using the
666 <option>-d</option> option, <application>pg_restore</application>
667 internally executes <acronym>SQL</acronym> statements. If you have
668 problems running <application>pg_restore</application>, make sure
669 you are able to select information from the database using, for
670 example, <xref linkend="app-psql">. Also, any default connection
671 settings and environment variables used by the
672 <application>libpq</application> front-end library will apply.
677 <refsect1 id="app-pgrestore-notes">
681 If your installation has any local additions to the
682 <literal>template1</> database, be careful to load the output of
683 <application>pg_restore</application> into a truly empty database;
684 otherwise you are likely to get errors due to duplicate definitions
685 of the added objects. To make an empty database without any local
686 additions, copy from <literal>template0</> not <literal>template1</>, for example:
688 CREATE DATABASE foo WITH TEMPLATE template0;
693 The limitations of <application>pg_restore</application> are detailed below.
698 When restoring data to a pre-existing table and the option
699 <option>--disable-triggers</> is used,
700 <application>pg_restore</application> emits commands
701 to disable triggers on user tables before inserting the data, then emits commands to
702 re-enable them after the data has been inserted. If the restore is stopped in the
703 middle, the system catalogs might be left in the wrong state.
708 <para><application>pg_restore</application> cannot restore large objects
709 selectively; for instance, only those for a specific table. If
710 an archive contains large objects, then all large objects will be
711 restored, or none of them if they are excluded via <option>-L</option>,
712 <option>-t</option>, or other options.
720 See also the <xref linkend="app-pgdump"> documentation for details on
721 limitations of <application>pg_dump</application>.
725 Once restored, it is wise to run <command>ANALYZE</> on each
726 restored table so the optimizer has useful statistics; see
727 <xref linkend="vacuum-for-statistics"> and
728 <xref linkend="autovacuum"> for more information.
734 <refsect1 id="app-pgrestore-examples">
735 <title>Examples</title>
738 Assume we have dumped a database called <literal>mydb</> into a
739 custom-format dump file:
742 <prompt>$</prompt> <userinput>pg_dump -Fc mydb > db.dump</userinput>
747 To drop the database and recreate it from the dump:
750 <prompt>$</prompt> <userinput>dropdb mydb</userinput>
751 <prompt>$</prompt> <userinput>pg_restore -C -d postgres db.dump</userinput>
754 The database named in the <option>-d</> switch can be any database existing
755 in the cluster; <application>pg_restore</> only uses it to issue the
756 <command>CREATE DATABASE</> command for <literal>mydb</>. With
757 <option>-C</>, data is always restored into the database name that appears
762 To reload the dump into a new database called <literal>newdb</>:
765 <prompt>$</prompt> <userinput>createdb -T template0 newdb</userinput>
766 <prompt>$</prompt> <userinput>pg_restore -d newdb db.dump</userinput>
769 Notice we don't use <option>-C</>, and instead connect directly to the
770 database to be restored into. Also note that we clone the new database
771 from <literal>template0</> not <literal>template1</>, to ensure it is
776 To reorder database items, it is first necessary to dump the table of
777 contents of the archive:
779 <prompt>$</prompt> <userinput>pg_restore -l db.dump > db.list</userinput>
781 The listing file consists of a header and one line for each item, e.g.:
784 ; Archive created at Mon Sep 14 13:55:39 2009
788 ; Dump Version: 1.10-0
792 ; Dumped from database version: 8.3.5
793 ; Dumped by pg_dump version: 8.3.8
796 ; Selected TOC Entries:
798 3; 2615 2200 SCHEMA - public pasha
799 1861; 0 0 COMMENT - SCHEMA public pasha
800 1862; 0 0 ACL - public pasha
801 317; 1247 17715 TYPE public composite pasha
802 319; 1247 25899 DOMAIN public domain0 pasha
804 Semicolons start a comment, and the numbers at the start of lines refer to the
805 internal archive ID assigned to each item.
809 Lines in the file can be commented out, deleted, and reordered. For example:
811 10; 145433 TABLE map_resolutions postgres
812 ;2; 145344 TABLE species postgres
813 ;4; 145359 TABLE nt_header postgres
814 6; 145402 TABLE species_records postgres
815 ;8; 145416 TABLE ss_old postgres
817 could be used as input to <application>pg_restore</application> and would only restore
818 items 10 and 6, in that order:
820 <prompt>$</prompt> <userinput>pg_restore -L db.list db.dump</userinput>
826 <title>See Also</title>
828 <simplelist type="inline">
829 <member><xref linkend="app-pgdump"></member>
830 <member><xref linkend="app-pg-dumpall"></member>
831 <member><xref linkend="app-psql"></member>