Overview
PostgreSQL Highly Available deploys a production-ready PostgreSQL cluster using Patroni for automatic leader election and failover, with etcd providing distributed consensus. An optional HAProxy workload routes all write traffic to the current primary replica, providing a stable connection endpoint regardless of which replica holds the leader role.For production use, maintain at least 3 PostgreSQL replicas and 3 etcd replicas. etcd requires an odd number of replicas (3, 5, 7) for quorum.
What Gets Created
- Stateful Workload — A Patroni-managed PostgreSQL cluster with configurable replica count and resources. Each replica has its own volume.
- etcd Workload — A dedicated etcd cluster (via dependency) providing distributed consensus for Patroni leader election.
- HAProxy Workload (optional, enabled by default) — A leader-routing proxy that always directs write traffic to the current primary replica.
- Volume Set — Persistent storage for PostgreSQL data, with optional autoscaling.
- Secrets — A dictionary secret with database credentials; opaque secrets for the Patroni startup script, HAProxy startup script, and WAL-G backup script (created as needed).
- Identity & Policy — An identity bound to the workload with
revealaccess to all required secrets, and cloud storage access when backup is enabled.
This template does not create a GVC. You must deploy it into an existing GVC.
Installation
This template has no external prerequisites unless backup is enabled. To install, follow the instructions for your preferred method:UI
Browse, install, and manage templates visually
CLI
Manage templates from your terminal
Terraform
Declare templates in your Terraform configurations
Pulumi
Declare templates in your Pulumi programs
Configuration
The defaultvalues.yaml for this template:
Credentials
postgres.username— PostgreSQL superuser username. Change before deploying to production.postgres.password— PostgreSQL superuser password. Change before deploying to production.postgres.database— Name of the database created on first startup.
These values are only applied on first startup when the data directory is empty. Updating them after the initial deployment will have no effect on the running database. To change credentials or the database name on an existing instance, use PostgreSQL’s native commands (e.g.
ALTER USER, ALTER DATABASE).PostgreSQL Cluster
replicas— Number of PostgreSQL replicas. Minimum 3 recommended for production.resources.minCpu/resources.minMemory— Minimum CPU and memory guaranteed per replica.resources.maxCpu/resources.maxMemory— Maximum CPU and memory per replica.multiZone— Spread replicas across availability zones within the location.
Storage
volumeset.capacity— Initial volume size in GiB (minimum 10). Each replica gets its own volume.volumeset.autoscaling.enabled— Automatically expand the volume as it fills. When enabled:maxCapacity— Maximum volume size in GiB.minFreePercentage— Trigger a scale-up when free space drops below this percentage.scalingFactor— Multiply the current capacity by this factor when scaling up.
etcd Cluster
etcd.replicas— Number of etcd replicas. Must be an odd number (3, 5, 7) for quorum.etcd.resources.cpu/etcd.resources.memory— CPU and memory per etcd replica.etcd.multiZone— Spread etcd replicas across availability zones.etcd.volumeset.capacity— Initial volume size for etcd data in GiB.etcd.internal_access.type— Controls which workloads can reach the etcd cluster.
HAProxy (Strongly Recommended)
In a Patroni cluster, only the leader replica accepts writes — other replicas are read-only. HAProxy automatically routes write traffic to the current leader, providing a stable connection endpoint even during failover.proxy.enabled— Deploy the HAProxy leader-routing workload (default:true).proxy.resources.cpu/proxy.resources.memory— CPU and memory per HAProxy replica.proxy.minReplicas/proxy.maxReplicas— Replica count for the proxy workload.
HAProxy must be enabled (
proxy.enabled: true) for logical backups to function correctly. WAL-G backups do not require the proxy.Internal Access
internal_access.type— Controls which workloads can connect to PostgreSQL on port5432:
| Type | Description |
|---|---|
same-gvc | Allow access from all workloads in the same GVC |
same-org | Allow access from all workloads in the same organization |
workload-list | Allow access only from specific workloads listed in workloads |
Connecting to PostgreSQL
Connect to PostgreSQL through the HAProxy workload, which always routes to the current leader:Backup
Two backup modes are available. Setbackup.enabled: true, choose a mode, and configure the storage provider.
| Mode | Description |
|---|---|
logical | Scheduled SQL dumps via pg_dump. Portable and suitable for smaller databases. Requires HAProxy to be enabled. |
wal-g | Continuous WAL archiving with point-in-time recovery. Suitable for larger databases requiring minimal data loss. Runs as a sidecar container alongside PostgreSQL. |
backup.mode—logicalorwal-g.backup.provider—awsorgcp.backup.resources.cpu/backup.resources.memory— Resources allocated to the backup container.
backup.logical.schedule— Cron expression for backup frequency (default: daily at 2am UTC).
backup.walg.intervalSeconds— Interval between base backups in seconds (default:21600, every 6 hours).
Backup Prerequisites
AWS S3
Before enabling backup withprovider: aws, complete the following in your AWS account:
- Create an S3 bucket. Set
backup.aws.bucketto the bucket name andbackup.aws.regionto its region. - If you do not have a Cloud Account set up, refer to the docs to Create a Cloud Account. Set
backup.aws.cloudAccountNameto its name. - Create an IAM policy with the following JSON, replacing
YOUR_BUCKET_NAME:
- Set
backup.aws.policyNameto the name of the policy created in step 3. - Set
backup.aws.prefixto the folder path where backups will be stored.
GCS
Before enabling backup withprovider: gcp, complete the following in your GCP account:
- Create a GCS bucket. Set
backup.gcp.bucketto the bucket name. - If you do not have a Cloud Account set up, refer to the docs to Create a Cloud Account. Set
backup.gcp.cloudAccountNameto its name. - Add the Storage Admin role to the GCP service account associated with the Cloud Account.
- Set
backup.gcp.prefixto the folder path where backups will be stored.
Restoring a Backup
Logical
Run the following from a client with access to the backup bucket. Connect through the proxy workload so the restore targets the current leader. AWS S3:WAL-G
WAL-G point-in-time restore requires an empty data directory. Follow these steps:- Run
wal-g backup-listto identify the desired backup. - Stop the PostgreSQL workload.
- Create a new Volume Set for the restored data.
- Run a one-off restore workload with the new Volume Set mounted at
/var/lib/postgresql/dataand run:
- Re-point the PostgreSQL workload to the restored Volume Set and restart.
- After the restore, change
backup.walg.prefixbefore re-enabling backups to avoid system identifier conflicts.