Create a full database backup
Low risk
What do risk levels mean?
- Read-only
- Inspects state without changing anything.
- Low risk
- Reversible with a routine follow-up command.
- Medium risk
- Changes state; undo path documented.
- Destructive
- Deletes or overwrites; confirmation required.
Dumps an entire PostgreSQL database to a compressed SQL file so you can restore it later or archive it before making risky changes.
postgresqlbackupdumppg-dump
Parameters
Name of the database to back up.
Path for the backup file, e.g. backup.sql.gz.
Commands
Export the full database schema and data into a compressed SQL file
pg_dump -d <database_name> --no-owner --no-privileges | gzip > backup.sql.gzVerification
ls -lh backup.sql.gzThe backup file exists with a non-zero size.
Undo
Not undoable
The backup file itself consumes disk space; delete it manually when it is no longer needed.
Pitfalls
- Runs a consistent snapshot but does not lock tables; concurrent writes during the dump may not be captured.
- Large databases may take minutes or hours; monitor the output file size during the dump.
- Backups require disk space equal to the uncompressed database size; ensure enough free space.
- On Windows, gzip may not be available; use pg_dump --format=custom for a compressed custom-format backup.