Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL 18 Incremental Backup: Take & Restore

· 4 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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)

  1. Create a full backup – Incremental backups require a prior full backup. Use pg_basebackup to 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.
  2. Verify the backup – Check that the backup directory is complete and the backup_label file exists.

Taking an Incremental Backup

  1. Use the new --incremental option – With PostgreSQL 18, pg_basebackup supports 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.
  2. 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.
  3. Label your backups – Use clear naming (e.g., full_20250101, inc1_20250102) to avoid confusion.

Restoring an Incremental Backup

  1. 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.
  2. Apply the incremental backup(s) – Use pg_combinebackup to 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.
  3. 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
  4. Start PostgreSQL – Start the service and verify data integrity with pg_checksums or 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_restore for physical backups – pg_restore is for logical backups; use pg_combinebackup for 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 --incremental points 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:postgres on 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_combinebackup with 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.