Back up a PostgreSQL database to a file.
low risk
pg_dump creates consistent backups of PostgreSQL databases without locking tables. The custom format (-Fc) is recommended — it supports parallel restore and selective table restoration. For full cluster backups including roles, use pg_dumpall.
| Flag | Meaning |
|---|---|
-Fc | Custom compressed format (best for large DBs) |
-Fp | Plain SQL format (human-readable) |
-t --table | Dump only specific table |
-n --schema | Dump only specific schema |
-j --jobs | Parallel dump with N jobs |
pg_dump -h localhost -U postgres -Fc mydb > mydb.dumpFull custom-format backup · explain & risk-check →pg_dump -h db.prod -U postgres -Fc -t users mydb > users_backup.dumpBack up single table · explain & risk-check →pg_restore -U postgres -d mydb_restore mydb.dumpRestore from custom format backup · explain & risk-check →pg_dump -Fc mydb | gzip > mydb_$(date +%Y%m%d).dump.gzCompressed timestamped backup · explain & risk-check →Use `pg_dump -Fc` (not plain SQL) for production backups — custom format is smaller, supports parallel restore with `pg_restore -j 4`, and allows restoring individual tables.
Reference text is hand-written and may be incomplete — check man pg_dump for your version. Test destructive commands on a non-production system first.