1PG_RESTORE(1) PostgreSQL 9.2.24 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, --data-only
43 Restore only the data, not the schema (data definitions). Table
44 data, large objects, and sequence values are restored, if present
45 in the archive.
46
47 This option is similar to, but for historical reasons not identical
48 to, specifying --section=data.
49
50 -c, --clean
51 Clean (drop) database objects before recreating them. (This might
52 generate some harmless error messages, if any objects were not
53 present in the destination database.)
54
55 -C, --create
56 Create the database before restoring into it. If --clean is also
57 specified, drop and recreate the target database before connecting
58 to it.
59
60 When this option is used, the database named with -d is used only
61 to issue the initial DROP DATABASE and CREATE DATABASE commands.
62 All data is restored into the database name that appears in the
63 archive.
64
65 -d dbname, --dbname=dbname
66 Connect to database dbname and restore directly into the database.
67
68 -e, --exit-on-error
69 Exit if an error is encountered while sending SQL commands to the
70 database. The default is to continue and to display a count of
71 errors at the end of the restoration.
72
73 -f filename, --file=filename
74 Specify output file for generated script, or for the listing when
75 used with -l. Default is the standard output.
76
77 -F format, --format=format
78 Specify format of the archive. It is not necessary to specify the
79 format, since pg_restore will determine the format automatically.
80 If specified, it can be one of the following:
81
82 c, custom
83 The archive is in the custom format of pg_dump.
84
85 d, directory
86 The archive is a directory archive.
87
88 t, tar
89 The archive is a tar archive.
90
91 -i, --ignore-version
92 A deprecated option that is now ignored.
93
94 -I index, --index=index
95 Restore definition of named index only.
96
97 -j number-of-jobs, --jobs=number-of-jobs
98 Run the most time-consuming parts of pg_restore — those which load
99 data, create indexes, or create constraints — using multiple
100 concurrent jobs. This option can dramatically reduce the time to
101 restore a large database to a server running on a multiprocessor
102 machine.
103
104 Each job is one process or one thread, depending on the operating
105 system, and uses a separate connection to the server.
106
107 The optimal value for this option depends on the hardware setup of
108 the server, of the client, and of the network. Factors include the
109 number of CPU cores and the disk setup. A good place to start is
110 the number of CPU cores on the server, but values larger than that
111 can also lead to faster restore times in many cases. Of course,
112 values that are too high will lead to decreased performance because
113 of thrashing.
114
115 Only the custom archive format is supported with this option. The
116 input file must be a regular file (not, for example, a pipe). This
117 option is ignored when emitting a script rather than connecting
118 directly to a database server. Also, multiple jobs cannot be used
119 together with the option --single-transaction.
120
121 -l, --list
122 List the contents of the archive. The output of this operation can
123 be used as input to the -L option. Note that if filtering switches
124 such as -n or -t are used with -l, they will restrict the items
125 listed.
126
127 -L list-file, --use-list=list-file
128 Restore only those archive elements that are listed in list-file,
129 and restore them in the order they appear in the file. Note that if
130 filtering switches such as -n or -t are used with -L, they will
131 further restrict the items restored.
132
133 list-file is normally created by editing the output of a previous
134 -l operation. Lines can be moved or removed, and can also be
135 commented out by placing a semicolon (;) at the start of the line.
136 See below for examples.
137
138 -n namespace, --schema=schema
139 Restore only objects that are in the named schema. This can be
140 combined with the -t option to restore just a specific table.
141
142 -O, --no-owner
143 Do not output commands to set ownership of objects to match the
144 original database. By default, pg_restore issues ALTER OWNER or SET
145 SESSION AUTHORIZATION statements to set ownership of created schema
146 elements. These statements will fail unless the initial connection
147 to the database is made by a superuser (or the same user that owns
148 all of the objects in the script). With -O, any user name can be
149 used for the initial connection, and this user will own all the
150 created objects.
151
152 -P function-name(argtype [, ...]), --function=function-name(argtype [,
153 ...])
154 Restore the named function only. Be careful to spell the function
155 name and arguments exactly as they appear in the dump file's table
156 of contents.
157
158 -R, --no-reconnect
159 This option is obsolete but still accepted for backwards
160 compatibility.
161
162 -s, --schema-only
163 Restore only the schema (data definitions), not data, to the extent
164 that schema entries are present in the archive.
165
166 This option is the inverse of --data-only. It is similar to, but
167 for historical reasons not identical to, specifying
168 --section=pre-data --section=post-data.
169
170 (Do not confuse this with the --schema option, which uses the word
171 “schema” in a different meaning.)
172
173 -S username, --superuser=username
174 Specify the superuser user name to use when disabling triggers.
175 This is only relevant if --disable-triggers is used.
176
177 -t table, --table=table
178 Restore definition and/or data of named table only. This can be
179 combined with the -n option to specify a schema.
180
181 -T trigger, --trigger=trigger
182 Restore named trigger only.
183
184 -v, --verbose
185 Specifies verbose mode.
186
187 -V, --version
188 Print the pg_restore version and exit.
189
190 -x, --no-privileges, --no-acl
191 Prevent restoration of access privileges (grant/revoke commands).
192
193 -1, --single-transaction
194 Execute the restore as a single transaction (that is, wrap the
195 emitted commands in BEGIN/COMMIT). This ensures that either all the
196 commands complete successfully, or no changes are applied. This
197 option implies --exit-on-error.
198
199 --disable-triggers
200 This option is only relevant when performing a data-only restore.
201 It instructs pg_restore to execute commands to temporarily disable
202 triggers on the target tables while the data is reloaded. Use this
203 if you have referential integrity checks or other triggers on the
204 tables that you do not want to invoke during data reload.
205
206 Presently, the commands emitted for --disable-triggers must be done
207 as superuser. So, you should also specify a superuser name with -S,
208 or preferably run pg_restore as a PostgreSQL superuser.
209
210 --no-data-for-failed-tables
211 By default, table data is restored even if the creation command for
212 the table failed (e.g., because it already exists). With this
213 option, data for such a table is skipped. This behavior is useful
214 if the target database already contains the desired table contents.
215 For example, auxiliary tables for PostgreSQL extensions such as
216 PostGIS might already be loaded in the target database; specifying
217 this option prevents duplicate or obsolete data from being loaded
218 into them.
219
220 This option is effective only when restoring directly into a
221 database, not when producing SQL script output.
222
223 --no-security-labels
224 Do not output commands to restore security labels, even if the
225 archive contains them.
226
227 --no-tablespaces
228 Do not output commands to select tablespaces. With this option, all
229 objects will be created in whichever tablespace is the default
230 during restore.
231
232 --section=sectionname
233 Only restore the named section. The section name can be pre-data,
234 data, or post-data. This option can be specified more than once to
235 select multiple sections. The default is to restore all sections.
236
237 The data section contains actual table data as well as large-object
238 definitions. Post-data items consist of definitions of indexes,
239 triggers, rules and constraints other than validated check
240 constraints. Pre-data items consist of all other data definition
241 items.
242
243 --use-set-session-authorization
244 Output SQL-standard SET SESSION AUTHORIZATION commands instead of
245 ALTER OWNER commands to determine object ownership. This makes the
246 dump more standards-compatible, but depending on the history of the
247 objects in the dump, might not restore properly.
248
249 -?, --help
250 Show help about pg_restore command line arguments, and exit.
251
252 pg_restore also accepts the following command line arguments for
253 connection parameters:
254
255 -h host, --host=host
256 Specifies the host name of the machine on which the server is
257 running. If the value begins with a slash, it is used as the
258 directory for the Unix domain socket. The default is taken from the
259 PGHOST environment variable, if set, else a Unix domain socket
260 connection is attempted.
261
262 -p port, --port=port
263 Specifies the TCP port or local Unix domain socket file extension
264 on which the server is listening for connections. Defaults to the
265 PGPORT environment variable, if set, or a compiled-in default.
266
267 -U username, --username=username
268 User name to connect as.
269
270 -w, --no-password
271 Never issue a password prompt. If the server requires password
272 authentication and a password is not available by other means such
273 as a .pgpass file, the connection attempt will fail. This option
274 can be useful in batch jobs and scripts where no user is present to
275 enter a password.
276
277 -W, --password
278 Force pg_restore to prompt for a password before connecting to a
279 database.
280
281 This option is never essential, since pg_restore will automatically
282 prompt for a password if the server demands password
283 authentication. However, pg_restore will waste a connection attempt
284 finding out that the server wants a password. In some cases it is
285 worth typing -W to avoid the extra connection attempt.
286
287 --role=rolename
288 Specifies a role name to be used to perform the restore. This
289 option causes pg_restore to issue a SET ROLErolename command after
290 connecting to the database. It is useful when the authenticated
291 user (specified by -U) lacks privileges needed by pg_restore, but
292 can switch to a role with the required rights. Some installations
293 have a policy against logging in directly as a superuser, and use
294 of this option allows restores to be performed without violating
295 the policy.
296
298 PGHOST, PGOPTIONS, PGPORT, PGUSER
299 Default connection parameters
300
301 This utility, like most other PostgreSQL utilities, also uses the
302 environment variables supported by libpq (see Section 31.14,
303 “Environment Variables”, in the documentation).
304
306 When a direct database connection is specified using the -d option,
307 pg_restore internally executes SQL statements. If you have problems
308 running pg_restore, make sure you are able to select information from
309 the database using, for example, psql(1). Also, any default connection
310 settings and environment variables used by the libpq front-end library
311 will apply.
312
314 If your installation has any local additions to the template1 database,
315 be careful to load the output of pg_restore into a truly empty
316 database; otherwise you are likely to get errors due to duplicate
317 definitions of the added objects. To make an empty database without any
318 local additions, copy from template0 not template1, for example:
319
320 CREATE DATABASE foo WITH TEMPLATE template0;
321
322 The limitations of pg_restore are detailed below.
323
324 · When restoring data to a pre-existing table and the option
325 --disable-triggers is used, pg_restore emits commands to disable
326 triggers on user tables before inserting the data, then emits
327 commands to re-enable them after the data has been inserted. If the
328 restore is stopped in the middle, the system catalogs might be left
329 in the wrong state.
330
331 · pg_restore cannot restore large objects selectively; for instance,
332 only those for a specific table. If an archive contains large
333 objects, then all large objects will be restored, or none of them
334 if they are excluded via -L, -t, or other options.
335
336 See also the pg_dump(1) documentation for details on limitations of
337 pg_dump.
338
339 Once restored, it is wise to run ANALYZE on each restored table so the
340 optimizer has useful statistics; see Section 23.1.3, “Updating Planner
341 Statistics”, in the documentation and Section 23.1.6, “The Autovacuum
342 Daemon”, in the documentation for more information.
343
345 Assume we have dumped a database called mydb into a custom-format dump
346 file:
347
348 $ pg_dump -Fc mydb > db.dump
349
350 To drop the database and recreate it from the dump:
351
352 $ dropdb mydb
353 $ pg_restore -C -d postgres db.dump
354
355 The database named in the -d switch can be any database existing in the
356 cluster; pg_restore only uses it to issue the CREATE DATABASE command
357 for mydb. With -C, data is always restored into the database name that
358 appears in the dump file.
359
360 To reload the dump into a new database called newdb:
361
362 $ createdb -T template0 newdb
363 $ pg_restore -d newdb db.dump
364
365 Notice we don't use -C, and instead connect directly to the database to
366 be restored into. Also note that we clone the new database from
367 template0 not template1, to ensure it is initially empty.
368
369 To reorder database items, it is first necessary to dump the table of
370 contents of the archive:
371
372 $ pg_restore -l db.dump > db.list
373
374 The listing file consists of a header and one line for each item, e.g.:
375
376 ;
377 ; Archive created at Mon Sep 14 13:55:39 2009
378 ; dbname: DBDEMOS
379 ; TOC Entries: 81
380 ; Compression: 9
381 ; Dump Version: 1.10-0
382 ; Format: CUSTOM
383 ; Integer: 4 bytes
384 ; Offset: 8 bytes
385 ; Dumped from database version: 8.3.5
386 ; Dumped by pg_dump version: 8.3.8
387 ;
388 ;
389 ; Selected TOC Entries:
390 ;
391 3; 2615 2200 SCHEMA - public pasha
392 1861; 0 0 COMMENT - SCHEMA public pasha
393 1862; 0 0 ACL - public pasha
394 317; 1247 17715 TYPE public composite pasha
395 319; 1247 25899 DOMAIN public domain0 pasha
396
397 Semicolons start a comment, and the numbers at the start of lines refer
398 to the internal archive ID assigned to each item.
399
400 Lines in the file can be commented out, deleted, and reordered. For
401 example:
402
403 10; 145433 TABLE map_resolutions postgres
404 ;2; 145344 TABLE species postgres
405 ;4; 145359 TABLE nt_header postgres
406 6; 145402 TABLE species_records postgres
407 ;8; 145416 TABLE ss_old postgres
408
409 could be used as input to pg_restore and would only restore items 10
410 and 6, in that order:
411
412 $ pg_restore -L db.list db.dump
413
415 pg_dump(1), pg_dumpall(1), psql(1)
416
417
418
419PostgreSQL 9.2.24 2017-11-06 PG_RESTORE(1)