PostgreSQL 18 Incremental Backup: Take & Restore
In this article
PostgreSQL 18 introduces native incremental backups, letting you back up only the changed data since the last full or incremental backup. This dramatically reduces backup time and storage for large databases. This guide walks you through taking and restoring incremental backups using the built-in pg_backup tool (or pg_basebackup with the new options). Before starting, ensure you have PostgreSQL 18 installed and have superuser or appropriate backup privileges.
What You'll Need
- PostgreSQL 18 installed and running.
- Access to a superuser account (e.g.,
postgres). - Sufficient disk space for backups (full + increments).
- Basic familiarity with the command line.
Step-by-Step
Taking a Full Backup (Baseline)
- Create a full backup – Incremental backups require a prior full backup. Use
pg_basebackupto create a full base backup:pg_basebackup -h localhost -U postgres -D /backups/full_backup -Fp -Xs -P
This creates a physical backup in/backups/full_backup. - Verify the backup – Check that the backup directory is complete and the
backup_labelfile exists.
Taking an Incremental Backup
- Use the new
--incrementaloption – With PostgreSQL 18,pg_basebackupsupports incremental backups. Run:pg_basebackup -h localhost -U postgres -D /backups/inc1 -Fp -Xs -P --incremental=/backups/full_backup
This creates an incremental backup based on the full backup, storing only changed blocks. - Repeat for subsequent increments – For the next incremental, base it on the previous incremental or the full backup. For example:
pg_basebackup -h localhost -U postgres -D /backups/inc2 -Fp -Xs -P --incremental=/backups/inc1
You can chain increments, but for simplicity, many prefer to base each on the full backup to avoid long chains. - Label your backups – Use clear naming (e.g.,
full_20250101,inc1_20250102) to avoid confusion.
Restoring an Incremental Backup
- Restore the full backup first – Copy the full backup directory to the target data directory (e.g.,
/var/lib/postgresql/18/main). Ensure the directory is empty and owned by the postgres user. - Apply the incremental backup(s) – Use
pg_combinebackupto merge the full and incremental backups into a complete data directory. For example:pg_combinebackup /backups/full_backup /backups/inc1 /backups/inc2 -o /tmp/restored
This produces a restored data directory in/tmp/restored. - Move the restored directory into place – Replace the target data directory with the restored one. Stop PostgreSQL first, then:
mv /var/lib/postgresql/18/main /var/lib/postgresql/18/main.old mv /tmp/restored /var/lib/postgresql/18/main chown -R postgres:postgres /var/lib/postgresql/18/main
- Start PostgreSQL – Start the service and verify data integrity with
pg_checksumsor a simple query.
Common Mistakes
- Basing an incremental on the wrong backup – Always specify the correct parent backup. If you chain increments, ensure the chain is unbroken.
- Skipping the full backup – Incremental backups are useless without a full baseline.
- Forgetting to stop PostgreSQL before restoring – Restoring over a running server can cause corruption.
- Using
pg_restorefor physical backups –pg_restoreis for logical backups; usepg_combinebackupfor physical increments.
Tips
- Automate with cron – Schedule full backups weekly and increments daily to minimize data loss.
- Test restores regularly – Periodically restore to a test server to ensure backups are valid.
- Use WAL archiving – Combine incremental backups with continuous WAL archiving for point-in-time recovery.
- Monitor backup sizes – Incremental backups should be much smaller than full ones; if not, check for excessive changes.
Troubleshooting
- “Incremental backup requires a full backup” – Ensure the path you pass to
--incrementalpoints to a valid full backup directory. - Restore fails with “backup_label not found” – The full backup may be incomplete. Recreate it.
- PostgreSQL won't start after restore – Check file permissions and ownership. Run
chown -R postgres:postgreson the data directory. - Data inconsistency after restore – Verify that the incremental chain is correct. If you skipped a link, the restore will be incomplete. Re-run
pg_combinebackupwith all backups in order.
Frequently Asked Questions
Can I take an incremental backup without a full backup?
No, PostgreSQL 18 requires a full backup as the base. The first backup must be full.
How much space do incremental backups save?
It depends on the rate of change. Typically, increments are much smaller than full backups, but if most of the database changes daily, the savings shrink.
Can I use pg_dump for incremental backups?
No, pg_dump is logical and does not support incremental backup. Use pg_basebackup with the --incremental option.
Do incremental backups affect performance?
They add minimal overhead, mainly reading changed blocks. The main performance impact is during the backup process, which can be scheduled during low activity.
Can I restore an incremental backup on a different server?
Yes, as long as the PostgreSQL version matches and the backup is complete. Copy the combined backup to the new server and start PostgreSQL.
What is the difference between WAL archiving and incremental backups?
WAL archiving captures every change for point-in-time recovery, while incremental backups capture only changed data blocks at specific points, reducing storage and backup time.