Skip to content

PostgreSQL#

Warning

This part of the Helm Chart is still influx and may change significantly. Use with caution.

This chapter covers how to provision a managed PostgreSQL database using the CloudNativePG (CNPG) operator.

Prerequisites#

The CloudNativePG operator must be installed in your Kubernetes cluster before enabling this feature. The chart creates CNPG custom resources — the operator handles the actual database lifecycle.

Quick Start#

The minimum configuration for a PostgreSQL instance:

persistence:
  postgres:
    enabled: true

When database.password is empty, the chart generates a random password in its managed credentials Secret and retains it on upgrades. This creates:

  • A single-instance CNPG Cluster running PostgreSQL 17.5
  • A database named app owned by user app
  • A Secret (kubernetes.io/basic-auth) with the credentials

Defaults#

Setting Default
Bootstrap mode initdb (fresh cluster)
Database name app
Database owner app
Image ghcr.io/cloudnative-pg/postgresql:17.5
Instances 1
Storage 1Gi
CPU requests/limits 250m / 500m
Memory requests/limits 512Mi / 1Gi

Production Configuration#

A more complete setup with multiple replicas and tuned resources:

persistence:
  postgres:
    enabled: true
    description: "Production database for my-app"
    instanceCount: 3
    storageSize: 50Gi

    image:
      repository: ghcr.io/cloudnative-pg/postgresql
      tag: 17.5

    database:
      name: myapp
      owner: myapp_user
      password: "strong-password-here"

    parameters:
      max_connections: "200"
      shared_buffers: "256MB"
      effective_cache_size: "768MB"

    resources:
      requests:
        cpu: "1"
        memory: 2Gi
      limits:
        cpu: "2"
        memory: 4Gi

Using an External Secret#

Instead of putting the password in values.yaml, reference an existing Kubernetes Secret:

persistence:
  postgres:
    enabled: true
    database:
      name: app
      owner: app
      externalSecret: my-existing-pg-secret

The secret must be of type kubernetes.io/basic-auth with username and password keys.

Additional Databases#

Provision extra databases on the same cluster using the CNPG Database CRD:

persistence:
  postgres:
    enabled: true
    database:
      password: "changeme"
    additionalDatabases:
      - name: analytics
        owner: analyst
      - name: audit_log
        owner: app
        encoding: UTF8

Each entry creates a separate Database custom resource managed by CNPG.

Disabling Read-Only Services#

By default, CNPG creates -ro (read-only) and -r (read) services. The chart disables these by default. To re-enable:

persistence:
  postgres:
    enabled: true
    disabledDefaultServices: false
    database:
      password: "changeme"

Bootstrapping the Schema#

Use initSQL to run SQL statements in the application database immediately after the cluster is initialised. This is the right place to create tables, indexes, or extensions your application expects to exist on first boot.

persistence:
  postgres:
    enabled: true
    database:
      name: chirpydb
      owner: chirpy
      password: "changeme"
    initSQL:
      - "CREATE TABLE IF NOT EXISTS chirps (id SERIAL PRIMARY KEY, content TEXT NOT NULL CHECK (char_length(content) <= 200), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);"
      - "CREATE INDEX IF NOT EXISTS idx_chirps_created_at ON chirps (created_at);"

Note

initSQL only runs during the very first cluster initialisation (mode: initdb). It is ignored during recovery bootstraps. For schema migrations that must run on every deploy, handle them inside the application itself (e.g. using a migration library or an initContainer).

Backups#

The chart supports automated backups via the Barman Cloud Plugin.

Prerequisites#

The Barman Cloud Plugin (barman-cloud.cloudnative-pg.io) must be installed in the cluster.

Enable Backups#

persistence:
  postgres:
    enabled: true
    database:
      password: "changeme"
    backup:
      enabled: true
      schedule: "0 0 0 * * *"        # Daily at midnight (6-field cron, includes seconds)
      retentionPolicy: "30d"
      objectStore:
        destinationPath: s3://my-bucket/backups/
        endpointURL: http://minio:9000    # Omit for actual AWS S3
        s3Credentials:
          accessKeyId:
            name: minio-creds             # Secret name
            key: ACCESS_KEY_ID            # Key within the Secret
          secretAccessKey:
            name: minio-creds
            key: ACCESS_SECRET_KEY

This creates three things:

  1. A plugins block on the Cluster enabling WAL archiving
  2. An ObjectStore CR defining the backup destination and credentials
  3. A ScheduledBackup CR with the cron schedule

Backup Options#

Option Description Default
schedule Six-field cron (includes seconds) "0 0 0 * * *"
immediate Take a backup immediately on creation false
suspend Pause scheduled backups false
target primary or prefer-standby (unset)
retentionPolicy e.g. "30d", "4w", "6m" "30d"
objectStore.wal.compression gzip, bzip2, snappy, or "" "gzip"

AWS S3 vs MinIO#

The endpointURL field is only required for S3-compatible stores like MinIO. Omit it when using AWS S3 directly.

Recovery#

From a Backup Object#

Restore from an existing CNPG Backup resource in the same namespace:

persistence:
  postgres:
    enabled: true
    mode: recovery
    database:
      password: "changeme"
    recovery:
      backupName: "daily-backup-20250324"

From an External Cluster (PITR)#

Restore from an external cluster's object store, optionally to a specific point in time:

persistence:
  postgres:
    enabled: true
    mode: recovery
    database:
      password: "changeme"
    recovery:
      source: origin-cluster
      recoveryTarget:
        targetTime: "2025-03-24T10:00:00Z"
    externalClusters:
      - name: origin-cluster
        plugin:
          name: barman-cloud.cloudnative-pg.io
          parameters:
            barmanObjectName: origin-backup
            serverName: origin-cluster

Recovery Target Options#

Target Description
targetTime RFC 3339 timestamp (e.g., "2025-03-24T10:00:00Z")
targetXID Transaction ID
targetName Named restore point (from pg_create_restore_point())
targetLSN WAL Log Sequence Number
targetImmediate Recover to earliest consistent state
exclusive Stop before the target instead of after

Protecting Against Accidental Deletion#

Prevent Helm from deleting the Postgres cluster on helm uninstall:

persistence:
  postgres:
    enabled: true
    preventDelete: true
    database:
      password: "changeme"

Connecting Your Application#

The CNPG Cluster creates a service named <release>-<chart>-postgres-rw OR <global-nameOverride>-postgres-rw. Connect your app using environment variables:

env:
  DATABASE_HOST: "<name>-postgres-rw"
  DATABASE_PORT: "5432"
  DATABASE_NAME: "app"
  DATABASE_USER:
    valueFrom:
      secretKeyRef:
        name: <name>-postgres-credentials
        key: username
  DATABASE_PASSWORD:
    valueFrom:
      secretKeyRef:
        name: <name>-postgres-credentials
        key: password

What's Next#

Continue to Redis to deploy a Redis cluster alongside your application.