Skip to content

How to Backup and Restore PostgreSQL Databases

DodaTech 2 min read

In this tutorial, you'll learn about How to Backup and Restore PostgreSQL Databases. We cover key concepts, practical examples, and best practices.

The Problem

You need to create a backup of a PostgreSQL database before upgrading, migrating servers, or recovering from accidental data deletion. Without proper backup tooling, restoring a corrupted or lost database is difficult.

Quick Fix

Step 1: Back up a single database with pg_dump

Export a database to a plain SQL file:

pg_dump -U postgres my_database > my_database_backup.sql

Step 2: Back up in custom format

The custom format supports compression and parallel restore:

pg_dump -U postgres -F c my_database > my_database_backup.dump

The -F c flag creates a compressed custom-format archive.

Step 3: Back up all databases

Use pg_dumpall to dump every database including roles and tablespaces:

pg_dumpall -U postgres > all_databases.sql

Step 4: Restore from a plain SQL backup

Use psql to import a plain SQL file:

psql -U postgres my_database < my_database_backup.sql

Or restore into a new database:

createdb -U postgres new_database
psql -U postgres new_database < my_database_backup.sql

Step 5: Restore from a custom format dump

Use pg_restore for custom-format files:

pg_restore -U postgres -d my_database my_database_backup.dump

For parallel restore (uses 4 CPU cores):

pg_restore -U postgres -d my_database -j 4 my_database_backup.dump

Step 6: Back up with compression

Add gzip compression for smaller backup files:

pg_dump -U postgres my_database | gzip > my_database_$(date +%Y%m%d).sql.gz

Restore from a compressed backup:

gunzip -c my_database_20260624.sql.gz | psql -U postgres my_database

Step 7: Exclude data for schema-only backups

Export only the schema without data:

pg_dump -U postgres -s my_database > schema_only.sql

Alternative Solutions

Use continuous archiving with WAL files

Set up point-in-time recovery for zero data loss:

sudo -u postgres psql -c "SELECT pg_start_backup('backup');"

Use pgAdmin for GUI backups

Database-level backups through a web interface.

Common Mistakes to Avoid

Using pg_dump without -F c for large databases. Plain SQL dumps are uncompressed and slow to restore. Use custom format for compression and parallel restore.

Forgetting to dump global objects. pg_dump only dumps one database. Use pg_dumpall -g to capture roles and tablespaces separately.

Not checking the PostgreSQL version mismatch. A dump from version 16 may not restore on version 14. Use pg_dump --no-owner for cross-version transfers.

Pro Tips

Use --jobs flag for parallel dump and restore. Both pg_dump and pg_restore support -j for parallel processing, significantly speeding up large databases.

Use --format=directory for large databases. Directory format creates one file per table, allowing selective restoration of individual tables.

Use pg_dump --exclude-table for partial backups. Exclude large log tables from daily backups and back them up separately on a different schedule.

Prevention

  • Schedule automated backups with pg_dump via cron and store them off-server.
  • Use custom format (-F c) for faster restores and compression.
  • Test restores in a staging environment before relying on backups in production.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro