Skip to content

Q&A · Answered

How to backup and restore PostgreSQL database

asked 9 months, 1 week ago 1810 views 1 answer
11

What is the command to backup and restore a PostgreSQL database?

1 Answer

One accepted
21

Accepted answer

Not confirmed since it was written, 9 months, 1 week ago
# 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.dump

Your answer

Say what you tried, not only what worked — the next person needs the reasoning. Code goes in a fenced block.

Shown beside your answer and never checked, so it cannot be a name someone already has here.

No account needed to answer. Sign in instead and it carries your name and earns reputation.