Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.
PostgreSQLCloudnativepgDatabaseBackupself-managed enterprise edition

Use CloudNativePG as your external PostgreSQL database

Run the Harness Self-Managed Enterprise Edition PostgreSQL database on Kubernetes with CloudNativePG, including migration, backups, and point-in-time recovery.

Use CloudNativePG (CNPG) to run the Harness Self-Managed Enterprise Edition PostgreSQL database as a highly available cluster on Kubernetes. CNPG handles failover, replication, backups, and point-in-time recovery.

This page uses the community cluster Helm chart. Go to Use an external self-managed PostgreSQL database with your installation if you run PostgreSQL on virtual machines instead.

Tested on Google Cloud only

Harness tested this CNPG installation on Google Cloud (GKE and Google Cloud Storage), and the steps on this page are for that platform. To install on another cloud provider, go to the CloudNativePG documentation and the Barman Cloud plugin object store documentation for your provider.

What you will learn from this topic

  • How to install a new CNPG cluster for Harness.

  • How to migrate an existing PostgreSQL database with minimal downtime.

  • How to enable backups to Google Cloud Storage.

  • How to restore a backup to a specific point in time.

Before you begin

  • Kubernetes: Version 1.29 or later.

  • Tools: helm, kubectl, and yq version 4.

  • PostgreSQL version: 16.

  • Extensions: pg_partman, pg_cron, and hstore.

  • Backups only: cert-manager, a Google Cloud Storage bucket, and volume snapshot support. Go to Check volume snapshot requirements to review the requirements.

Use the Harness PostgreSQL image

Harness requires the pg_cron and pg_partman libraries. The standard CNPG images do not include them, so a cluster that uses a standard image fails Harness database setup.

Use docker.io/harnesssecure/cnpg-postgres:16.14-bookworm. The image also includes pgvector and pgaudit.

Architecture

Harness services connect to the primary instance. CNPG streams changes to a replica, archives write-ahead log (WAL) files to object storage, and takes a daily volume snapshot from the replica.

Backup and recovery use two parts:

  • Base backups: A scheduled backup takes a volume snapshot of the replica data volume every day.

  • WAL archive: CNPG continuously archives WAL files to a Google Cloud Storage bucket through the Barman Cloud plugin.

To recover, CNPG restores a snapshot, then replays the archived WAL files up to the point in time that you choose. This is point-in-time recovery (PITR).

Install the operator and plugin

Install the CNPG operator. Go to CloudNativePG installation to review all installation options.

If you enable backups, install the Barman Cloud plugin. Go to Barman Cloud plugin installation to review the requirements.

Create the values file

Every procedure on this page uses this file. Replace <storage-class> with a storage class in your cluster.

The helm.sh/resource-policy: keep annotation keeps the cluster and its volumes if you uninstall the Helm release. The initdb settings apply only when CNPG creates a new cluster. The database, owner, and secret settings stop CNPG from creating a separate app database and user.

Install a new cluster

Use this procedure for a new Harness installation that has no existing PostgreSQL data.

1

Create the superuser secret. Create the secret in the database namespace and in the Harness namespace. A secret can be read only from its own namespace: CNPG reads it in cnpg-system, and the Harness pods read it in the Harness namespace.

2

Install the cluster.

The primary is available at harness-pg-rw.cnpg-system.svc.cluster.local:5432.

3

Go to Connect Harness to PostgreSQL to point Harness at the new cluster.

Migrate an existing PostgreSQL database

Use this procedure to move the PostgreSQL database that ships with Harness to CNPG. The new cluster starts as a streaming replica of your current database, so downtime is limited to the cutover.

1

Allow replication on the current database. Add these values to your Harness release, then run helm upgrade. This restarts the bundled PostgreSQL once.

Replace <pod-cidr> with the pod CIDR of your cluster. Set wal_keep_size to at least the amount of WAL written while the copy runs.

2

Create the secrets. The copy uses the existing postgres secret to connect to the current database.

3

Start the replica cluster. Create the migration values, then install the cluster.

Wait for Cluster in healthy state. All databases, roles, and pg_cron jobs are copied.

4

Stop Harness and confirm the new cluster is caught up. Scale Harness down, then compare the write positions.

The second value must be equal to or higher than the first.

5

Promote the new cluster. Promotion detaches the new cluster from the old database and cannot be undone.

The result must be f. Then upgrade Helm with the standard values, which adds the second instance.

