pgBackRest for PostgreSQL on Ubuntu 24.04 (Backup + Restore + Encryption + Retention)

pgBackRest is a powerful, open-source backup and restore solution for PostgreSQL, designed to provide reliable, efficient, and secure database protection. It supports full, differential, and incremental backups, WAL archiving, parallel compression, and encryption — all while maintaining low overhead on the production system.

Running pgBackRest on Ubuntu 24.04 LTS (Noble Numbat) ensures long-term stability, high security, and up-to-date PostgreSQL compatibility. Ubuntu 24.04 includes systemd 255, OpenSSL 3, and the latest PostgreSQL 16 packages from the official PGDG repository — making it an excellent environment for setting up enterprise-grade PostgreSQL backup workflows.


Architecture Overview

LayerComponentRole
OSUbuntu 24.04 LTSStable, secure base with long-term support
DatabasePostgreSQL 15 / 16Primary database instance to be backed up
Backup ToolpgBackRestHandles backup, restore, archiving, and retention policies
StorageLocal or Remote RepositoryStores backup archives and WAL segments
EncryptionOpenSSL / GPGEnsures encrypted, tamper-proof backup data
Process ControlsystemdManages PostgreSQL and pgBackRest services

Why Use pgBackRest?

  • Enterprise-grade reliability – designed for large PostgreSQL databases.
  • Parallel processing – fast backups and restores using multiple streams.
  • Compression and encryption – saves storage space and secures data.
  • Full + incremental backups – efficient retention and recovery strategy.
  • WAL archiving – supports point-in-time recovery (PITR).
  • Remote backups – supports SSH-based offsite or central repository setups.
  • Automated retention policies – keeps recent backups and prunes older ones automatically.

pgBackRest vs Other Backup Tools

Feature / CapabilitypgBackRestBarmanNative pg_basebackup
Incremental backups✅ Yes✅ Yes❌ No
Compression✅ Yes✅ Yes❌ No
Parallel processing✅ Yes❌ No❌ No
Encryption✅ Yes✅ Yes❌ No
Retention policies✅ Yes✅ Yes❌ No
Remote backups✅ Yes✅ Yes✅ Limited
PITR (WAL restore)✅ Yes✅ Yes✅ Yes

pgBackRest is the preferred choice for production PostgreSQL environments requiring high-performance, encrypted, and automated backups.


Security & Best Practices

  • Use dedicated PostgreSQL and pgBackRest users for separation of privileges.
  • Store configuration files under /etc/pgbackrest with restrictive permissions (750).
  • Enable encryption for both data and communication channels.
  • Mount or map backup storage to separate disks or network shares for isolation.
  • Schedule automated full and incremental backups via systemd timers or cron jobs.
  • Keep WAL archiving enabled to allow point-in-time recovery (PITR).
  • Monitor backups regularly with pgbackrest info and integrate with Prometheus or Grafana.
  • Use UFW or nftables to limit access to backup repositories if remote.

Typical Use Cases

  • Production PostgreSQL environments requiring robust disaster recovery.
  • Large databases with frequent transactions and WAL activity.
  • Organizations implementing offsite or encrypted backup policies.
  • Database administrators needing point-in-time recovery or long-term archival.
  • Cloud and hybrid deployments using object storage (S3, Azure Blob, GCS) via pgBackRest plugins.

Deploying pgBackRest for PostgreSQL on Ubuntu 24.04 provides a secure, efficient, and enterprise-grade backup solution that ensures data durability, quick recovery, and minimal downtime — making it the go-to choice for serious PostgreSQL deployments.


Step 1: Create a Server Instance on Shape.Host

Log in to your Shape.Host account.

Click Create → Instance.

Shape.host Resources dashboard with Create > Instance highlighted.

Choose your preferred data center location.

Shape.host instance form with the data center location selector highlighted.

Select a plan with at least 2 CPUs, 4 GB RAM, and 20 GB SSD.

Choose Ubuntu 24.04 (64-bit) as the operating system.

