Skip to content

PostgreSQL

Every app that needs PostgreSQL gets its own CloudNative-PG cluster from the shared kubernetes/components/database/postgres component. There is no shared database server. Apps that need MySQL or MariaDB use the MariaDB component instead.

Using the component

Add the component and its health check to the app's Flux Kustomization:

spec:
  components:
    - ../../../../components/database/postgres
  healthCheckExprs:
    - apiVersion: postgresql.cnpg.io/v1
      kind: Cluster
      current: has(status.conditions) && status.conditions.exists(e, e.type == 'Ready' && e.status == 'True')
  healthChecks:
    - apiVersion: postgresql.cnpg.io/v1
      kind: Cluster
      name: myapp-postgres
      namespace: <namespace>
  postBuild:
    substitute:
      APP: *app

Flux only evaluates healthCheckExprs for the resources listed in healthChecks (or with wait: true), so both are needed. The expression only defines current: a cluster that is still starting or switching over counts as in progress until the Kustomization's timeout, instead of failing at once.

Variable Default Notes
APP (required) Name of the consuming app; used for cluster, Secret and backup paths
POSTGRES_USERNAME ${APP} Owner role created on initial bootstrap
POSTGRES_DATABASE ${APP} Database created on initial bootstrap
POSTGRES_BACKUP_SCHEDULE 23 0 * * * Cron schedule for the local NFS dump. Set it per app to <minute> 0 * * * with the minute from just k8s cron-minute <app>, so the dumps are spread over the hour
POSTGRES_BASE_BACKUP_SCHEDULE 0 40 4 * * * Barman base backup to Garage: six fields, seconds first, in the operator's time zone (Europe/Amsterdam, injected by k8tz, like the MariaDB operator's). Set it per app to its own slot in the 04:xx hour, see Backups

A brand-new database needs one extra label on first deploy. See New Database.

Connecting from an app

CNPG names everything after the Cluster, ${APP}-postgres. It generates a ${APP}-postgres-app Secret with the keys uri, jdbc-uri, username, password, host, port, dbname and pgpass. In an app-template HelmRelease:

env:
  DATABASE_URL:
    valueFrom:
      secretKeyRef:
        name: "${APP}-postgres-app"
        key: uri

uri points at the read-write primary Service ${APP}-postgres-rw. There is no PgBouncer Pooler. Apps connect directly. If transaction-mode pooling is ever needed, add a CNPG Pooler per cluster.

Bootstrap behavior

By default the Cluster bootstraps with bootstrap.recovery from Barman at s3://postgresql/${APP}/${POSTGRES_DATABASE}/. Delete the Cluster CR and its PVCs, and it rebuilds from the latest base backup plus WAL replay.

Same-path, same-serverName rebuilds work because of the cnpg.io/skipEmptyWalArchiveCheck: enabled annotation. The recovered cluster inherits the source's system_identifier and writes new WAL on a new timeline, so names don't collide.

Backups

  • Barman Cloud (ScheduledBackup, daily) with continuous WAL archiving to Garage S3 at s3://postgresql/${APP}/${POSTGRES_DATABASE}/, retentionPolicy: 14d. Each app's base backup has its own slot, eight minutes apart (a base backup takes up to about five):

    Time App
    04:00 autobrr
    04:08 immich
    04:16 litellm
    04:24 mail-archiver
    04:32 paperless
    04:40 windshift

    04:50 is the MariaDB physical backup. Until November 2026 every base backup started at 04:40; uploading together, one or two aborted with HTTP 499 on Garage's UploadPart every few nights, while WAL archiving kept working. A new app takes the next free slot. - Local dumps with postgres-backup-local to an NFS share on the NAS, keeping 7 daily, 4 weekly and 1 monthly. The per-app folder /mnt/vault/Backups/Database/${APP} must exist first, see New Database.

Trigger a manual base backup with just k8s db-backup <namespace> <app>.

Administration is through pgAdmin in the default namespace.