CYBERTEC PostgreSQL Logo

Exploration: CNPG Point In Time Recovery

09.2025
Category: 

Introduction

In our CNPG series, we have mentioned that backups are crucial in every database system in case of any disaster. PostgreSQL has powerful recovery capabilities as well as backup capabilities, including the ability to restore a database cluster to a specific moment in time. This is extremely useful in scenarios where we need to recover from user errors—such as accidental data deletion, wrong updates, dropped tables, or even dropped databases.

We will now walk through how to perform Point-in-Time Recovery (PITR) with CloudNativePG (CNPG) using a Barman Cloud plugin for backups.


Preparing the Scenario

First, we need to simulate a typical disaster. In our CNPG cluster instance:

Create a sample table and load some data:

Update some rows to check later:

Note the current Write-Ahead Log (WAL) position and timestamp to recover exactly to this point with PITR:

Now, simulate a disaster:

At this point, the table is dropped and the only option we have to recover the table to timestamp we want is PITR.


Backup and WAL Archiving

We already have a backup and WAL archive system in place (as covered in the CNPG Backup with Barman Cloud blog). For point in time recovery, WAL files are essential since they contain all transactions after the last backup.

We can confirm WAL switching and the WAL filename:


Cluster Manifest for PITR

To recover the cluster to a point just before the DROP TABLE, we’ll define a new cluster manifest with bootstrap.recovery:

In this manifest, there are a few things to emphasize.

  • recovery.source is the name of the external cluster we define in the cluster.spec.
  • serverName is the name of the cluster we recover. CNPG looks for a main directory with this name in our barman object store.
  • Under recovery.recoveryTarget we can specify multiple key-values, such as backupID or targetTLI, but targetTime is enough in our case. Because we noted the LSN number right before the drop table, we can specify targetLSN, but targetTime is more practical.

Apply the manifest:

CNPG will fetch the backup and WAL archives from barman-cloud, then replay transactions up until the specified timestamp, stopping just before the DROP TABLE.


Validating the PITR Cluster

Once the cluster is running:

Connect to the restored cluster:

Now verify the data:

The table is back with all rows intact — successfully recovered to the desired point in time before it is dropped.


Conclusion

Point-in-Time Recovery (PITR) is a powerful feature that lets you rewind your PostgreSQL database to any exact moment—rescuing us from accidental mistakes like dropped tables or bad updates/deletes.

With CloudNativePG and Barman Cloud, PITR becomes a declarative and Kubernetes-native process. By simply adjusting our cluster manifest, we can restore our data to the specific LSN, timeline, or timestamp we need.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

CYBERTEC Logo white
Get the newest PostgreSQL Info & Tools


    This site is protected by reCAPTCHA and the Google Privacy Policy & Terms of Service apply.

    ©
    2025
    CYBERTEC PostgreSQL International GmbH
    phone-handsetmagnifiercrosscross-circle
    linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram