pg_dump and pg_restore Workflow
Create reliable database backups with pg_dump and restore them with pg_restore for disaster recovery and environment cloning.
Prerequisites
- -PostgreSQL installed
- -psql and pg_dump available in PATH
- -Database connection credentials
Steps
Create a plain SQL backup
Dump the entire database as a SQL script file for portability.
Plain format is human-readable and can be restored with psql. Use -F custom for large databases that benefit from compression.
Create a compressed custom-format backup
Dump the database in PostgreSQL custom format with built-in compression.
-Z 6 sets gzip compression level (0-9). Custom format also supports parallel restore.
Backup a single table
Dump only a specific table for targeted backup or migration.
Restore from a custom-format backup
Restore a database from a custom-format dump file.
The -c flag drops existing objects before recreating them. Omit it if restoring into an empty database.
Restore from a plain SQL backup
Execute a plain SQL dump file to restore the database.
Parallel restore for large databases
Speed up restoration by using multiple parallel jobs.
-j 4 uses 4 parallel workers. Match this to your available CPU cores for optimal speed.
Full Script
FAQ
Discussion
Loading comments...