Skip to content

Back up and restore the database

With the default cnpg database the chart can archive the write-ahead log continuously and take scheduled base backups to an object store. Together they give you point-in-time recovery. Backups are off by default.

The chart supports Google Cloud Storage today (through the CloudNativePG built-in Barman object store).

  1. A bucket, for example gs://my-vesicle-backups.
  2. A Google service account with these permissions on the bucket: object read/write/delete (for example Storage Object Admin) and bucket metadata read (for example Storage Legacy Bucket Reader). Without the bucket permission, archiving fails with an access error on storage.buckets.get.
  3. A JSON key for that service account, stored in a Kubernetes Secret.

The chart does not create the credentials Secret. Create it yourself, in the release namespace:

Terminal window
kubectl -n vss create secret generic postgres-backup \
--from-file=gcsCredentials=./service-account-key.json

The Secret key is gcsCredentials by default. Change it with database.cnpg.backup.credentialsSecret.key. If you keep secrets in a store, create the Secret with the External Secrets Operator: declare the ExternalSecret under extraObjects in your values, and reference the Secret it produces.

On GKE with Workload Identity, set database.cnpg.backup.gkeEnvironment: true and no credentials Secret is needed.

database:
cnpg:
backup:
enabled: true
destinationPath: gs://my-vesicle-backups/production
retentionPolicy: 30d
schedule: "0 0 2 * * *" # six fields, seconds first, UTC: daily at 02:00
credentialsSecret:
name: postgres-backup

Other settings: walCompression and dataCompression (default gzip).

Terminal window
kubectl -n vss get cluster,backup,scheduledbackup
kubectl -n vss get cluster vss-postgres -o jsonpath='{range .status.conditions[*]}{.type}={.status} {.message}{"\n"}{end}'

You want ContinuousArchiving=True, and the first Backup ending in the phase completed. A backup stuck in walArchivingFailing means the archiver cannot write to the bucket: check the permissions above, then delete the stuck Backup object so a new one can start. The scheduled job takes the first backup right after you enable it.

A cluster must be the only writer of its folder in the bucket. If the folder already holds files from another cluster, archiving fails with Expected empty archive. This happens when you reinstall and the old data is still there. Either empty the folder, or set a new folder name:

database:
cnpg:
backup:
serverName: vss-postgres-2 # default: the cluster name

Pick a new serverName every time you recreate the database, and keep the old folder until you no longer need it.

A restore creates a new cluster from the bucket. It never overwrites a running one. Use it for disaster recovery, or when a database must be recreated.

database:
cnpg:
backup:
enabled: true
destinationPath: gs://my-vesicle-backups/production
serverName: vss-postgres-2 # NEW name: never the folder you restore from
credentialsSecret:
name: postgres-backup
recovery:
enabled: true
sourceServerName: vss-postgres # the folder to restore from
# backupName: <a specific Backup> # default: the latest
# targetTime: "2026-11-28 09:30:00+00" # replay the log up to this moment

Steps:

  1. Remove the broken database: helm uninstall deletes the database cluster. The generated Secrets and the NATS volumes stay.
  2. Install again with the values above, under the same release name.
  3. When the new cluster is healthy, log in and check your data.

The recovery block applies only when the cluster is created. Keep it in your values while the restored cluster exists: if the cluster is ever recreated without it, you get an empty database.

NATS keeps its own state (durable queues and media) and is not part of the database backup. After a restore, check that message sync works, and rehearse the whole procedure in a spare environment before you need it.

  • Practise a restore in a spare environment before you rely on backups.
  • Back up before every upgrade and wait for the Backup to complete.
  • Add a lifecycle rule on the bucket for old objects. retentionPolicy removes expired backups, but a rule is a second line of defence.
  • Use a separate service account and bucket per environment.
  • Back up the federation signing key separately: it is not in the database (see Install with Helm).