1PG_RESTORE(1)            PostgreSQL 11.6 Documentation           PG_RESTORE(1)
2
3
4

NAME

6       pg_restore - restore a PostgreSQL database from an archive file created
7       by pg_dump
8

SYNOPSIS

10       pg_restore [connection-option...] [option...] [filename]
11

DESCRIPTION

13       pg_restore is a utility for restoring a PostgreSQL database from an
14       archive created by pg_dump(1) in one of the non-plain-text formats. It
15       will issue the commands necessary to reconstruct the database to the
16       state it was in at the time it was saved. The archive files also allow
17       pg_restore to be selective about what is restored, or even to reorder
18       the items prior to being restored. The archive files are designed to be
19       portable across architectures.
20
21       pg_restore can operate in two modes. If a database name is specified,
22       pg_restore connects to that database and restores archive contents
23       directly into the database. Otherwise, a script containing the SQL
24       commands necessary to rebuild the database is created and written to a
25       file or standard output. This script output is equivalent to the plain
26       text output format of pg_dump. Some of the options controlling the
27       output are therefore analogous to pg_dump options.
28
29       Obviously, pg_restore cannot restore information that is not present in
30       the archive file. For instance, if the archive was made using the “dump
31       data as INSERT commands” option, pg_restore will not be able to load
32       the data using COPY statements.
33

OPTIONS

