PostgreSQL Backup Scripts
PostgreSQL Backup Scripts
Logical Backups, Schema & Table Backups, Cluster Backup, Physical Backup & DBA Utility Commands
Introduction
A reliable PostgreSQL backup strategy is one of the most important responsibilities of a database administrator. Backups help protect databases against accidental data loss, corruption, failed deployments, infrastructure failures, and disaster-recovery scenarios.
PostgreSQL provides several backup approaches. Logical backups can be created with
pg_dump and pg_dumpall, while physical backups can be created
with utilities such as pg_basebackup. WAL archiving can additionally support
Point-in-Time Recovery when properly configured.
Contents
1. Custom-Format Database Backup 2. Plain SQL Database Backup 3. Parallel Directory-Format Backup 4. Schema-Level Backup 5. Table-Level Backup 6. Schema-Only Backup 7. Data-Only Backup 8. Backup All Databases 9. Backup PostgreSQL Global Objects 10. Physical Backup with pg_basebackup 11. Inspect and Validate Backup 12. Example Production Backup Strategy1. Custom-Format Database Backup
The PostgreSQL custom archive format provides a flexible logical backup that can
later be selectively restored using pg_restore.
- -h specifies the PostgreSQL server.
- -p specifies the PostgreSQL port.
- -U specifies the PostgreSQL user.
- -F c selects custom archive format.
- -f specifies the backup output file.
2. Plain SQL Database Backup
A plain-format backup creates a readable SQL script containing commands required to reconstruct the database objects and data included in the dump.
psql, while archive-format backups are normally restored with pg_restore.
3. Parallel Directory-Format Backup
Directory format is useful for larger databases because multiple worker processes can participate in the dump.
- -F d selects directory format.
- -j 4 uses four parallel dump jobs.
- Choose the number of workers according to available CPU, storage throughput, and workload.
4. Schema-Level Backup
Use a schema-specific dump when only selected application objects need to be exported rather than the entire database.
5. Table-Level Backup
A specific table can be exported with pg_dump when an object-level
logical copy is required.
6. Schema-Only Backup
Use --schema-only when the database object definitions are required
without table data.
7. Data-Only Backup
A data-only dump exports table data without recreating the complete database schema.
8. Backup All Databases
pg_dumpall can generate a plain SQL dump covering all databases
in a PostgreSQL cluster together with cluster-wide objects.
pg_dumpall produces a plain-text SQL script. For larger environments,
DBAs may prefer separate database dumps alongside a dedicated globals backup.
9. Backup PostgreSQL Global Objects
Roles and tablespaces are cluster-wide rather than database-specific objects.
Back them up separately when individual databases are protected with pg_dump.
10. Physical Backup with pg_basebackup
pg_basebackup creates a physical base backup of a running PostgreSQL
cluster. It is commonly used as part of physical replication and recovery designs.
- -D specifies the destination directory.
- -F p requests plain output format.
- -X stream streams required WAL while the backup runs.
- -P displays progress.
pg_hba.conf, network access, permissions,
available disk space, WAL retention, and recovery design must be configured correctly
before relying on pg_basebackup in production.
11. Inspect and Validate a Logical Backup
For a PostgreSQL archive created by pg_dump, use
pg_restore --list to inspect the archive contents.
12. Example Production Backup Strategy
There is no single PostgreSQL backup schedule suitable for every production system. The strategy should be driven by business RPO, RTO, database size, recovery architecture, and workload characteristics.
| Frequency / Event | Action | Purpose |
|---|---|---|
| Daily | Logical Database Backup | Provide portable logical recovery copies where appropriate |
| After role changes | Globals Backup | Protect roles and other global objects |
| According to recovery design | Physical Base Backup | Establish a physical recovery baseline |
| Continuous | WAL Archiving | Support PITR where configured |
| Regularly | Test Restore | Prove recoverability and recovery procedures |
Production Backup Checklist
- Define business RPO and RTO requirements.
- Choose logical, physical, and WAL-based protection according to recovery requirements.
- Back up PostgreSQL global objects where required.
- Monitor backup execution failures.
- Monitor WAL archiving where PITR is used.
- Monitor backup duration and storage consumption.
- Define backup retention requirements.
- Protect backup files from unauthorized access and deletion.
- Maintain copies outside the primary database infrastructure.
- Document encryption and key-management requirements.
- Perform complete restore tests regularly.
- Measure actual recovery time against the required RTO.
- Document and rehearse the PostgreSQL disaster-recovery runbook.
Key Takeaways
pg_dumpprovides logical backups of individual PostgreSQL databases.- Custom format provides flexible restore capabilities through
pg_restore. - Directory format supports parallel logical dumps.
pg_dumpallcan protect all databases and PostgreSQL global objects.- Roles and tablespaces require attention when databases are backed up individually.
pg_basebackupprovides physical base backups of a PostgreSQL cluster.- WAL archiving is an important component of PostgreSQL PITR designs.
- Schema-only, data-only, schema-specific, and table-specific dumps are useful but are not substitutes for a complete recovery strategy.
- The correct backup design should be based on business RPO and RTO requirements.
- Actual recovery testing provides stronger assurance than checking whether a backup command simply completed successfully.
Conclusion
PostgreSQL backup planning is more than running pg_dump or
pg_basebackup. A production strategy must connect business recovery
requirements with the correct logical or physical backup methods, WAL protection,
retention, security, monitoring, and restore procedures.
From a DBA perspective, the most important question is not "Did the PostgreSQL backup command succeed?" The more important question is: "Can the database be recovered within the required RPO and RTO?"
A reliable PostgreSQL backup strategy should therefore be validated through actual restore exercises, documented recovery procedures, monitoring, and regular disaster-recovery testing.
Comments
Post a Comment