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.
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, andyqversion 4.PostgreSQL version: 16.
Extensions:
pg_partman,pg_cron, andhstore.Backups only: cert-manager, a Google Cloud Storage bucket, and volume snapshot support. Go to Check volume snapshot requirements to review the requirements.
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.
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.
Install the cluster.
The primary is available at harness-pg-rw.cnpg-system.svc.cluster.local:5432.
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.
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.
Create the secrets. The copy uses the existing postgres secret to connect to the current database.
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.
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.
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.
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 asstandard-rwo.Snapshot components: The
VolumeSnapshot,VolumeSnapshotContent, andVolumeSnapshotClasscustom resource definitions, and the snapshot controller.Snapshot class: A
VolumeSnapshotClassfor the CSI driver.
If you do not have a snapshot class, create one.
Go to Volume snapshots on GKE to review the GKE requirements.
Set up backups
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.
Add the snapshot settings to the cluster. Replace <snapshot-class> with your VolumeSnapshotClass.
Go to Volume snapshot backups to review these settings.
Create the backup values. The schedule runs daily at 02:00. The six fields start with seconds.
Apply the backup values. The plugin adds a sidecar container, so the instances restart one after the other. Run this step outside peak hours.
Verify the backup.
The first backup shows completed, the snapshot shows READYTOUSE true, and ContinuousArchiving is True.
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.
Choose the snapshot. The restored cluster must archive to a new, empty path.
Give the new cluster access to the bucket. Its service account is harness-pg-restored.
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.
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.
Apply the manifest. Create the object stores first, then the cluster.
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.
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
Next steps
Configure external databasesLast updated
Was this helpful?