35       pg_restore accepts the following command line arguments.
36
37       filename
38           Specifies the location of the archive file (or directory, for a
39           directory-format archive) to be restored. If not specified, the
40           standard input is used.
41
42       -a
43       --data-only
44           Restore only the data, not the schema (data definitions). Table
45           data, large objects, and sequence values are restored, if present
46           in the archive.
47
48           This option is similar to, but for historical reasons not identical
49           to, specifying --section=data.
50
51       -c
52       --clean
53           Clean (drop) database objects before recreating them. (Unless
54           --if-exists is used, this might generate some harmless error
55           messages, if any objects were not present in the destination
56           database.)
57
58       -C
59       --create
60           Create the database before restoring into it. If --clean is also
61           specified, drop and recreate the target database before connecting
62           to it.
63
64           With --create, pg_restore also restores the database's comment if
65           any, and any configuration variable settings that are specific to
66           this database, that is, any ALTER DATABASE ... SET ...  and ALTER
67           ROLE ... IN DATABASE ... SET ...  commands that mention this
68           database. Access privileges for the database itself are also
69           restored, unless --no-acl is specified.
70
71           When this option is used, the database named with -d is used only
72           to issue the initial DROP DATABASE and CREATE DATABASE commands.
73           All data is restored into the database name that appears in the
74           archive.
75
76       -d dbname
77       --dbname=dbname
78           Connect to database dbname and restore directly into the database.
79
80       -e
81       --exit-on-error
82           Exit if an error is encountered while sending SQL commands to the
83           database. The default is to continue and to display a count of
84           errors at the end of the restoration.
85
86       -f filename
87       --file=filename
88           Specify output file for generated script, or for the listing when
89           used with -l. Use - for the standard output, which is also the
90           default.
91
92       -F format
93       --format=format
94           Specify format of the archive. It is not necessary to specify the
95           format, since pg_restore will determine the format automatically.
96           If specified, it can be one of the following:
97
98           c
99           custom
100               The archive is in the custom format of pg_dump.
101
102           d
103           directory
104               The archive is a directory archive.
105
106           t
107           tar
108               The archive is a tar archive.
109
110       -I index
111       --index=index
112           Restore definition of named index only. Multiple indexes may be
113           specified with multiple -I switches.
114
115       -j number-of-jobs
116       --jobs=number-of-jobs
117           Run the most time-consuming parts of pg_restore — those which load
118           data, create indexes, or create constraints — using multiple
119           concurrent jobs. This option can dramatically reduce the time to
120           restore a large database to a server running on a multiprocessor
121           machine.
122
123           Each job is one process or one thread, depending on the operating
124           system, and uses a separate connection to the server.
125
126           The optimal value for this option depends on the hardware setup of
127           the server, of the client, and of the network. Factors include the
128           number of CPU cores and the disk setup. A good place to start is
129           the number of CPU cores on the server, but values larger than that
130           can also lead to faster restore times in many cases. Of course,
131           values that are too high will lead to decreased performance because
132           of thrashing.
133
134           Only the custom and directory archive formats are supported with
135           this option. The input must be a regular file or directory (not,
136           for example, a pipe). This option is ignored when emitting a script
137           rather than connecting directly to a database server. Also,
138           multiple jobs cannot be used together with the option
139           --single-transaction.
140
141       -l
142       --list
143           List the table of contents of the archive. The output of this
144           operation can be used as input to the -L option. Note that if
145           filtering switches such as -n or -t are used with -l, they will
146           restrict the items listed.
147
148       -L list-file
149       --use-list=list-file
150           Restore only those archive elements that are listed in list-file,
151           and restore them in the order they appear in the file. Note that if
152           filtering switches such as -n or -t are used with -L, they will
153           further restrict the items restored.
154
155           list-file is normally created by editing the output of a previous
156           -l operation. Lines can be moved or removed, and can also be
157           commented out by placing a semicolon (;) at the start of the line.
158           See below for examples.
159
160       -n schema
161       --schema=schema
162           Restore only objects that are in the named schema. Multiple schemas
163           may be specified with multiple -n switches. This can be combined
164           with the -t option to restore just a specific table.
165
166       -N schema
167       --exclude-schema=schema
168           Do not restore objects that are in the named schema. Multiple
169           schemas to be excluded may be specified with multiple -N switches.
170
171           When both -n and -N are given for the same schema name, the -N
172           switch wins and the schema is excluded.
173
174       -O
175       --no-owner
176           Do not output commands to set ownership of objects to match the
177           original database. By default, pg_restore issues ALTER OWNER or SET
178           SESSION AUTHORIZATION statements to set ownership of created schema
179           elements. These statements will fail unless the initial connection
180           to the database is made by a superuser (or the same user that owns
181           all of the objects in the script). With -O, any user name can be
182           used for the initial connection, and this user will own all the
183           created objects.
184
185       -P function-name(argtype [, ...])
186       --function=function-name(argtype [, ...])
187           Restore the named function only. Be careful to spell the function
188           name and arguments exactly as they appear in the dump file's table
189           of contents. Multiple functions may be specified with multiple -P
190           switches.
191
192       -R
193       --no-reconnect
194           This option is obsolete but still accepted for backwards
195           compatibility.
196
197       -s
198       --schema-only
199           Restore only the schema (data definitions), not data, to the extent
200           that schema entries are present in the archive.
201
202           This option is the inverse of --data-only. It is similar to, but
203           for historical reasons not identical to, specifying
204           --section=pre-data --section=post-data.
205
206           (Do not confuse this with the --schema option, which uses the word
207           “schema” in a different meaning.)
208
209       -S username
210       --superuser=username
211           Specify the superuser user name to use when disabling triggers.
212           This is relevant only if --disable-triggers is used.
213
214       -t table
215       --table=table
216           Restore definition and/or data of only the named table. For this
217           purpose, “table” includes views, materialized views, sequences, and
218           foreign tables. Multiple tables can be selected by writing multiple
219           -t switches. This option can be combined with the -n option to
220           specify table(s) in a particular schema.
221
222               Note
223               When -t is specified, pg_restore makes no attempt to restore
224               any other database objects that the selected table(s) might
225               depend upon. Therefore, there is no guarantee that a
226               specific-table restore into a clean database will succeed.
227
228               Note
229               This flag does not behave identically to the -t flag of
230               pg_dump. There is not currently any provision for wild-card
231               matching in pg_restore, nor can you include a schema name
232               within its -t. And, while pg_dump's -t flag will also dump
233               subsidiary objects (such as indexes) of the selected table(s),
234               pg_restore's -t flag does not include such subsidiary objects.
235
236               Note
237               In versions prior to PostgreSQL 9.6, this flag matched only
238               tables, not any other type of relation.
239
240       -T trigger
241       --trigger=trigger
242           Restore named trigger only. Multiple triggers may be specified with
243           multiple -T switches.
244
245       -v
246       --verbose
247           Specifies verbose mode.
248
249       -V
250       --version
251           Print the pg_restore version and exit.
252
253       -x
254       --no-privileges
255       --no-acl
256           Prevent restoration of access privileges (grant/revoke commands).
257
258       -1
259       --single-transaction
260           Execute the restore as a single transaction (that is, wrap the
261           emitted commands in BEGIN/COMMIT). This ensures that either all the
262           commands complete successfully, or no changes are applied. This
263           option implies --exit-on-error.
264
265       --disable-triggers
266           This option is relevant only when performing a data-only restore.
267           It instructs pg_restore to execute commands to temporarily disable
268           triggers on the target tables while the data is reloaded. Use this
269           if you have referential integrity checks or other triggers on the
270           tables that you do not want to invoke during data reload.
271
272           Presently, the commands emitted for --disable-triggers must be done
273           as superuser. So you should also specify a superuser name with -S
274           or, preferably, run pg_restore as a PostgreSQL superuser.
275
276       --enable-row-security
277           This option is relevant only when restoring the contents of a table
278           which has row security. By default, pg_restore will set
279           row_security to off, to ensure that all data is restored in to the
280           table. If the user does not have sufficient privileges to bypass
281           row security, then an error is thrown. This parameter instructs
282           pg_restore to set row_security to on instead, allowing the user to
283           attempt to restore the contents of the table with row security
284           enabled. This might still fail if the user does not have the right
285           to insert the rows from the dump into the table.
286
287           Note that this option currently also requires the dump be in INSERT
288           format, as COPY FROM does not support row security.
289
290       --if-exists
291           Use conditional commands (i.e. add an IF EXISTS clause) to drop
292           database objects. This option is not valid unless --clean is also
293           specified.
294
295       --no-comments
296           Do not output commands to restore comments, even if the archive
297           contains them.
298
299       --no-data-for-failed-tables
300           By default, table data is restored even if the creation command for
301           the table failed (e.g., because it already exists). With this
302           option, data for such a table is skipped. This behavior is useful
303           if the target database already contains the desired table contents.
304           For example, auxiliary tables for PostgreSQL extensions such as
305           PostGIS might already be loaded in the target database; specifying
306           this option prevents duplicate or obsolete data from being loaded
307           into them.
308
309           This option is effective only when restoring directly into a
310           database, not when producing SQL script output.
311
312       --no-publications
313           Do not output commands to restore publications, even if the archive
314           contains them.
315
316       --no-security-labels
317           Do not output commands to restore security labels, even if the
318           archive contains them.
319
320       --no-subscriptions
321           Do not output commands to restore subscriptions, even if the
322           archive contains them.
323
324       --no-tablespaces
325           Do not output commands to select tablespaces. With this option, all
326           objects will be created in whichever tablespace is the default
327           during restore.
328
329       --section=sectionname
330           Only restore the named section. The section name can be pre-data,
331           data, or post-data. This option can be specified more than once to
332           select multiple sections. The default is to restore all sections.
333
334           The data section contains actual table data as well as large-object
335           definitions. Post-data items consist of definitions of indexes,
336           triggers, rules and constraints other than validated check
337           constraints. Pre-data items consist of all other data definition
338           items.
339
340       --strict-names
341           Require that each schema (-n/--schema) and table (-t/--table)
342           qualifier match at least one schema/table in the backup file.
343
344       --use-set-session-authorization
345           Output SQL-standard SET SESSION AUTHORIZATION commands instead of
346           ALTER OWNER commands to determine object ownership. This makes the
347           dump more standards-compatible, but depending on the history of the
348           objects in the dump, might not restore properly.
349
350       -?
351       --help
352           Show help about pg_restore command line arguments, and exit.
353
354       pg_restore also accepts the following command line arguments for
355       connection parameters:
356
357       -h host
358       --host=host
359           Specifies the host name of the machine on which the server is
360           running. If the value begins with a slash, it is used as the
361           directory for the Unix domain socket. The default is taken from the
362           PGHOST environment variable, if set, else a Unix domain socket
363           connection is attempted.
364
365       -p port
366       --port=port
367           Specifies the TCP port or local Unix domain socket file extension
368           on which the server is listening for connections. Defaults to the
369           PGPORT environment variable, if set, or a compiled-in default.
370
371       -U username
372       --username=username
373           User name to connect as.
374
375       -w
376       --no-password
377           Never issue a password prompt. If the server requires password
378           authentication and a password is not available by other means such
379           as a .pgpass file, the connection attempt will fail. This option
380           can be useful in batch jobs and scripts where no user is present to
381           enter a password.
382
383       -W
384       --password
385           Force pg_restore to prompt for a password before connecting to a
386           database.
387
388           This option is never essential, since pg_restore will automatically
389           prompt for a password if the server demands password
390           authentication. However, pg_restore will waste a connection attempt
391           finding out that the server wants a password. In some cases it is
392           worth typing -W to avoid the extra connection attempt.
393
394       --role=rolename
395           Specifies a role name to be used to perform the restore. This
396           option causes pg_restore to issue a SET ROLE rolename command after
397           connecting to the database. It is useful when the authenticated
398           user (specified by -U) lacks privileges needed by pg_restore, but
399           can switch to a role with the required rights. Some installations
400           have a policy against logging in directly as a superuser, and use
401           of this option allows restores to be performed without violating
402           the policy.
403

ENVIRONMENT

405       PGHOST
406       PGOPTIONS
407       PGPORT
408       PGUSER
409           Default connection parameters
410
411       This utility, like most other PostgreSQL utilities, also uses the
412       environment variables supported by libpq (see Section 34.14). However,
413       it does not read PGDATABASE when a database name is not supplied.
414

DIAGNOSTICS

416       When a direct database connection is specified using the -d option,
417       pg_restore internally executes SQL statements. If you have problems
418       running pg_restore, make sure you are able to select information from
419       the database using, for example, psql(1). Also, any default connection
420       settings and environment variables used by the libpq front-end library
421       will apply.
422

NOTES

424       If your installation has any local additions to the template1 database,
425       be careful to load the output of pg_restore into a truly empty
426       database; otherwise you are likely to get errors due to duplicate
427       definitions of the added objects. To make an empty database without any
428       local additions, copy from template0 not template1, for example:
429
430           CREATE DATABASE foo WITH TEMPLATE template0;
431
432       The limitations of pg_restore are detailed below.
433
434       ·   When restoring data to a pre-existing table and the option
435           --disable-triggers is used, pg_restore emits commands to disable
436           triggers on user tables before inserting the data, then emits
437           commands to re-enable them after the data has been inserted. If the
438           restore is stopped in the middle, the system catalogs might be left
439           in the wrong state.
440
441       ·   pg_restore cannot restore large objects selectively; for instance,
442           only those for a specific table. If an archive contains large
443           objects, then all large objects will be restored, or none of them
444           if they are excluded via -L, -t, or other options.
445
446       See also the pg_dump(1) documentation for details on limitations of
447       pg_dump.
448
449       Once restored, it is wise to run ANALYZE on each restored table so the
450       optimizer has useful statistics; see Section 24.1.3 and Section 24.1.6
451       for more information.
452