6

Go to Connect Harness to PostgreSQL, then scale Harness up. Keep the old PostgreSQL volume until you verify the new cluster and take a backup.

Connect Harness to PostgreSQL

Add these values to your Harness release, then run helm upgrade.

Enable backups

CNPG archives WAL files continuously to object storage. It takes base backups as daily volume snapshots from the replica. Together they let you restore to any point in time.

Check volume snapshot requirements

Base backups are volume snapshots, so the cluster must support them. Check the following before you continue.

  • CSI driver: A CSI driver that supports snapshots. On GKE, this is the Persistent Disk CSI driver, pd.csi.storage.gke.io, and the PostgreSQL volumes must use a storage class that it backs, such as standard-rwo.

  • Snapshot components: The VolumeSnapshot, VolumeSnapshotContent, and VolumeSnapshotClass custom resource definitions, and the snapshot controller.

  • Snapshot class: A VolumeSnapshotClass for the CSI driver.

If you do not have a snapshot class, create one.

Plan for a regional failure

Set storage-locations to a multi-region, such as us, so the snapshots stay available if the region of the cluster fails. A dual-region bucket protects only the WAL archive.

With deletionPolicy: Delete, deleting a VolumeSnapshot also deletes the Google Cloud snapshot. Deleting the namespace deletes the snapshots too.

Go to Volume snapshots on GKE to review the GKE requirements.

Set up backups

1

Create a bucket and grant the cluster access. The Kubernetes service account has the same name as the cluster, harness-pg.

Use a dual-region bucket if possible, so the WAL archive survives the failure of a region. For example, use nam4 for the United States or eur4 for Europe. Go to Bucket locations to choose a location.

This procedure uses GKE Workload Identity.

Go to Object stores to review other authentication options.

2

Add the snapshot settings to the cluster. Replace <snapshot-class> with your VolumeSnapshotClass.

Go to Volume snapshot backups to review these settings.

3

Create the backup values. The schedule runs daily at 02:00. The six fields start with seconds.

4

Apply the backup values. The plugin adds a sidecar container, so the instances restart one after the other. Run this step outside peak hours.

5

Verify the backup.

The first backup shows completed, the snapshot shows READYTOUSE true, and ContinuousArchiving is True.

Clean up old backups

CNPG never deletes old snapshots. Delete old Backup objects to remove their snapshots, and add a lifecycle rule to the bucket to expire old WAL files.

Restore from a backup

A restore always creates a new cluster, named harness-pg-restored in this procedure. The running cluster is not changed. CNPG restores the snapshot, then replays WAL files up to the point in time that you choose.

1

Choose the snapshot. The restored cluster must archive to a new, empty path.

2

Give the new cluster access to the bucket. Its service account is harness-pg-restored.

3

Create the restore values. Leave pitrTarget.time blank to replay all archived WAL files up to the latest point. To recover to an earlier moment, set the time in UTC, for example 2026-10-19T12:00:00Z. The time must be after the snapshot was taken.

4

Render the manifest with the snapshot settings. Replace <snapshot-class>. The render also points the recovery at the existing harness-pg-superuser secret. Without it, CNPG generates a random password for the postgres owner and replaces the real one.

5

Apply the manifest. Create the object stores first, then the cluster.

6

Verify the recovery.

pg_is_in_recovery() must return f, which means recovery finished and the cluster is promoted. The timeline is higher than the timeline of the source cluster. Check your data at the recovery point.

7

Hand the cluster to Helm. This creates the scheduled backups for the restored cluster.

Point Harness at harness-pg-restored-rw.cnpg-system.svc.cluster.local when you are ready to use the restored cluster.

Troubleshooting

The restored cluster stays in BootstrapPending

The cluster was created before its object stores. Apply the ObjectStore objects first, wait a few seconds, then apply the rest of the manifest.

ContinuousArchiving is False with a 403 error on Google Cloud Storage

The service account of the cluster needs both roles/storage.objectAdmin and roles/storage.legacyBucketReader on the bucket. The first WAL archive checks the bucket, which requires storage.buckets.get. The restored cluster has its own service account, so grant the roles to harness-pg-restored as well.

The restore fails with "Expected empty archive"

A new cluster must archive to an empty path. Set NEWPATH to a path that does not exist in the bucket, or delete the contents of the old path of a previous test restore.

Next steps

Configure external databases

Last updated

Was this helpful?