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>d</></term>
172
<term><literal>directory</></term>
175
The archive is a <command>directory</command> archive.
181
<term><literal>t</></term>
182
<term><literal>tar</></term>
185
The archive is a <command>tar</command> archive.
191
<term><literal>c</></term>
192
<term><literal>custom</></term>
195
The archive is in the custom format of
196
<application>pg_dump</application>.
206
<term><option>-i</option></term>
207
<term><option>--ignore-version</option></term>
210
A deprecated option that is now ignored.
216
<term><option>-I <replaceable class="parameter">index</replaceable></option></term>
217
<term><option>--index=<replaceable class="parameter">index</replaceable></option></term>
220
Restore definition of named index only.
226
<term><option>-j <replaceable class="parameter">number-of-jobs</replaceable></option></term>
227
<term><option>--jobs=<replaceable class="parameter">number-of-jobs</replaceable></option></term>
230
Run the most time-consuming parts
231
of <application>pg_restore</> — those which load data,
232
create indexes, or create constraints — using multiple
233
concurrent jobs. This option can dramatically reduce the time
234
to restore a large database to a server running on a
235
multiprocessor machine.
239
Each job is one process or one thread, depending on the
240
operating system, and uses a separate connection to the
245
The optimal value for this option depends on the hardware
246
setup of the server, of the client, and of the network.
247
Factors include the number of CPU cores and the disk setup. A
248
good place to start is the number of CPU cores on the server,
249
but values larger than that can also lead to faster restore
250
times in many cases. Of course, values that are too high will
251
lead to decreased performance because of thrashing.
255
Only the custom archive format is supported with this option.
256
The input file must be a regular file (not, for example, a
257
pipe). This option is ignored when emitting a script rather
258
than connecting directly to a database server. Also, multiple
259
jobs cannot be used together with the
260
option <option>--single-transaction</option>.
266
<term><option>-l</option></term>
267
<term><option>--list</option></term>
270
List the contents of the archive. The output of this operation
271
can be used as input to the <option>-L</option> option. Note that
272
if filtering switches such as <option>-n</> or <option>-t</> are
273
used with <option>-l</>, they will restrict the items listed.
279
<term><option>-L <replaceable class="parameter">list-file</replaceable></option></term>
280
<term><option>--use-list=<replaceable class="parameter">list-file</replaceable></option></term>
283
Restore only those archive elements that are listed in <replaceable
284
class="PARAMETER">list-file</replaceable>, and restore them in the
285
order they appear in the file. Note that
286
if filtering switches such as <option>-n</> or <option>-t</> are
287
used with <option>-L</>, they will further restrict the items restored.
290
<replaceable class="PARAMETER">list-file</> is normally created by
291
editing the output of a previous <option>-l</> operation.
292
Lines can be moved or removed, and can also
293
be commented out by placing a semicolon (<literal>;</literal>) at the
294
start of the line. See below for examples.
300
<term><option>-n <replaceable class="parameter">namespace</replaceable></option></term>
301
<term><option>--schema=<replaceable class="parameter">schema</replaceable></option></term>
304
Restore only objects that are in the named schema. This can be
305
combined with the <option>-t</option> option to restore just a
312
<term><option>-O</option></term>
313
<term><option>--no-owner</option></term>
316
Do not output commands to set
317
ownership of objects to match the original database.
318
By default, <application>pg_restore</application> issues
319
<command>ALTER OWNER</> or
320
<command>SET SESSION AUTHORIZATION</command>
321
statements to set ownership of created schema elements.
322
These statements will fail unless the initial connection to the
323
database is made by a superuser
324
(or the same user that owns all of the objects in the script).
325
With <option>-O</option>, any user name can be used for the
326
initial connection, and this user will own all the created objects.
332
<term><option>--no-tablespaces</option></term>
335
Do not output commands to select tablespaces.
336
With this option, all objects will be created in whichever
337
tablespace is the default during restore.
343
<term><option>--no-security-label</option></term>
346
Do not output commands to restore security labels,
347
even if the archive contains them.
353
<term><option>-P <replaceable class="parameter">function-name(argtype [, ...])</replaceable></option></term>
354
<term><option>--function=<replaceable class="parameter">function-name(argtype [, ...])</replaceable></option></term>
357
Restore the named function only. Be careful to spell the function
358
name and arguments exactly as they appear in the dump file's table
365
<term><option>-R</option></term>
366
<term><option>--no-reconnect</option></term>
369
This option is obsolete but still accepted for backwards
376
<term><option>-s</option></term>
377
<term><option>--schema-only</option></term>
380
Restore only the schema (data definitions), not the data (table
381
contents). Current sequence values will not be restored, either.
382
(Do not confuse this with the <option>--schema</> option, which
383
uses the word <quote>schema</> in a different meaning.)
389
<term><option>-S <replaceable class="parameter">username</replaceable></option></term>
390
<term><option>--superuser=<replaceable class="parameter">username</replaceable></option></term>
393
Specify the superuser user name to use when disabling triggers.
394
This is only relevant if <option>--disable-triggers</> is used.
400
<term><option>-t <replaceable class="parameter">table</replaceable></option></term>
401
<term><option>--table=<replaceable class="parameter">table</replaceable></option></term>
404
Restore definition and/or data of named table only. This can be
405
combined with the <option>-n</option> option to specify a schema.
411
<term><option>-T <replaceable class="parameter">trigger</replaceable></option></term>
412
<term><option>--trigger=<replaceable class="parameter">trigger</replaceable></option></term>
415
Restore named trigger only.
421
<term><option>-v</option></term>
422
<term><option>--verbose</option></term>
425
Specifies verbose mode.
431
<term><option>-V</></term>
432
<term><option>--version</></term>
435
Print the <application>pg_restore</application> version and exit.
441
<term><option>-x</option></term>
442
<term><option>--no-privileges</option></term>
443
<term><option>--no-acl</option></term>
446
Prevent restoration of access privileges (grant/revoke commands).
452
<term><option>--disable-triggers</></term>
455
This option is only relevant when performing a data-only restore.
456
It instructs <application>pg_restore</application> to execute commands
457
to temporarily disable triggers on the target tables while
458
the data is reloaded. Use this if you have referential
459
integrity checks or other triggers on the tables that you
460
do not want to invoke during data reload.
464
Presently, the commands emitted for
465
<option>--disable-triggers</> must be done as superuser. So, you
466
should also specify a superuser name with <option>-S</>, or
467
preferably run <application>pg_restore</application> as a
468
<productname>PostgreSQL</> superuser.
474
<term><option>--use-set-session-authorization</option></term>
477
Output SQL-standard <command>SET SESSION AUTHORIZATION</> commands
478
instead of <command>ALTER OWNER</> commands to determine object
479
ownership. This makes the dump more standards-compatible, but
480
depending on the history of the objects in the dump, might not restore
487
<term><option>--no-data-for-failed-tables</option></term>
490
By default, table data is restored even if the creation command
491
for the table failed (e.g., because it already exists).
492
With this option, data for such a table is skipped.
493
This behavior is useful if the target database already
494
contains the desired table contents. For example,
495
auxiliary tables for <productname>PostgreSQL</> extensions
496
such as <productname>PostGIS</> might already be loaded in
497
the target database; specifying this option prevents duplicate
498
or obsolete data from being loaded into them.
502
This option is effective only when restoring directly into a
503
database, not when producing SQL script output.
509
<term><option>-1</option></term>
510
<term><option>--single-transaction</option></term>
513
Execute the restore as a single transaction (that is, wrap the
514
emitted commands in <command>BEGIN</>/<command>COMMIT</>). This
515
ensures that either all the commands complete successfully, or no
516
changes are applied. This option implies
517
<option>--exit-on-error</>.
523
<term><option>-?</></term>
524
<term><option>--help</></term>
527
Show help about <application>pg_restore</application> command line
537
<application>pg_restore</application> also accepts
538
the following command line arguments for connection parameters:
542
<term><option>-h <replaceable class="parameter">host</replaceable></option></term>
543
<term><option>--host=<replaceable class="parameter">host</replaceable></option></term>
546
Specifies the host name of the machine on which the server is
547
running. If the value begins with a slash, it is used as the
548
directory for the Unix domain socket. The default is taken
549
from the <envar>PGHOST</envar> environment variable, if set,
550
else a Unix domain socket connection is attempted.
556
<term><option>-p <replaceable class="parameter">port</replaceable></option></term>
557
<term><option>--port=<replaceable class="parameter">port</replaceable></option></term>
560
Specifies the TCP port or local Unix domain socket file
561
extension on which the server is listening for connections.
562
Defaults to the <envar>PGPORT</envar> environment variable, if
563
set, or a compiled-in default.
569
<term><option>-U <replaceable>username</replaceable></option></term>
570
<term><option>--username=<replaceable class="parameter">username</replaceable></option></term>
573
User name to connect as.
579
<term><option>-w</></term>
580
<term><option>--no-password</></term>
583
Never issue a password prompt. If the server requires
584
password authentication and a password is not available by
585
other means such as a <filename>.pgpass</filename> file, the
586
connection attempt will fail. This option can be useful in
587
batch jobs and scripts where no user is present to enter a
594
<term><option>-W</option></term>
595
<term><option>--password</option></term>
598
Force <application>pg_restore</application> to prompt for a
599
password before connecting to a database.
603
This option is never essential, since
604
<application>pg_restore</application> will automatically prompt
605
for a password if the server demands password authentication.
606
However, <application>pg_restore</application> will waste a
607
connection attempt finding out that the server wants a password.
608
In some cases it is worth typing <option>-W</> to avoid the extra
615
<term><option>--role=<replaceable class="parameter">rolename</replaceable></option></term>
618
Specifies a role name to be used to perform the restore.
619
This option causes <application>pg_restore</> to issue a
620
<command>SET ROLE</> <replaceable class="parameter">rolename</>
621
command after connecting to the database. It is useful when the
622
authenticated user (specified by <option>-U</>) lacks privileges
623
needed by <application>pg_restore</>, but can switch to a role with
624
the required rights. Some installations have a policy against
625
logging in directly as a superuser, and use of this option allows
626
restores to be performed without violating the policy.
637
<title>Environment</title>
641
<term><envar>PGHOST</envar></term>
642
<term><envar>PGOPTIONS</envar></term>
643
<term><envar>PGPORT</envar></term>
644
<term><envar>PGUSER</envar></term>
648
Default connection parameters
655
This utility, like most other <productname>PostgreSQL</> utilities,
656
also uses the environment variables supported by <application>libpq</>
657
(see <xref linkend="libpq-envars">).
663
<refsect1 id="app-pgrestore-diagnostics">
664
<title>Diagnostics</title>
667
When a direct database connection is specified using the
668
<option>-d</option> option, <application>pg_restore</application>
669
internally executes <acronym>SQL</acronym> statements. If you have
670
problems running <application>pg_restore</application>, make sure
671
you are able to select information from the database using, for
672
example, <xref linkend="app-psql">. Also, any default connection
673
settings and environment variables used by the
674
<application>libpq</application> front-end library will apply.
679
<refsect1 id="app-pgrestore-notes">
683
If your installation has any local additions to the
684
<literal>template1</> database, be careful to load the output of
685
<application>pg_restore</application> into a truly empty database;
686
otherwise you are likely to get errors due to duplicate definitions
687
of the added objects. To make an empty database without any local
688
additions, copy from <literal>template0</> not <literal>template1</>, for example:
690
CREATE DATABASE foo WITH TEMPLATE template0;
695
The limitations of <application>pg_restore</application> are detailed below.
700
When restoring data to a pre-existing table and the option
701
<option>--disable-triggers</> is used,
702
<application>pg_restore</application> emits commands
703
to disable triggers on user tables before inserting the data, then emits commands to
704
re-enable them after the data has been inserted. If the restore is stopped in the
705
middle, the system catalogs might be left in the wrong state.
711
<application>pg_restore</application> cannot restore large objects
712
selectively; for instance, only those for a specific table. If
713
an archive contains large objects, then all large objects will be
714
restored, or none of them if they are excluded via <option>-L</option>,
715
<option>-t</option>, or other options.
723
See also the <xref linkend="app-pgdump"> documentation for details on
724
limitations of <application>pg_dump</application>.
728
Once restored, it is wise to run <command>ANALYZE</> on each
729
restored table so the optimizer has useful statistics; see
730
<xref linkend="vacuum-for-statistics"> and
731
<xref linkend="autovacuum"> for more information.
737
<refsect1 id="app-pgrestore-examples">
738
<title>Examples</title>
741
Assume we have dumped a database called <literal>mydb</> into a
742
custom-format dump file:
745
<prompt>$</prompt> <userinput>pg_dump -Fc mydb > db.dump</userinput>
750
To drop the database and recreate it from the dump:
753
<prompt>$</prompt> <userinput>dropdb mydb</userinput>
754
<prompt>$</prompt> <userinput>pg_restore -C -d postgres db.dump</userinput>
757
The database named in the <option>-d</> switch can be any database existing
758
in the cluster; <application>pg_restore</> only uses it to issue the
759
<command>CREATE DATABASE</> command for <literal>mydb</>. With
760
<option>-C</>, data is always restored into the database name that appears
765
To reload the dump into a new database called <literal>newdb</>:
768
<prompt>$</prompt> <userinput>createdb -T template0 newdb</userinput>
769
<prompt>$</prompt> <userinput>pg_restore -d newdb db.dump</userinput>
772
Notice we don't use <option>-C</>, and instead connect directly to the
773
database to be restored into. Also note that we clone the new database
774
from <literal>template0</> not <literal>template1</>, to ensure it is
779
To reorder database items, it is first necessary to dump the table of
780
contents of the archive:
782
<prompt>$</prompt> <userinput>pg_restore -l db.dump > db.list</userinput>
784
The listing file consists of a header and one line for each item, e.g.:
787
; Archive created at Mon Sep 14 13:55:39 2009
791
; Dump Version: 1.10-0
795
; Dumped from database version: 8.3.5
796
; Dumped by pg_dump version: 8.3.8
799
; Selected TOC Entries:
801
3; 2615 2200 SCHEMA - public pasha
802
1861; 0 0 COMMENT - SCHEMA public pasha
803
1862; 0 0 ACL - public pasha
804
317; 1247 17715 TYPE public composite pasha
805
319; 1247 25899 DOMAIN public domain0 pasha
807
Semicolons start a comment, and the numbers at the start of lines refer to the
808
internal archive ID assigned to each item.
812
Lines in the file can be commented out, deleted, and reordered. For example:
814
10; 145433 TABLE map_resolutions postgres
815
;2; 145344 TABLE species postgres
816
;4; 145359 TABLE nt_header postgres
817
6; 145402 TABLE species_records postgres
818
;8; 145416 TABLE ss_old postgres
820
could be used as input to <application>pg_restore</application> and would only restore
821
items 10 and 6, in that order:
823
<prompt>$</prompt> <userinput>pg_restore -L db.list db.dump</userinput>
830
<title>See Also</title>
832
<simplelist type="inline">
833
<member><xref linkend="app-pgdump"></member>
834
<member><xref linkend="app-pg-dumpall"></member>
835
<member><xref linkend="app-psql"></member>