11
What is the command to backup and restore a PostgreSQL database?
Loading...
Q&A · Answered
What is the command to backup and restore a PostgreSQL database?
Accepted answer
# Backup
pg_dump -U username -d database_name > backup.sql
# Compressed backup
pg_dump -U username -d database_name | gzip > backup.sql.gz
# Restore
psql -U username -d database_name < backup.sql
# Restore from compressed
gunzip -c backup.sql.gz | psql -U username -d database_name
# Custom format (recommended)
pg_dump -Fc -U username database_name > backup.dump
pg_restore -U username -d database_name backup.dumpSay what you tried, not only what worked — the next person needs the reasoning. Code goes in a fenced block.