PostgreSQL
databasepostgresqlGuideIntermediate
Restoring PostgreSQL Databases with pg_restore
Restore PostgreSQL databases from custom dumps, directory archives, and SQL scripts using pg_restore and psql.
Verified: 2026-09-30
Tested on:postgres 16.x
Platforms:
linuxmacoswindows
#postgresql#restore#pg_restore#backup#disaster-recovery
1. Restoring Custom Binary Dumps (pg_restore)
For backups created with pg_dump -F c:
# Restore into an existing database
pg_restore -h localhost -p 5432 -U postgres -d target_db -v backup.dump
# Clean (drop) existing tables before restoring and recreate schema
pg_restore -U postgres -d target_db --clean --if-exists -v backup.dump2. Multi-Threaded Parallel Restore (-j)
Drastically accelerate database restores on multi-core servers:
pg_restore -U postgres -d target_db -j 4 -v backup.dump3. Restoring Specific Tables or Schemas
Extract only a specific table from a full database backup dump:
# Restore only the users and orders tables
pg_restore -U postgres -d target_db -t users -t orders backup.dump
# Restore only the public schema
pg_restore -U postgres -d target_db -n public backup.dump4. Restoring Plain SQL Files (psql)
Plain text .sql files cannot be processed by pg_restore. Use standard psql piping:
# Restore plain SQL script
psql -h localhost -U postgres -d target_db -f backup.sql
# Restore gzipped SQL script
gunzip -c backup.sql.gz | psql -U postgres -d target_dbDisaster Recovery Checklist
- Always verify backup integrity by performing regular test restores in a staging environment.
- Ensure database users and roles have been created before restoring database objects.
- Terminate active application connections before performing full
--cleandatabase restorations.
PostgreSQL Database Backup with pg_dump
Export and backup PostgreSQL databases using pg_dump, custom compressed archives, multi-threaded parallel dumps, and pg_dumpall.
PostgreSQL Performance Optimization & Tuning
Analyze slow queries with EXPLAIN ANALYZE, tune memory parameters (shared_buffers, work_mem), manage autovacuum, and pool connections with PgBouncer.