Database Reference
In-Depth Information
Using pg_restore
If you backed up using
pg_dump
and chose a format such as
tar
,
custom
, or
directo
ry
, you can use the versatile
pg_restore
utility to restore.
pg_restore
provides you
with a dizzying array of options and far surpasses any restore utility found in other
database products we've used. Some of its outstanding features are:
• You can perform parallel restores using the
-j
option to control the number of
threads to use. This allows each thread to be restoring a separate table simultane‐
ously, thereby significantly picking up the pace of what could otherwise be a lengthy
process.
• You can use it to generate a table of contents file from your backup file to confirm
what has been backed up. You can also edit this table of contents and use the revised
file to control which objects to restore.
• Just as
pg_dump
allows you to do selective backups of objects to save time,
pg_re
store
allows you to do selective restores, even from within a backup of a full da‐
tabase.
•
pg_restore
is backward-compatible, for the most part. You can back up a database
on an older version of PostgreSQL and restore to a newer version.
See
“Database Restore: pg_restore” on page 198
for a listing of
pg_restore
command
options.
To perform a restore using
pg_restore
, first create the database using SQL:
CREATE
DATABASE
mydb
;
Then restore:
pg_restore --dbname=
mydb
--jobs=4 --verbose
mydb.backup
If the database is the same as the one you backed up, you can create and restore the
database in one step:
pg_restore --dbname=postgres --create --jobs=4 --verbose
mydb.backup
When you use the
--create
option, the database name is always the
name of the one you backed up. You can't rename it. If you're also
using the
--dbname
option, that database name must be different from
the name of the database being restored. We usually just specify the
postgres
database.
If you are running version 9.2 or later, you can take advantage of the
--section
option
to restore just the structure without the data. This is useful if you want to use an existing
database as a template for a new one. To do so, first create the target database:
CREATE
DATABASE
mydb2
;