Shape.host instance form with the operating system image selector highlighted.

Click Create Instance.

Shape.host instance form with the Create Instance button highlighted.

Once created, note down your public IP address under the Resources section.

Shape.host Resources dashboard with the server IP address highlighted.

Step 2: Connect to Your Server

Use SSH to connect to the instance.

  • Linux/macOS: ssh root@your_server_ip
  • Windows (PuTTY):
    1. Open PuTTY.
    2. Enter the IP address and connect as root.

Step 3: Install PostgreSQL

First, update system repositories:

apt update
Terminal showing APT package management commands and output.

Install the required utilities and PostgreSQL packages:

apt install curl wget gnupg lsb-release
apt install postgresql postgresql-client
Terminal showing APT package management commands and output.
Terminal showing APT package management commands and output.

Enable and start the PostgreSQL service:

systemctl enable postgresql
systemctl start postgresql

Check if PostgreSQL is active:

systemctl status postgresql

You should see the service listed as active (running).

Terminal showing systemd service management commands or output.

Step 4: Add the PostgreSQL APT Repository

pgBackRest is distributed through the official PostgreSQL repository.
Add the repository to your system:

echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list

Import the PostgreSQL signing key:

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -

Update package lists:

apt update
Terminal showing APT package management commands and output.

Step 5: Install pgBackRest

Install pgBackRest:

apt install pgbackrest
Terminal showing APT package management commands and output.

Verify the installation:

pgbackrest version

If successful, you’ll see a version number such as pgBackRest 2.x.

Terminal displaying the pgBackRest version.

Step 6: Configure pgBackRest

Create the main configuration directory:

mkdir -p /etc/pgbackrest
chown postgres:postgres /etc/pgbackrest
chmod 750 /etc/pgbackrest
Terminal showing file ownership or permission commands.

Open the configuration file:

nano /etc/pgbackrest/pgbackrest.conf

Paste the following content:

[global]
repo1-path=/var/lib/pgbackrest
repo1-retention-full=2
log-level-console=info
log-level-file=detail
start-fast=y
compress-type=gz

[main]
pg1-path=/var/lib/postgresql/16/main
pg1-port=5432
Terminal showing the GNU nano editor and configuration text.

Explanation

ParameterDescription
repo1-pathDirectory where backups will be stored.
repo1-retention-fullKeeps the last two full backups.
compress-typeUses gzip for compression.
pg1-pathLocation of PostgreSQL data directory.
pg1-portDefault PostgreSQL port.

Save and exit (CTRL + O, then CTRL + X).


Step 7: Configure PostgreSQL for Archiving

Edit the PostgreSQL configuration file:

nano /etc/postgresql/16/main/postgresql.conf

Find and update (or add) these lines:

archive_mode = on
archive_command = 'pgbackrest --stanza=main archive-push %p'
archive_timeout = 60

These settings enable WAL (Write Ahead Log) archiving, allowing pgBackRest to capture and store transaction logs.

Terminal showing file and directory setup commands.

Restart PostgreSQL to apply changes:

systemctl restart postgresql
Terminal showing systemd service management commands or output.

Step 8: Initialize and Test pgBackRest

Switch to the PostgreSQL user:

su - postgres

Create a new backup stanza:

pgbackrest --stanza=main stanza-create
Terminal showing su commands or output during Initialize and Test pgBackRest.

Check system integrity:

pgbackrest --stanza=main check

Run your first full backup:

pgbackrest --stanza=main --type=full backup
Terminal showing su commands or output during Initialize and Test pgBackRest.

Exit the postgres user:

exit

View backup information:

pgbackrest info
Terminal displaying pgBackRest backup information.

You’ve successfully installed and configured pgBackRest on Ubuntu 24.04 with PostgreSQL 16.
You can now easily create, verify, and restore backups to ensure your database’s safety.

For high-performance backup solutions with full root access and SSD storage, host your PostgreSQL infrastructure on a Shape.Host Cloud VPS — optimized for reliability and speed.