1PG_RESTORE(1) PostgreSQL Client Applications PG_RESTORE(1)
2
3
4
6 pg_restore - restore a PostgreSQL database from an archive file cre‐
7 ated by pg_dump
8
9
11 pg_restore [ option... ] [ filename ]
12
14 pg_restore is a utility for restoring a PostgreSQL database from an ar‐
15 chive created by pg_dump(1) in one of the non-plain-text formats. It
16 will issue the commands necessary to reconstruct the database to the
17 state it was in at the time it was saved. The archive files also allow
18 pg_restore to be selective about what is restored, or even to reorder
19 the items prior to being restored. The archive files are designed to be
20 portable across architectures.
21
22 pg_restore can operate in two modes. If a database name is specified,
23 pg_restore connects to that database and restores archive contents
24 directly into the database. Otherwise, a script containing the SQL com‐
25 mands necessary to rebuild the database is created and written to a
26 file or standard output. This script output is equivalent to the plain
27 text output format of pg_dump. Some of the options controlling the
28 output are therefore analogous to pg_dump options.
29
30 Obviously, pg_restore cannot restore information that is not present in
31 the archive file. For instance, if the archive was made using the
32 ``dump data as INSERT commands'' option, pg_restore will not be able to
33 load the data using COPY statements.
34
36 pg_restore accepts the following command line arguments.
37
38 filename
39 Specifies the location of the archive file to be restored. If
40 not specified, the standard input is used.
41
42 -a
43
44 --data-only
45 Restore only the data, not the schema (data definitions).
46
47 -c
48
49 --clean
50 Clean (drop) database objects before recreating them.
51
52 -C
53
54 --create
55 Create the database before restoring into it. (When this option
56 is used, the database named with -d is used only to issue the
57 initial CREATE DATABASE command. All data is restored into the
58 database name that appears in the archive.)
59
60 -d dbname
61
62 --dbname=dbname
63 Connect to database dbname and restore directly into the data‐
64 base.
65
66 -e
67
68 --exit-on-error
69 Exit if an error is encountered while sending SQL commands to
70 the database. The default is to continue and to display a count
71 of errors at the end of the restoration.
72
73 -f filename
74
75 --file=filename
76 Specify output file for generated script, or for the listing
77 when used with -l. Default is the standard output.
78
79 -F format
80
81 --format=format
82 Specify format of the archive. It is not necessary to specify
83 the format, since pg_restore will determine the format automati‐
84 cally. If specified, it can be one of the following:
85
86 t
87
88 tar The archive is a tar archive.
89
90 c
91
92 custom The archive is in the custom format of pg_dump.
93
94 -i
95
96 --ignore-version
97 A deprecated option that is now ignored.
98
99 -I index
100
101 --index=index
102 Restore definition of named index only.
103
104 -j number-of-jobs
105
106 --jobs=number-of-jobs
107 Run the most time-consuming parts of pg_restore — those which
108 load data, create indexes, or create constraints — using multi‐
109 ple concurrent jobs. This option can dramatically reduce the
110 time to restore a large database to a server running on a multi-
111 processor machine.
112
113 Each job is one process or one thread, depending on the operat‐
114 ing system, and uses a separate connection to the server.
115
116 The optimal value for this option depends on the hardware setup
117 of the server, of the client, and of the network. Factors
118 include the number of CPU cores and the disk setup. A good place
119 to start is the number of CPU cores on the server, but values
120 larger than that can also lead to faster restore times in many
121 cases. Of course, values that are too high will lead to decreas‐
122 ing performance because of thrashing.
123
124 Only the custom archive format is supported with this option.
125 The input file must be a regular file (not, for example, a
126 pipe). This option is ignored when emitting a script rather than
127 connecting directly to a database server. Also, multiple jobs
128 cannot be used together with the option --single-transaction.
129
130 -l
131
132 --list List the contents of the archive. The output of this operation
133 can be used as input to the -L option. Note that if filtering
134 switches such as -n or -t are used with -l, they will restrict
135 the items listed.
136
137 -L list-file
138
139 --use-list=list-file
140 Restore only those archive elements that are listed in list-
141 file, and restore them in the order they appear in the file.
142 Note that if filtering switches such as -n or -t are used with
143 -L, they will further restrict the items restored.
144
145 list-file is normally created by editing the output of a previ‐
146 ous -l operation. Lines can be moved or removed, and can also
147 be commented out by placing a semicolon (;) at the start of the
148 line. See below for examples.
149
150 -n namespace
151
152 --schema=schema
153 Restore only objects that are in the named schema. This can be
154 combined with the -t option to restore just a specific table.
155
156 -O
157
158 --no-owner
159 Do not output commands to set ownership of objects to match the
160 original database. By default, pg_restore issues ALTER OWNER or
161 SET SESSION AUTHORIZATION statements to set ownership of created
162 schema elements. These statements will fail unless the initial
163 connection to the database is made by a superuser (or the same
164 user that owns all of the objects in the script). With -O, any
165 user name can be used for the initial connection, and this user
166 will own all the created objects.
167
168 --no-tablespaces
169 Do not output commands to select tablespaces. With this option,
170 all objects will be created in whichever tablespace is the
171 default during restore.
172
173 -P function-name(argtype [, ...])
174
175 --function=function-name(argtype [, ...])
176 Restore the named function only. Be careful to spell the func‐
177 tion name and arguments exactly as they appear in the dump
178 file's table of contents.
179
180 -R
181
182 --no-reconnect
183 This option is obsolete but still accepted for backwards compat‐
184 ibility.
185
186 -s
187
188 --schema-only
189 Restore only the schema (data definitions), not the data (table
190 contents). Sequence current values will not be restored, either.
191 (Do not confuse this with the --schema option, which uses the
192 word ``schema'' in a different meaning.)
193
194 -S username
195
196 --superuser=username
197 Specify the superuser user name to use when disabling triggers.
198 This is only relevant if --disable-triggers is used.
199
200 -t table
201
202 --table=table
203 Restore definition and/or data of named table only.
204
205 -T trigger
206
207 --trigger=trigger
208 Restore named trigger only.
209
210 -v
211
212 --verbose
213 Specifies verbose mode.
214
215 -x
216
217 --no-privileges
218
219 --no-acl
220 Prevent restoration of access privileges (grant/revoke com‐
221 mands).
222
223 --disable-triggers
224 This option is only relevant when performing a data-only
225 restore. It instructs pg_restore to execute commands to tempo‐
226 rarily disable triggers on the target tables while the data is
227 reloaded. Use this if you have referential integrity checks or
228 other triggers on the tables that you do not want to invoke dur‐
229 ing data reload.
230
231 Presently, the commands emitted for --disable-triggers must be
232 done as superuser. So, you should also specify a superuser name
233 with -S, or preferably run pg_restore as a PostgreSQL superuser.
234
235 --use-set-session-authorization
236 Output SQL-standard SET SESSION AUTHORIZATION commands instead
237 of ALTER OWNER commands to determine object ownership. This
238 makes the dump more standards compatible, but depending on the
239 history of the objects in the dump, might not restore properly.
240
241 --no-data-for-failed-tables
242 By default, table data is restored even if the creation command
243 for the table failed (e.g., because it already exists). With
244 this option, data for such a table is skipped. This behavior is
245 useful if the target database already contains the desired table
246 contents. For example, auxiliary tables for PostgreSQL exten‐
247 sions such as PostGIS might already be loaded in the target
248 database; specifying this option prevents duplicate or obsolete
249 data from being loaded into them.
250
251 This option is effective only when restoring directly into a
252 database, not when producing SQL script output.
253
254 -1
255
256 --single-transaction
257 Execute the restore as a single transaction (that is, wrap the
258 emitted commands in BEGIN/COMMIT). This ensures that either all
259 the commands complete successfully, or no changes are applied.
260 This option implies --exit-on-error.
261
262 pg_restore also accepts the following command line arguments for con‐
263 nection parameters:
264
265 -h host
266
267 --host=host
268 Specifies the host name of the machine on which the server is
269 running. If the value begins with a slash, it is used as the
270 directory for the Unix domain socket. The default is taken from
271 the PGHOST environment variable, if set, else a Unix domain
272 socket connection is attempted.
273
274 -p port
275
276 --port=port
277 Specifies the TCP port or local Unix domain socket file exten‐
278 sion on which the server is listening for connections. Defaults
279 to the PGPORT environment variable, if set, or a compiled-in
280 default.
281
282 -U username
283
284 --username=username
285 User name to connect as.
286
287 -w
288
289 --no-password
290 Never issue a password prompt. If the server requires password
291 authentication and a password is not available by other means
292 such as a .pgpass file, the connection attempt will fail. This
293 option can be useful in batch jobs and scripts where no user is
294 present to enter a password.
295
296 -W
297
298 --password
299 Force pg_restore to prompt for a password before connecting to a
300 database.
301
302 This option is never essential, since pg_restore will automati‐
303 cally prompt for a password if the server demands password
304 authentication. However, pg_restore will waste a connection
305 attempt finding out that the server wants a password. In some
306 cases it is worth typing -W to avoid the extra connection
307 attempt.
308
309 --role=rolename
310 Specifies a role name to be used to perform the restore. This
311 option causes pg_restore to issue a SET ROLE rolename command
312 after connecting to the database. It is useful when the authen‐
313 ticated user (specified by -U) lacks privileges needed by
314 pg_restore, but can switch to a role with the required rights.
315 Some installations have a policy against logging in directly as
316 a superuser, and use of this option allows restores to be per‐
317 formed without violating the policy.
318
320 PGHOST
321
322 PGOPTIONS
323
324 PGPORT
325
326 PGUSER Default connection parameters
327
328 This utility, like most other PostgreSQL utilities, also uses the envi‐
329 ronment variables supported by libpq (see in the documentation).
330
332 When a direct database connection is specified using the -d option,
333 pg_restore internally executes SQL statements. If you have problems
334 running pg_restore, make sure you are able to select information from
335 the database using, for example, psql(1). Also, any default connection
336 settings and environment variables used by the libpq front-end library
337 will apply.
338
340 If your installation has any local additions to the template1 database,
341 be careful to load the output of pg_restore into a truly empty data‐
342 base; otherwise you are likely to get errors due to duplicate defini‐
343 tions of the added objects. To make an empty database without any local
344 additions, copy from template0 not template1, for example:
345
346 CREATE DATABASE foo WITH TEMPLATE template0;
347
348
349 The limitations of pg_restore are detailed below.
350
351 · When restoring data to a pre-existing table and the option --disable-
352 triggers is used, pg_restore emits commands to disable triggers on
353 user tables before inserting the data then emits commands to re-
354 enable them after the data has been inserted. If the restore is
355 stopped in the middle, the system catalogs might be left in the wrong
356 state.
357
358 · pg_restore cannot restore large objects selectively, for instance
359 only those for a specific table. If an archive contains large
360 objects, then all large objects will be restored, or none of them if
361 they are excluded via -L, -t, or other options.
362
363 See also the pg_dump(1) documentation for details on limitations of
364 pg_dump.
365
366 Once restored, it is wise to run ANALYZE on each restored table so the
367 optimizer has useful statistics; see in the documentation and in the
368 documentation for more information.
369
371 Assume we have dumped a database called mydb into a custom-format dump
372 file:
373
374 $ pg_dump -Fc mydb > db.dump
375
376
377 To drop the database and recreate it from the dump:
378
379 $ dropdb mydb
380 $ pg_restore -C -d postgres db.dump
381
382 The database named in the -d switch can be any database existing in the
383 cluster; pg_restore only uses it to issue the CREATE DATABASE command
384 for mydb. With -C, data is always restored into the database name that
385 appears in the dump file.
386
387 To reload the dump into a new database called newdb:
388
389 $ createdb -T template0 newdb
390 $ pg_restore -d newdb db.dump
391
392 Notice we don't use -C, and instead connect directly to the database to
393 be restored into. Also note that we clone the new database from tem‐
394 plate0 not template1, to ensure it is initially empty.
395
396 To reorder database items, it is first necessary to dump the table of
397 contents of the archive:
398
399 $ pg_restore -l db.dump > db.list
400
401 The listing file consists of a header and one line for each item, e.g.:
402
403 ;
404 ; Archive created at Fri Jul 28 22:28:36 2000
405 ; dbname: mydb
406 ; TOC Entries: 74
407 ; Compression: 0
408 ; Dump Version: 1.4-0
409 ; Format: CUSTOM
410 ;
411 ;
412 ; Selected TOC Entries:
413 ;
414 2; 145344 TABLE species postgres
415 3; 145344 ACL species
416 4; 145359 TABLE nt_header postgres
417 5; 145359 ACL nt_header
418 6; 145402 TABLE species_records postgres
419 7; 145402 ACL species_records
420 8; 145416 TABLE ss_old postgres
421 9; 145416 ACL ss_old
422 10; 145433 TABLE map_resolutions postgres
423 11; 145433 ACL map_resolutions
424 12; 145443 TABLE hs_old postgres
425 13; 145443 ACL hs_old
426
427 Semicolons start a comment, and the numbers at the start of lines refer
428 to the internal archive ID assigned to each item.
429
430 Lines in the file can be commented out, deleted, and reordered. For
431 example:
432
433 10; 145433 TABLE map_resolutions postgres
434 ;2; 145344 TABLE species postgres
435 ;4; 145359 TABLE nt_header postgres
436 6; 145402 TABLE species_records postgres
437 ;8; 145416 TABLE ss_old postgres
438
439 could be used as input to pg_restore and would only restore items 10
440 and 6, in that order:
441
442 $ pg_restore -L db.list db.dump
443
444
446 pg_dump(1), pg_dumpall(1), psql(1)
447
448
449
450Application 2014-02-17 PG_RESTORE(1)