Skip to the workspace
Pebellslearn by building
Backups and Recovery  ›  Lesson

Restoring with pg_restore and pg_dumpall

pg_restore 14 minRestore a cluster in order — globals from pg_dumpall with psql, then each database from its pg_dump archive with pg_restore.

postgresql dba / Restoring with pg_restore and pg_dumpall

Explore each scene at your own pace.

01 · The rule

The rule

Restoring with pg_restore and pg_dumpall

It is restore time for real: Pedal's staging server must be rebuilt from production's backups. The team runs pg_restore, and the log fills with role "pedal_owner" does not exist. Every command was the right one; the order was wrong.

The rule

Globals first, then the database, then its contents.

A cluster's backup comes in two parts:

  • pg_dumpall --globals-only writes the cluster-wide objects: roles (with their password hashes, attributes and memberships) and tablespaces. It is plain SQL, restored with psql.
  • pg_dump -Fc per database, restored with pg_restore.

pg_dumpall without the flag dumps globals and every database into one plain SQL file. That is simple for a small cluster but gives up parallel and selective restore, so many teams run pg_dumpall -g plus one pg_dump -Fc per database.

# backup, nightly
pg_dumpall -h db1 -U postgres --globals-only -f globals.sql
pg_dump    -h db1 -U postgres -d pedal -Fc -f pedal.dump

# restore, on the new server
psql -U postgres -f globals.sql                  # 1. roles and tablespaces
pg_restore -U postgres -d postgres --create -j 4 pedal.dump   # 2 and 3

--create tells pg_restore to create the pedal database and then connect to it; the -d postgres is only where it starts. Useful pg_restore options:

  • -j 4: restore with four parallel jobs (custom or directory format).
  • -l: list the archive's table of contents without restoring anything.
  • -t tariffs or -n billing: restore only one table or one schema.
  • --clean --if-exists: drop existing objects before re-creating them.
  • --no-owner: skip ownership, so everything is owned by the role that runs the restore.
  • -f out.sql: write the SQL instead of running it, to inspect it.
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.