Skip to main content
members-only

How to Use pgBackRest to Backup PostgreSQL Database

In this article, learn how to install and configure pgBackRest for PostgreSQL, create full, differential, and incremental backups, perform point-in-time recovery, and automate backups with cron jobs.

β€” Ravi Saive

pgBackRest is an open source enterprise-grade backup and restore solution designed specifically for PostgreSQL databases, offering features like parallel processing, compression, encryption, and support for full, differential, and incremental backups.

Unlike pg_dump command-line utility that creates logical backups by exporting database contents, pgBackRest performs physical backups that copy the actual database files directly, making it significantly faster for large databases and production environments where backup and restore speed is most important.

In this guide, you'll learn how to install and configure pgBackRest for PostgreSQL, from initial setup through performing backups and restores with confidence.

Why Use pgBackRest?

Before going into the installation, it's worth understanding what makes pgBackRest the preferred backup solution for PostgreSQL in production environments.

  • Multiple backup types - pgBackRest supports full, differential, and incremental backups, allowing you to optimize storage space and backup time.
  • Parallel processing: Backup and restore operations run in parallel across multiple CPU cores, dramatically reducing the time needed for large databases.
  • Point-in-Time Recovery (PITR): Restore your database to any specific moment in time, which is crucial when you need to recover from accidental data deletion or corruption that happened between scheduled backups.
  • Compression and encryption: Built-in support for multiple compression algorithms (gzip, lz4, zstd) reduces storage requirements, while encryption protects sensitive data at rest.
  • Remote backup support: Back up to local storage, remote servers via SSH/TLS, or cloud storage providers like AWS S3, Azure Blob Storage, and Google Cloud Storage.
  • Backup verification: pgBackRest validates checksums during backup operations and can detect page-level corruption early.
  • Resume capability: If a backup is interrupted due to network issues or system problems, pgBackRest can resume from where it stopped instead of starting over.
  • Backup from standby: Reduce load on your primary production server by performing backups from a standby replica, keeping your primary server focused on serving production traffic.

These features make pgBackRest suitable for everything from small development databases to enterprise-scale production systems handling critical data.

Understanding Backup Types

pgBackRest supports three types of backups, each with different trade-offs between backup speed, storage requirements, and how easy it is to restore the database.

  • Full Backup - A full backup copies the entire database, including data, configuration files, and WAL (Write-Ahead Logging) archives, and is always the first backup, forming the base for later differential or incremental backups, and it can be restored on its own, making recovery simple and fast.
  • Differential Backup - A differential backup saves only the changes made since the last full backup, making it faster and using less storage, and to restore, you only need the last full backup plus the latest differential backup.
  • Incremental Backup - An incremental backup saves only the changes made since the last backup of any type, making it the fastest and most storage-efficient, but restoring takes more steps because you must apply the last full backup along with all incremental backups in order.

Prerequisites

Before installing pgBackRest, make sure you have the following in place:

  • A running PostgreSQL installation - pgBackRest works with PostgreSQL 9.3 and later, though PostgreSQL 12 or newer is recommended for the best performance and feature support.
  • Root or sudo access - You'll need administrative privileges to install packages, modify PostgreSQL configuration files, and set up backup directories with proper permissions.
  • Sufficient storage space - Plan backup storage on a separate disk or remote server, allocating 1.5-2 times your database size for full backups plus extra for differential and incremental backups, so a 100GB database needs 200-300GB.
  • Basic PostgreSQL knowledge - You should be familiar with PostgreSQL configuration files (postgresql.conf and pg_hba.conf), know how to restart PostgreSQL, and understand basic concepts like data directories and WAL.

Installing pgBackRest in Linux

pgBackRest is available in the official PostgreSQL repositories for most Linux distributions, making installation easy and ensuring you get regular updates.

On Ubuntu and Debian

If you haven't already added the PostgreSQL APT repository, start by adding it to get the latest version of pgBackRest.