Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

Before any restore, write the three steps as a checklist — globals, database, contents — and for each, name the file and the tool. If any line is blank, you have found the gap before it bites.

Rebuilding stagingStep 1 of 4
  1. Globals

    globals.sql with psql. Roles such as pedal_owner and analysts now exist.

The trap is restoring production's globals onto a server that already has roles with the same names, such as a shared staging box. The existing roles cause errors, and production password hashes arrive with the others. For staging, consider --no-owner and creating a small set of staging roles by hand.

Drills

Answer fast — trust the rule.

quick drill

Which command backs up roles and tablespaces only?

quick drill

How do you restore a plain-SQL file made by pg_dump or pg_dumpall?

quick drill

You need only the tariffs table from pedal.dump. What do you run?

quick drill

A restore prints role "analysts" does not exist for every GRANT. What was skipped?

Remember

  • Globals first, then the database, then its contents.
  • pg_dumpall -g with psql for roles; pg_dump -Fc with pg_restore for each database.
  • -l lists, -t picks, -j speeds up, --create makes the database.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWrite the restore runbook

Write the steps to rebuild the pedal database on an empty server from globals.sql and pedal.dump. For each step name the tool, and explain what goes wrong if the order is changed.