Sub topic 7
Restoring Custom and Directory Dumps with `pg_restore`
Assume you know basic psql, PostgreSQL roles, and database creation on Linux. This chapter focuses on restoring archive formats produced by pg_dump: custom format and directory format. These formats are not plain SQL files, so restore them with pg_restore.
How `pg_restore` works
A custom-format dump is one archive file. A directory-format dump is a directory containing several archive files. Both store database objects and data in a format that pg_restore can inspect and restore selectively.
A restore usually follows this sequence:
The target database must already exist unless you ask pg_restore to create it. The target server must also have compatible extensions, tablespaces, and roles. pg_restore does not restore cluster-wide objects such as roles. Restore those separately with pg_dumpall --globals-only.
A dump can contain ownership commands. If the original role does not exist on the target, use --no-owner or create the role first.
Restore a custom-format dump
Create a custom archive with pg_dump -Fc. Restore it into an empty database like this:
createdb -U postgres mydb_restored
pg_restore -U postgres -d mydb_restored --no-owner mydb.dumpThe -d option names the database receiving the restored objects. The --no-owner option prevents the archive from trying to change object ownership to roles that may not exist on the target.
This example creates the archive:
pg_dump -h localhost -U postgres -Fc -f mydb.dump mydbCustom archives support selective restoration. First list their contents:
pg_restore --list mydb.dump | head -n 20You can save the full list to a text file, remove unwanted entries, and pass the edited file to pg_restore --use-list. You can also restore only selected schemas or tables with options such as --schema and --table.
Restore a directory-format dump
Directory format is required for parallel dumping and restoring. Create and restore one with:
pg_dump -U postgres -Fd -j 4 -f mydb_dir mydb
createdb -U postgres mydb_restored
pg_restore -U postgres -j 4 -d mydb_restored mydb_dirThe -j 4 option uses four worker jobs. More jobs are not always faster. Storage speed, CPU, network bandwidth, and the number of restorable objects limit the benefit.
A directory archive is a directory, not a single file. Copy the complete directory and preserve its contents when moving it between systems.
Useful restore controls
| Option | Purpose |
|---|---|
| `--clean` | Drop database objects before recreating them |
| `--if-exists` | Suppress errors when objects for `--clean` are absent |
| `--no-owner` | Skip original ownership commands |
| `--no-acl` | Skip access-control list commands |
| `--schema=name` | Restore only one schema |
| `--table=name` | Restore only one table |
| `--exit-on-error` | Stop after the first restore error |
--clean is useful for a controlled refresh, but it can remove objects. Use it only when the target is confirmed and disposable or when the replacement procedure is documented.
Restore cluster-wide roles first
If the dump refers to application roles, restore the global objects before restoring the database:
pg_dumpall -U postgres --globals-only > globals.sql
psql -U postgres -d postgres -f globals.sqlReview this file before applying it to another environment. It may contain role passwords and permissions that should not be copied unchanged.
Exercise
Why can a custom-format dump restore successfully with --no-owner even when the original application's role does not exist on the target?
Show answer
--no-owner skips commands that assign objects to their original roles. The restoring role creates the objects, and ownership can be changed later if needed.