PostgreSQL Backup and Restore Cheat Sheet

pg_dump, pg_restore, pg_dumpall, pg_basebackup, COPY, and migration workflows.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

pg_dump and pg_restore Basics

Core backup and restore commands used constantly.

Custom format dump

Create a compressed custom-format backup archive.

bashANYpg_dumpbackup
bash
pg_dump -h localhost -U postgres -d appdb -F c -f appdb.dump
Notes

See summary for usage details.

Directory format dump

Create a directory-format dump that supports parallel restore.

bashANYpg_dumpbackupparallel
bash
pg_dump -h localhost -U postgres -d appdb -F d -j 4 -f /backups/appdb_dir
Notes

See summary for usage details.

Plain SQL dump

Create a plain-text SQL dump.

bashANYpg_dumpbackupsql
bash
pg_dump -h localhost -U postgres -d appdb -F p -f appdb.sql
Notes

See summary for usage details.

Schema-only dump

Export only DDL without row data.

bashANYpg_dumpschema
bash
pg_dump -h localhost -U postgres -d appdb --schema-only -f schema.sql
Notes

See summary for usage details.

Data-only dump

Export only table data.

bashANYpg_dumpdata
bash
pg_dump -h localhost -U postgres -d appdb --data-only -f data.sql
Notes

See summary for usage details.

Restore archive to database

Restore a custom or directory archive into a database.

bashANYpg_restorerestore
bash
pg_restore -h localhost -U postgres -d appdb appdb.dump
Notes

See summary for usage details.

Restore with clean

Drop objects before recreating them during restore.

bashANYpg_restorerestoreclean
bash
pg_restore -h localhost -U postgres -d appdb --clean --if-exists appdb.dump
Notes

See summary for usage details.

Restore plain SQL dump

Replay a plain-text SQL dump with psql.

bashANYpsqlrestoresql
bash
psql -h localhost -U postgres -d appdb -f appdb.sql
Notes

See summary for usage details.

Cluster-wide Backup and Migration

Commands for globals, all databases, and upgrades.

Dump global objects

Export cluster-wide roles and tablespaces.

bashANYpg_dumpallglobals
bash
pg_dumpall -h localhost -U postgres --globals-only > globals.sql
Notes

See summary for usage details.

Dump entire cluster

Dump all databases in one plain SQL file.

bashANYpg_dumpallcluster
bash
pg_dumpall -h localhost -U postgres > cluster.sql
Notes

See summary for usage details.

Physical base backup

Take a physical base backup suitable for replication or disaster recovery.

bashANYpg_basebackupphysical-backup
bash
pg_basebackup -h primary-db -U replicator -D /backups/base -Fp -Xs -P
Notes

See summary for usage details.

Verify base backup

Verify integrity of a base backup manifest.

bashANYpg_verifybackupbackup
bash
pg_verifybackup /backups/base
Notes

See summary for usage details.

Run pg_upgrade check

Check whether an in-place major upgrade is feasible.

bashANYpg_upgradeupgrade
bash
pg_upgrade --check -b /usr/lib/postgresql/17/bin -B /usr/lib/postgresql/18/bin -d /var/lib/postgresql/17/data -D /var/lib/postgresql/18/data
Notes

See summary for usage details.

Run pg_upgrade

Perform an in-place major version upgrade.

bashANYpg_upgradeupgrade
bash
pg_upgrade -b /usr/lib/postgresql/17/bin -B /usr/lib/postgresql/18/bin -d /var/lib/postgresql/17/data -D /var/lib/postgresql/18/data
Notes

See summary for usage details.

Analyze upgraded cluster

Collect statistics after a fresh restore or upgrade.

bashANYvacuumdbanalyzeupgrade
bash
vacuumdb --all --analyze-in-stages
Notes

See summary for usage details.

Selective Backup and Restore

Dump or restore individual schemas, tables, and objects.

Dump one table

Export one table definition and data.

bashANYpg_dumptable
bash
pg_dump -h localhost -U postgres -d appdb -t public.users -f users.sql
Notes

See summary for usage details.

Dump one schema

Export one schema from a database.

bashANYpg_dumpschema
bash
pg_dump -h localhost -U postgres -d appdb -n accounting -f accounting.sql
Notes

See summary for usage details.

List archive contents

Show archive items without restoring.

bashANYpg_restoreinspect
bash
pg_restore -l appdb.dump
Notes

See summary for usage details.

Restore one table

Restore only a named relation from an archive.

bashANYpg_restoretable
bash
pg_restore -h localhost -U postgres -d appdb -t public.users appdb.dump
Notes

See summary for usage details.

Restore schema only from archive

Restore DDL only from an archive.

bashANYpg_restoreschema
bash
pg_restore -h localhost -U postgres -d appdb --schema-only appdb.dump
Notes

See summary for usage details.

Restore data only from archive

Restore table data without recreating objects.

bashANYpg_restoredata
bash
pg_restore -h localhost -U postgres -d appdb --data-only appdb.dump
Notes

See summary for usage details.

Parallel restore

Restore using multiple worker jobs when archive format allows it.

bashANYpg_restoreparallel
bash
pg_restore -h localhost -U postgres -d appdb -j 4 appdb.dump
Notes

See summary for usage details.

COPY Import and Export

Fast data movement into and out of PostgreSQL.

Server-side COPY to CSV

Write a table to a server-side file.

sqlANYcopycsvexport
sql
COPY public.users TO '/var/lib/postgresql/users.csv' CSV HEADER;
Notes

Requires superuser or suitable permissions.

Server-side COPY from CSV

Load rows from a server-side CSV file.

sqlANYcopycsvimport
sql
COPY public.users (user_id, email, created_at)
FROM '/var/lib/postgresql/users.csv' CSV HEADER;
Notes

See summary for usage details.

Client-side \copy export

Export via the client machine rather than the database server.

sqlANYpsqlcopycsv
sql
\copy public.users TO './users.csv' CSV HEADER
Notes

See summary for usage details.

Client-side \copy import

Import CSV from the client machine.

sqlANYpsqlcopycsv
sql
\copy public.users (user_id, email, created_at) FROM './users.csv' CSV HEADER
Notes

See summary for usage details.

COPY query to program

Stream exported data to a server-side program.

sqlANYcopyprogramexport
sql
COPY (SELECT * FROM public.users) TO PROGRAM 'gzip > /tmp/users.csv.gz' CSV HEADER;
Notes

See summary for usage details.

COPY FREEZE into fresh table

Load immutable initial data into a newly created or truncated table.

sqlANYcopybulk-loadfreeze
sql
COPY staging_import FROM STDIN WITH (FORMAT csv, HEADER true, FREEZE true);
Notes

Useful for bulk-load workflows with certain constraints.

Recommended next

No recommendations yet.