1PG_RESTORE(1) PostgreSQL 16.1 Documentation PG_RESTORE(1)
2
3
4
6 pg_restore - restore a PostgreSQL database from an archive file created
7 by pg_dump
8
10 pg_restore [connection-option...] [option...] [filename]
11
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
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 Before restoring database objects, issue commands to DROP all the
54 objects that will be restored. This option is useful for
55 overwriting an existing database. If any of the objects do not
56 exist in the destination database, ignorable error messages will be
57 reported, unless --if-exists is also specified.
58
59 -C
60 --create
61 Create the database before restoring into it. If --clean is also
62 specified, drop and recreate the target database before connecting
63 to it.
64
65 With --create, pg_restore also restores the database's comment if
66 any, and any configuration variable settings that are specific to
67 this database, that is, any ALTER DATABASE ... SET ... and ALTER
68 ROLE ... IN DATABASE ... SET ... commands that mention this
69 database. Access privileges for the database itself are also
70 restored, unless --no-acl is specified.
71
72 When this option is used, the database named with -d is used only
73 to issue the initial DROP DATABASE and CREATE DATABASE commands.
74 All data is restored into the database name that appears in the
75 archive.
76
77 -d dbname
78 --dbname=dbname
79 Connect to database dbname and restore directly into the database.
80 The dbname can be a connection string. If so, connection string
81 parameters will override any conflicting command line options.
82
83 -e
84 --exit-on-error
85 Exit if an error is encountered while sending SQL commands to the
86 database. The default is to continue and to display a count of
87 errors at the end of the restoration.
88
89 -f filename
90 --file=filename
91 Specify output file for generated script, or for the listing when
92 used with -l. Use - for stdout.
93
94 -F format
95 --format=format
96 Specify format of the archive. It is not necessary to specify the
97 format, since pg_restore will determine the format automatically.
98 If specified, it can be one of the following:
99
100 c
101 custom
102 The archive is in the custom format of pg_dump.
103
104 d
105 directory
106 The archive is a directory archive.
107
108 t
109 tar
110 The archive is a tar archive.
111
112 -I index
113 --index=index
114 Restore definition of named index only. Multiple indexes may be
115 specified with multiple -I switches.
116
117 -j number-of-jobs
118 --jobs=number-of-jobs
119 Run the most time-consuming steps of pg_restore — those that load
120 data, create indexes, or create constraints — concurrently, using
121 up to number-of-jobs concurrent sessions. This option can
122 dramatically reduce the time to restore a large database to a
123 server running on a multiprocessor machine. This option is ignored
124 when emitting a script rather than connecting directly to a
125 database server.
126
127 Each job is one process or one thread, depending on the operating
128 system, and uses a separate connection to the server.
129
130 The optimal value for this option depends on the hardware setup of
131 the server, of the client, and of the network. Factors include the
132 number of CPU cores and the disk setup. A good place to start is
133 the number of CPU cores on the server, but values larger than that
134 can also lead to faster restore times in many cases. Of course,
135 values that are too high will lead to decreased performance because
136 of thrashing.
137
138 Only the custom and directory archive formats are supported with
139 this option. The input must be a regular file or directory (not,
140 for example, a pipe or standard input). Also, multiple jobs cannot
141 be used together with the option --single-transaction.
142
143 -l
144 --list
145 List the table of contents of the archive. The output of this
146 operation can be used as input to the -L option. Note that if
147 filtering switches such as -n or -t are used with -l, they will
148 restrict the items listed.
149
150 -L list-file
151 --use-list=list-file
152 Restore only those archive elements that are listed in list-file,
153 and restore them in the order they appear in the file. Note that if
154 filtering switches such as -n or -t are used with -L, they will
155 further restrict the items restored.
156
157 list-file is normally created by editing the output of a previous
158 -l operation. Lines can be moved or removed, and can also be
159 commented out by placing a semicolon (;) at the start of the line.
160 See below for examples.
161
162 -n schema
163 --schema=schema
164 Restore only objects that are in the named schema. Multiple schemas
165 may be specified with multiple -n switches. This can be combined
166 with the -t option to restore just a specific table.
167
168 -N schema
169 --exclude-schema=schema
170 Do not restore objects that are in the named schema. Multiple
171 schemas to be excluded may be specified with multiple -N switches.
172
173 When both -n and -N are given for the same schema name, the -N
174 switch wins and the schema is excluded.
175
176 -O
177 --no-owner
178 Do not output commands to set ownership of objects to match the
179 original database. By default, pg_restore issues ALTER OWNER or SET
180 SESSION AUTHORIZATION statements to set ownership of created schema
181 elements. These statements will fail unless the initial connection
182 to the database is made by a superuser (or the same user that owns
183 all of the objects in the script). With -O, any user name can be
184 used for the initial connection, and this user will own all the
185 created objects.
186
187 -P function-name(argtype [, ...])
188 --function=function-name(argtype [, ...])
189 Restore the named function only. Be careful to spell the function
190 name and arguments exactly as they appear in the dump file's table
191 of contents. Multiple functions may be specified with multiple -P
192 switches.
193
194 -R
195 --no-reconnect
196 This option is obsolete but still accepted for backwards
197 compatibility.
198
199 -s
200 --schema-only
201 Restore only the schema (data definitions), not data, to the extent
202 that schema entries are present in the archive.
203
204 This option is the inverse of --data-only. It is similar to, but
205 for historical reasons not identical to, specifying
206 --section=pre-data --section=post-data.
207
208 (Do not confuse this with the --schema option, which uses the word
209 “schema” in a different meaning.)
210
211 -S username
212 --superuser=username
213 Specify the superuser user name to use when disabling triggers.
214 This is relevant only if --disable-triggers is used.
215
216 -t table
217 --table=table
218 Restore definition and/or data of only the named table. For this
219 purpose, “table” includes views, materialized views, sequences, and
220 foreign tables. Multiple tables can be selected by writing multiple
221 -t switches. This option can be combined with the -n option to
222 specify table(s) in a particular schema.
223
224 Note
225 When -t is specified, pg_restore makes no attempt to restore
226 any other database objects that the selected table(s) might
227 depend upon. Therefore, there is no guarantee that a
228 specific-table restore into a clean database will succeed.
229
230 Note
231 This flag does not behave identically to the -t flag of
232 pg_dump. There is not currently any provision for wild-card
233 matching in pg_restore, nor can you include a schema name
234 within its -t. And, while pg_dump's -t flag will also dump
235 subsidiary objects (such as indexes) of the selected table(s),
236 pg_restore's -t flag does not include such subsidiary objects.
237
238 Note
239 In versions prior to PostgreSQL 9.6, this flag matched only
240 tables, not any other type of relation.
241
242 -T trigger
243 --trigger=trigger
244 Restore named trigger only. Multiple triggers may be specified with
245 multiple -T switches.
246
247 -v
248 --verbose
249 Specifies verbose mode. This will cause pg_restore to output
250 detailed object comments and start/stop times to the output file,
251 and progress messages to standard error. Repeating the option
252 causes additional debug-level messages to appear on standard error.
253
254 -V
255 --version
256 Print the pg_restore version and exit.
257
258 -x
259 --no-privileges
260 --no-acl
261 Prevent restoration of access privileges (grant/revoke commands).
262
263 -1
264 --single-transaction
265 Execute the restore as a single transaction (that is, wrap the
266 emitted commands in BEGIN/COMMIT). This ensures that either all the
267 commands complete successfully, or no changes are applied. This
268 option implies --exit-on-error.
269
270 --disable-triggers
271 This option is relevant only when performing a data-only restore.
272 It instructs pg_restore to execute commands to temporarily disable
273 triggers on the target tables while the data is restored. Use this
274 if you have referential integrity checks or other triggers on the
275 tables that you do not want to invoke during data restore.
276
277 Presently, the commands emitted for --disable-triggers must be done
278 as superuser. So you should also specify a superuser name with -S
279 or, preferably, run pg_restore as a PostgreSQL superuser.
280
281 --enable-row-security
282 This option is relevant only when restoring the contents of a table
283 which has row security. By default, pg_restore will set
284 row_security to off, to ensure that all data is restored in to the
285 table. If the user does not have sufficient privileges to bypass
286 row security, then an error is thrown. This parameter instructs
287 pg_restore to set row_security to on instead, allowing the user to
288 attempt to restore the contents of the table with row security
289 enabled. This might still fail if the user does not have the right
290 to insert the rows from the dump into the table.
291
292 Note that this option currently also requires the dump be in INSERT
293 format, as COPY FROM does not support row security.
294
295 --if-exists
296 Use DROP ... IF EXISTS commands to drop objects in --clean mode.
297 This suppresses “does not exist” errors that might otherwise be
298 reported. This option is not valid unless --clean is also
299 specified.
300
301 --no-comments
302 Do not output commands to restore comments, even if the archive
303 contains them.
304
305 --no-data-for-failed-tables
306 By default, table data is restored even if the creation command for
307 the table failed (e.g., because it already exists). With this
308 option, data for such a table is skipped. This behavior is useful
309 if the target database already contains the desired table contents.
310 For example, auxiliary tables for PostgreSQL extensions such as
311 PostGIS might already be loaded in the target database; specifying
312 this option prevents duplicate or obsolete data from being loaded
313 into them.
314
315 This option is effective only when restoring directly into a
316 database, not when producing SQL script output.
317
318 --no-publications
319 Do not output commands to restore publications, even if the archive
320 contains them.
321
322 --no-security-labels
323 Do not output commands to restore security labels, even if the
324 archive contains them.
325
326 --no-subscriptions
327 Do not output commands to restore subscriptions, even if the
328 archive contains them.
329
330 --no-table-access-method
331 Do not output commands to select table access methods. With this
332 option, all objects will be created with whichever access method is
333 the default during restore.
334
335 --no-tablespaces
336 Do not output commands to select tablespaces. With this option, all
337 objects will be created in whichever tablespace is the default
338 during restore.
339
340 --section=sectionname
341 Only restore the named section. The section name can be pre-data,
342 data, or post-data. This option can be specified more than once to
343 select multiple sections. The default is to restore all sections.
344
345 The data section contains actual table data as well as large-object
346 definitions. Post-data items consist of definitions of indexes,
347 triggers, rules and constraints other than validated check
348 constraints. Pre-data items consist of all other data definition
349 items.
350
351 --strict-names
352 Require that each schema (-n/--schema) and table (-t/--table)
353 qualifier match at least one schema/table in the backup file.
354
355 --use-set-session-authorization
356 Output SQL-standard SET SESSION AUTHORIZATION commands instead of
357 ALTER OWNER commands to determine object ownership. This makes the
358 dump more standards-compatible, but depending on the history of the
359 objects in the dump, might not restore properly.
360
361 -?
362 --help
363 Show help about pg_restore command line arguments, and exit.
364
365 pg_restore also accepts the following command line arguments for
366 connection parameters:
367
368 -h host
369 --host=host
370 Specifies the host name of the machine on which the server is
371 running. If the value begins with a slash, it is used as the
372 directory for the Unix domain socket. The default is taken from the
373 PGHOST environment variable, if set, else a Unix domain socket
374 connection is attempted.
375
376 -p port
377 --port=port
378 Specifies the TCP port or local Unix domain socket file extension
379 on which the server is listening for connections. Defaults to the
380 PGPORT environment variable, if set, or a compiled-in default.
381
382 -U username
383 --username=username
384 User name to connect as.
385
386 -w
387 --no-password
388 Never issue a password prompt. If the server requires password
389 authentication and a password is not available by other means such
390 as a .pgpass file, the connection attempt will fail. This option
391 can be useful in batch jobs and scripts where no user is present to
392 enter a password.
393
394 -W
395 --password
396 Force pg_restore to prompt for a password before connecting to a
397 database.
398
399 This option is never essential, since pg_restore will automatically
400 prompt for a password if the server demands password
401 authentication. However, pg_restore will waste a connection attempt
402 finding out that the server wants a password. In some cases it is
403 worth typing -W to avoid the extra connection attempt.
404
405 --role=rolename
406 Specifies a role name to be used to perform the restore. This
407 option causes pg_restore to issue a SET ROLE rolename command after
408 connecting to the database. It is useful when the authenticated
409 user (specified by -U) lacks privileges needed by pg_restore, but
410 can switch to a role with the required rights. Some installations
411 have a policy against logging in directly as a superuser, and use
412 of this option allows restores to be performed without violating
413 the policy.
414
416 PGHOST
417 PGOPTIONS
418 PGPORT
419 PGUSER
420 Default connection parameters
421
422 PG_COLOR
423 Specifies whether to use color in diagnostic messages. Possible
424 values are always, auto and never.
425
426 This utility, like most other PostgreSQL utilities, also uses the
427 environment variables supported by libpq (see Section 34.15). However,
428 it does not read PGDATABASE when a database name is not supplied.
429
431 When a direct database connection is specified using the -d option,
432 pg_restore internally executes SQL statements. If you have problems
433 running pg_restore, make sure you are able to select information from
434 the database using, for example, psql(1). Also, any default connection
435 settings and environment variables used by the libpq front-end library
436 will apply.
437
439 If your installation has any local additions to the template1 database,
440 be careful to load the output of pg_restore into a truly empty
441 database; otherwise you are likely to get errors due to duplicate
442 definitions of the added objects. To make an empty database without any
443 local additions, copy from template0 not template1, for example:
444
445 CREATE DATABASE foo WITH TEMPLATE template0;
446
447 The limitations of pg_restore are detailed below.
448
449 • When restoring data to a pre-existing table and the option
450 --disable-triggers is used, pg_restore emits commands to disable
451 triggers on user tables before inserting the data, then emits
452 commands to re-enable them after the data has been inserted. If the
453 restore is stopped in the middle, the system catalogs might be left
454 in the wrong state.
455
456 • pg_restore cannot restore large objects selectively; for instance,
457 only those for a specific table. If an archive contains large
458 objects, then all large objects will be restored, or none of them
459 if they are excluded via -L, -t, or other options.
460
461 See also the pg_dump(1) documentation for details on limitations of
462 pg_dump.
463
464 Once restored, it is wise to run ANALYZE on each restored table so the
465 optimizer has useful statistics; see Section 25.1.3 and Section 25.1.6
466 for more information.
467
469 Assume we have dumped a database called mydb into a custom-format dump
470 file:
471
472 $ pg_dump -Fc mydb > db.dump
473
474 To drop the database and recreate it from the dump:
475
476 $ dropdb mydb
477 $ pg_restore -C -d postgres db.dump
478
479 The database named in the -d switch can be any database existing in the
480 cluster; pg_restore only uses it to issue the CREATE DATABASE command
481 for mydb. With -C, data is always restored into the database name that
482 appears in the dump file.
483
484 To restore the dump into a new database called newdb:
485
486 $ createdb -T template0 newdb
487 $ pg_restore -d newdb db.dump
488
489 Notice we don't use -C, and instead connect directly to the database to
490 be restored into. Also note that we clone the new database from
491 template0 not template1, to ensure it is initially empty.
492
493 To reorder database items, it is first necessary to dump the table of
494 contents of the archive:
495
496 $ pg_restore -l db.dump > db.list
497
498 The listing file consists of a header and one line for each item, e.g.:
499
500 ;
501 ; Archive created at Mon Sep 14 13:55:39 2009
502 ; dbname: DBDEMOS
503 ; TOC Entries: 81
504 ; Compression: 9
505 ; Dump Version: 1.10-0
506 ; Format: CUSTOM
507 ; Integer: 4 bytes
508 ; Offset: 8 bytes
509 ; Dumped from database version: 8.3.5
510 ; Dumped by pg_dump version: 8.3.8
511 ;
512 ;
513 ; Selected TOC Entries:
514 ;
515 3; 2615 2200 SCHEMA - public pasha
516 1861; 0 0 COMMENT - SCHEMA public pasha
517 1862; 0 0 ACL - public pasha
518 317; 1247 17715 TYPE public composite pasha
519 319; 1247 25899 DOMAIN public domain0 pasha
520
521 Semicolons start a comment, and the numbers at the start of lines refer
522 to the internal archive ID assigned to each item.
523
524 Lines in the file can be commented out, deleted, and reordered. For
525 example:
526
527 10; 145433 TABLE map_resolutions postgres
528 ;2; 145344 TABLE species postgres
529 ;4; 145359 TABLE nt_header postgres
530 6; 145402 TABLE species_records postgres
531 ;8; 145416 TABLE ss_old postgres
532
533 could be used as input to pg_restore and would only restore items 10
534 and 6, in that order:
535
536 $ pg_restore -L db.list db.dump
537
539 pg_dump(1), pg_dumpall(1), psql(1)
540
541
542
543PostgreSQL 16.1 2023 PG_RESTORE(1)