CodeGym /Courses /SQL SELF /Backup Types in PostgreSQL: Full, Incremental, Differenti...

Backup Types in PostgreSQL: Full, Incremental, Differential

SQL SELF
Level 43 , Lesson 1
Available

So, imagine your database is this massive library archive with thousands of books. A full backup is like sending a courier every night to copy every single book. But what if almost nothing changed? That’s not just slow, it also puts a heavy load on your transport and eats up a ton of space. That’s where incremental and differential approaches come in. We work smarter, not harder, by copying only what actually changed.

Today we’ll break down these backup types:

  • Full backup: a complete copy of the database.
  • Incremental backup: copies only the changes since the last backup.
  • Differential backup: copies changes since the last full backup.

Each of these approaches has its own pros, cons, and use cases. Let’s dig in.

Full Backup

A full backup is a snapshot of your entire database at a specific point in time. It includes both the structure (tables, indexes, schemas) and all the data. Basically, it’s a “snapshot” of everything in your database.

Example command for a full backup:

pg_dump -U username -d database_name -F c -f full_backup.dump

Here:

  • -U username — PostgreSQL user name.
  • -d database_name — database name.
  • -F c — backup format (custom, handy for restoring with pg_restore).
  • -f full_backup.dump — file name where the backup will be saved.

You’ll learn more about the pg_dump command and its options in the next lectures :P

Advantages of a full backup:

  1. Easy restore. If something goes wrong, a full backup lets you quickly restore the whole database.
  2. Complete data snapshot. Can be used to move the entire database to another server or environment.

Disadvantages of a full backup:

  1. Data volume: a full backup can be huge, especially if you have a lot of data.
  2. Takes a long time: the more data you have, the longer it takes to make a copy.
  3. Storage space: each full backup takes up separate space, which can eat up your storage resources.

You can use a full backup:

  • When creating your first backup.
  • Before making major changes to the database.
  • For long-term archiving (make a full backup and send it to “cold” storage).

Incremental Backups

An incremental backup saves only those files that have changed since the last backup — whether that was a full or incremental one. This can save a ton of disk space and speed up the backup process.

In PostgreSQL, you can do incremental backups using the pgBackRest tool — it’s a powerful utility designed for creating and managing PostgreSQL backups with support for full, incremental, and differential strategies.

How does pgBackRest work?

  • pgBackRest uses metadata and file checksums to detect changes.
  • Only files that actually changed are included in the incremental backup.
  • Automatic WAL file archiving is supported, so you can restore to any point in time.

Example command for an incremental backup:

pgbackrest --stanza=main --type=incr backup

Where:

  • --stanza=main — configuration name (a set of parameters for a specific DB).
  • --type=incr — tells it to do an incremental backup.

Before your first incremental backup, you gotta do a full (--type=full) backup.

Advantages of incremental backups with pgBackRest:

  1. Space saving: only changed files are copied.
  2. Fast execution: especially if there aren’t many changes.
  3. Automatic WAL management: pgBackRest handles WAL file archiving and applying for you.
  4. Flexible restore: you can pick a restore point (PITR).

Disadvantages and gotchas:

  1. Initial setup required: you need to configure the stanza, repository directory, and permissions.
  2. Can be tricky for newbies: you need to understand WAL and PostgreSQL architecture.
  3. Repository dependency: all backups are stored centrally — you gotta manage your storage.

When to use incremental backups:

  • If your database is updated a lot.
  • When a full backup is too heavy or takes too long.
  • If you want a reliable and automated restore strategy with minimal overhead.

Differential Backups

A differential backup saves all changes that happened since the last full backup. The difference from incremental is that each differential backup contains the full set of changes since the last full backup. So, to restore your database, you only need the last full backup and the latest differential backup.

Example process:

  1. First night: make a full backup (A).
  2. Second night (B): make a differential backup — contains changes since A.
  3. Third night (C): make a differential backup — also contains changes since A.

Advantages:

  1. Easy restore: To restore the database, you only need the full backup and the latest differential backup.
  2. Faster than a full backup: Differential backup is faster than a full one, since only changes are copied.

Disadvantages:

  1. Backup file size: Each new differential backup gets bigger, since it includes all changes since the last full backup.
  2. Disk space: In the long run, you might need more space than with incremental backups.

Differential backup is handy when you want to make restoring the database easier: it keeps all changes since the last full backup. This is especially useful if your data changes often, but making full copies every time is too time-consuming or resource-heavy. In those cases, the differential approach is a nice compromise between speed and reliability.

Comparing Backup Types

Backup Type Full Incremental Differential
Data volume Maximum Minimum Average
Execution time Long Fast Moderate
Restore Simple Complex (depends on all backups) Medium (need last full and diff.)
When to use Initial copy, migrations Frequent real-time backups Regular backups

The backup type you pick depends on your data safety requirements, how often your data changes, and your available resources. For small databases, regular full backups work fine. If your database is growing, incremental backups help save space. And if you care about fast restores, differential backups might be your best bet. The main thing — don’t forget about regularity and setting up automation!

2
Task
SQL SELF, level 43, lesson 1
Locked
Creating a Full Database Backup
Creating a Full Database Backup
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION