Skip to main content

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 Patroni PostgreSQL Workload — A Patroni-managed PostgreSQL cluster with configurable replica count and resources. Each replica has its own volume. Leadership is gracefully handed off before any replica is shut down, ensuring write availability is maintained during deployments and restarts.
  • etcd Workload — A dedicated etcd cluster providing distributed consensus for Patroni leader election.
  • HAProxy Leader-Routing Workload (optional, enabled by default) — Routes write traffic to the current primary replica, providing a stable connection endpoint during failover.
  • PgBouncer Workload (optional) — A connection pooler deployed in front of HAProxy, multiplexing application connections into a smaller pool of real database connections.
  • 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 reveal access 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 default values.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.
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. HAProxy exposes two HTTP endpoints on the proxy workload for observability:
  • 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 port 5432:

PgBouncer Connection Pooling

PgBouncer is an optional connection pooler that sits in front of HAProxy, multiplexing application connections into a smaller pool of real database connections. HAProxy handles leader routing and failover transparently — PgBouncer is unaware of which PostgreSQL replica is the primary. Enabling PgBouncer automatically enables HAProxy, as it is required for leader-aware routing in the HA cluster. When enabled, PgBouncer becomes the primary connection endpoint:
  • pgbouncer.poolMode — Controls how connections are reused:
  • pgbouncer.defaultPoolSize — Number of real PostgreSQL connections PgBouncer maintains per pod (default: 25).
  • pgbouncer.maxClientConn — Maximum number of client connections PgBouncer accepts per pod (default: 1000).
  • pgbouncer.maxDbConnections — Hard cap on total PostgreSQL connections across all PgBouncer pods. Prevents connection blowout when PgBouncer scales out — set this to a value the PostgreSQL primary can safely handle (default: 100).
  • pgbouncer.minReplicas / pgbouncer.maxReplicas — PgBouncer autoscales on RPS between these bounds. Increase maxReplicas for high-throughput workloads.
  • pgbouncer.resources.cpu / pgbouncer.resources.memory — Resources allocated to each PgBouncer pod.

Application Retry Logic

Your application should implement retry logic on database connections. Several normal cluster events surface transient errors to the client:
  • Patroni failover — During a leader election, the old primary steps down and a new one is promoted. HAProxy detects the change and re-routes writes, but connections in flight during the transition may receive a connection reset or brief refusal.
  • Rolling restarts and deployments — Each replica is restarted in turn. Leadership is handed off gracefully before the leader shuts down, but applications may see a momentary connection disruption while the new leader is established and HAProxy updates its routing.
  • Scale down — Removing replicas triggers a shutdown sequence. If the departing replica held the leader role, a new election occurs and writes are unavailable until the new leader is ready.
These events are expected and short-lived. Implementing exponential backoff with retry on connection errors ensures your application recovers automatically without surfacing errors to end users.

Connecting to PostgreSQL

Connect to PostgreSQL through the HAProxy workload, which always routes to the current leader:

Backup

Two backup modes are available. Set backup.enabled: true, choose a mode, and configure the storage provider.
  • backup.modelogical or wal-g.
  • backup.provideraws, gcp, or minio.
  • backup.resources.cpu / backup.resources.memory — Resources allocated to the backup container.
Logical backup settings:
  • backup.logical.schedule — Cron expression for backup frequency (default: daily at 2am UTC).
WAL-G backup settings:
  • backup.walg.intervalSeconds — Interval between base backups in seconds (default: 21600, every 6 hours).

Backup Prerequisites

AWS S3

Before enabling backup with provider: aws, complete the following in your AWS account:
  1. Create an S3 bucket. Set backup.aws.bucket to the bucket name and backup.aws.region to its region.
  2. If you do not have a Cloud Account set up, refer to the docs to Create a Cloud Account. Set backup.aws.cloudAccountName to its name.
  3. Create an IAM policy with the following JSON, replacing YOUR_BUCKET_NAME:
  1. Set backup.aws.policyName to the name of the policy created in step 3.
  2. Set backup.aws.prefix to the folder path where backups will be stored.

GCS

Before enabling backup with provider: gcp, complete the following in your GCP account:
  1. Create a GCS bucket. Set backup.gcp.bucket to the bucket name.
  2. If you do not have a Cloud Account set up, refer to the docs to Create a Cloud Account. Set backup.gcp.cloudAccountName to its name.
  3. Add the Storage Admin role to the GCP service account associated with the Cloud Account.
  4. Set backup.gcp.prefix to the folder path where backups will be stored.

MinIO

Before enabling backup with provider: minio, ensure your MinIO instance is accessible:
  1. Create a bucket in MinIO. Set backup.minio.bucket to its name.
  2. Set backup.minio.endpoint to the MinIO S3 API address including the port. For the minio marketplace template deployed in the same GVC, use http://WORKLOAD_NAME:9000.
  3. Set backup.minio.accessKey and backup.minio.secretKey to the MinIO root credentials — these match the admin.username and admin.password values from the MinIO template installation.
  4. Set backup.minio.prefix to the folder path where backups will be stored.
MinIO backup requires no Control Plane Cloud Account — credentials are passed directly. Both logical and wal-g modes are supported; WAL-G uses its native S3-compatible storage support (AWS_ENDPOINT + path-style addressing).

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:
GCS:
MinIO:

WAL-G

WAL-G point-in-time restore requires an empty data directory. Follow these steps:
  1. Run wal-g backup-list to identify the desired backup.
  2. Stop the PostgreSQL workload.
  3. Create a new Volume Set for the restored data.
  4. Run a one-off restore workload with the new Volume Set mounted at /var/lib/postgresql/data and run:
  1. Re-point the PostgreSQL workload to the restored Volume Set and restart.
  2. After the restore, change backup.walg.prefix before re-enabling backups to avoid system identifier conflicts.

External References

Patroni Documentation

Patroni automatic failover documentation

PostgreSQL Documentation

Official PostgreSQL documentation

etcd Documentation

Official etcd documentation

PgBouncer Documentation

PgBouncer configuration reference

Backup Image Source

Source code for the PostgreSQL backup container image

PostgreSQL HA Template

View the source files, default values, and chart definition