Skip to content

pg_dump and pg_restore — logical backups for PostgreSQL

Produce parallel custom-format dumps with pg_dump, restore selectively with pg_restore, and use directory format for the fastest restores at scale.

pg_dump is the PostgreSQL utility for logical backups — a portable, version-tolerant snapshot of one database. It is the right tool for cross-version restores, for cloning a database into another cluster, and for backing up databases small enough that the restore window is acceptable. This article walks the four output formats, the parallel-dump pattern that compresses an hour to ten minutes, the selective restore, and the limits that push you toward pg_basebackup or pgBackRest for larger workloads.

How to verify

To see whether a dump file is intact and what it contains:

# Custom format — list contents without restoring
pg_restore -l backup.dump | head -30

# Tar or directory — same
pg_restore -l backup.dir/ | head -30

# Plain SQL — first and last lines
head -30 backup.sql
tail -10 backup.sql   # should end with a clean COMMIT

For directory format the directory listing tells you whether the parallel dump completed all tables:

ls -la backup.dir/ | wc -l
# Should equal the number of dumped objects + a TOC file

What’s happening

pg_dump has four output formats: plain SQL (-Fp), custom binary (-Fc), tar (-Ft), and directory (-Fd). Plain SQL is human-readable and replayed with psql. The other three are restored with pg_restore and support selective restore, parallelism, and built-in compression.

The custom format (-Fc) is a single file with internal compression and a TOC; it is the default recommendation for most cases. The directory format (-Fd) is one file per table plus a TOC directory — slower to ship as a single artifact but the only format that supports parallel dump (-j N), which speeds up the backup of a large database by 5-10x on multi-core hosts.

pg_dump operates on a single database. To capture the cluster’s roles, tablespaces, and configuration, use pg_dumpall --globals-only alongside per-database pg_dump invocations.

The procedure

  1. Create a backup role with minimum required privileges.

    CREATE ROLE backup WITH LOGIN PASSWORD 'strong-password';
    GRANT pg_read_all_data TO backup;   -- PG 14+
    -- Or, on older versions: GRANT SELECT ON ALL TABLES IN SCHEMA <each> TO backup;
  2. Store credentials in a .pgpass file (protected, mode 0600):

    # ~/.pgpass
    localhost:5432:*:backup:strong-password
    chmod 600 ~/.pgpass
  3. Take a custom-format dump of a single database.

    pg_dump \
      -h localhost -p 5432 -U backup \
      -d orders \
      -Fc \
      --compress=9 \
      --no-owner --no-privileges \
      -f /var/backups/postgres/orders-$(date +%F).dump

    What each flag does:

    • -Fc — custom format with internal compression.
    • --compress=9 — maximum compression (slower CPU, smaller file).
    • --no-owner --no-privileges — useful when restoring into a cluster where the role mapping differs from source.
  4. Take a parallel directory-format dump for large databases.

    pg_dump \
      -h localhost -p 5432 -U backup \
      -d orders \
      -Fd \
      -j 8 \
      --compress=6 \
      -f /var/backups/postgres/orders-$(date +%F).dir

    -j 8 uses 8 parallel workers. The workers each take a snapshot at the same LSN, so the dump is consistent even though it is parallel.

  5. Capture cluster-level objects (roles, tablespaces) separately.

    sudo -u postgres pg_dumpall --globals-only \
      > /var/backups/postgres/globals-$(date +%F).sql
  6. Ship offsite. Push to object storage in a different region with rclone or restic; encrypt in flight (TLS) and at rest (provider-side encryption or client-side with openssl enc / age).

    rclone copy /var/backups/postgres/ remote:db-backups/postgres/ \
      --include "*.dump" --include "*.dir/*" --include "globals-*.sql"
  7. Restore drill — into a sandbox. Confirm the file restores end-to-end.

    # Custom format
    pg_restore \
      -h sandbox.internal -p 5432 -U postgres \
      -d orders_restored \
      --clean --if-exists \
      -j 4 \
      /var/backups/postgres/orders-$(date +%F).dump
    
    # Directory format with parallelism
    pg_restore \
      -h sandbox.internal -p 5432 -U postgres \
      -d orders_restored \
      -j 8 \
      /var/backups/postgres/orders-$(date +%F).dir

    The target database must exist; create it empty with createdb orders_restored first. Use --clean --if-exists if the target is not empty and you want to drop existing objects.

  8. Selective restore. Restore only specific tables or schemas — use the TOC to choose.

    # List contents
    pg_restore -l /var/backups/postgres/orders-$(date +%F).dump > toc.list
    
    # Edit toc.list to keep only the lines you want, comment the rest with ;
    # Then restore with the filtered TOC
    pg_restore \
      -h sandbox.internal -d orders_restored \
      -L toc.list \
      /var/backups/postgres/orders-$(date +%F).dump

Common pitfalls

  • pg_dump operates on a single database — it does not capture cluster-level objects. Always run pg_dumpall --globals-only alongside, or you lose role definitions on restore.
  • The custom format does not support parallelism on dump (only on restore). For dumps over 50 GB use directory format with -j.
  • A dump taken without --no-owner reproduces the original OWNER statements; restoring into a cluster without those roles fails. Add --no-owner for cross-cluster restores.
  • pg_restore -j N restores tables in parallel but indexes and constraints serialize on the same table — for very large tables, the index build is your bottleneck, not the table copy.
  • Plain SQL dumps (-Fp) are human-readable and version-portable but slow to restore and do not support parallelism — reserve them for migration between major versions or for archival audit.

In the engagements we run, the PostgreSQL backup pipeline is a layered model: pg_dump of small operational databases nightly into encrypted object storage in another region, and pgBackRest for the larger production clusters where physical backups give us PITR. We carry that under Managed Operations — and we drill restores monthly into disposable VMs so the backup-day instructions are battle-tested.