EXAMPLES

454       Assume we have dumped a database called mydb into a custom-format dump
455       file:
456
457           $ pg_dump -Fc mydb > db.dump
458
459       To drop the database and recreate it from the dump:
460
461           $ dropdb mydb
462           $ pg_restore -C -d postgres db.dump
463
464       The database named in the -d switch can be any database existing in the
465       cluster; pg_restore only uses it to issue the CREATE DATABASE command
466       for mydb. With -C, data is always restored into the database name that
467       appears in the dump file.
468
469       To reload the dump into a new database called newdb:
470
471           $ createdb -T template0 newdb
472           $ pg_restore -d newdb db.dump
473
474       Notice we don't use -C, and instead connect directly to the database to
475       be restored into. Also note that we clone the new database from
476       template0 not template1, to ensure it is initially empty.
477
478       To reorder database items, it is first necessary to dump the table of
479       contents of the archive:
480
481           $ pg_restore -l db.dump > db.list
482
483       The listing file consists of a header and one line for each item, e.g.:
484
485           ;
486           ; Archive created at Mon Sep 14 13:55:39 2009
487           ;     dbname: DBDEMOS
488           ;     TOC Entries: 81
489           ;     Compression: 9
490           ;     Dump Version: 1.10-0
491           ;     Format: CUSTOM
492           ;     Integer: 4 bytes
493           ;     Offset: 8 bytes
494           ;     Dumped from database version: 8.3.5
495           ;     Dumped by pg_dump version: 8.3.8
496           ;
497           ;
498           ; Selected TOC Entries:
499           ;
500           3; 2615 2200 SCHEMA - public pasha
501           1861; 0 0 COMMENT - SCHEMA public pasha
502           1862; 0 0 ACL - public pasha
503           317; 1247 17715 TYPE public composite pasha
504           319; 1247 25899 DOMAIN public domain0 pasha
505
506       Semicolons start a comment, and the numbers at the start of lines refer
507       to the internal archive ID assigned to each item.
508
509       Lines in the file can be commented out, deleted, and reordered. For
510       example:
511
512           10; 145433 TABLE map_resolutions postgres
513           ;2; 145344 TABLE species postgres
514           ;4; 145359 TABLE nt_header postgres
515           6; 145402 TABLE species_records postgres
516           ;8; 145416 TABLE ss_old postgres
517
518       could be used as input to pg_restore and would only restore items 10
519       and 6, in that order:
520
521           $ pg_restore -L db.list db.dump
522

SEE ALSO

524       pg_dump(1), pg_dumpall(1), psql(1)
525
526
527
528PostgreSQL 11.6                      2019                        PG_RESTORE(1)
Impressum