cnpg-database-manager

by encircle360 GmbH

Helm chartv0.5.0App 1.0Apache-2.0Verified publisher

Multi-database PostgreSQL management for CloudNativePG with declarative extensions, connection pooling, disaster recovery, and automated backups

About this chart

cnpg-database-manager

Multi-database PostgreSQL management for CloudNativePG with declarative extensions, connection pooling, disaster recovery, and automated backups

Homepage: https://github.com/encircle360-oss/helm-charts/tree/main/charts/cnpg-database-manager

⚠️ UNDER CONSTRUCTION This chart is currently under active development and has not been battle-tested in production environments. NOT PRODUCTION READY - Use at your own risk and thoroughly test in non-production environments first.

Why This Chart?

The official CloudNativePG cluster chart only supports single database per deployment. This chart fills the gap by providing:

  • Multi-Database Management: Deploy multiple databases in one PostgreSQL cluster
  • Declarative Role Management: Automatic PostgreSQL user/role creation via CloudNativePG managed roles
  • Database CRD Integration: Uses CloudNativePG's Database CRD for proper lifecycle management
  • Multi-Tenant Ready: Perfect for consolidating multiple lightweight databases
  • Flexible Configuration: Support for any CloudNativePG feature via additionalClusterSpec

Use this chart when you want to consolidate multiple applications into one PostgreSQL cluster while maintaining security isolation.

Prerequisites

  • Kubernetes 1.24+
  • Helm 3.8+
  • CloudNativePG operator installed (v1.25+)

Installing the Chart

To install the chart with the release name my-databases:

helm repo add encircle360-oss https://encircle360-oss.github.io/helm-charts/
helm repo update
helm install my-databases encircle360-oss/cnpg-database-manager

Uninstalling the Chart

To uninstall/delete the my-databases deployment:

helm uninstall my-databases

Configuration

Basic Example - Single Cluster with Multiple Databases

clusters:
  main:
    enabled: true
    instances: 3
    imageName: ghcr.io/cloudnative-pg/postgresql:17.2
    storage:
      size: 50Gi
      storageClass: standard
    databases:
      - name: keycloak
        owner: keycloak
      - name: paperless
        owner: paperless
      - name: n8n
        owner: n8n

This creates:

  • One PostgreSQL cluster named main with 3 instances (HA setup)
  • Three Database CRDs (main-keycloak, main-paperless, main-n8n)
  • Three managed PostgreSQL roles (keycloak, paperless, n8n)
  • Three role password secrets (main-keycloak-password, main-paperless-password, main-n8n-password)

Multi-Cluster Setup

clusters:
  production:
    enabled: true
    instances: 3
    imageName: ghcr.io/cloudnative-pg/postgresql:17.2
    storage:
      size: 100Gi
    databases:
      - name: api-prod
        owner: api
      - name: web-prod
        owner: web
 
  staging:
    enabled: true
    instances: 1
    imageName: ghcr.io/cloudnative-pg/postgresql:17.2
    storage:
      size: 20Gi
    databases:
      - name: api-staging
        owner: api
      - name: web-staging
        owner: web

Connection Pooling (PgBouncer)

clusters:
  main:
    enabled: true
    instances: 3
    imageName: ghcr.io/cloudnative-pg/postgresql:17.2
    storage:
      size: 50Gi
    databases:
      - name: app-db
        owner: app
    poolers:
      - name: rw
        type: rw  # read-write pooler
        instances: 3
        poolMode: transaction
        parameters:
          max_client_conn: "1000"
          default_pool_size: "25"
        monitoring:
          enablePodMonitor: true
      - name: ro
        type: ro  # read-only pooler
        instances: 5
        poolMode: transaction

Backup Configuration (S3)

clusters:
  main:
    enabled: true
    instances: 3
    imageName: ghcr.io/cloudnative-pg/postgresql:17.2
    storage:
      size: 50Gi
    backup:
      enabled: true
      schedule: "0 0 0 * * *"  # Daily at midnight (6-term cron)
      retentionPolicy: "30d"
      s3:
        bucket: my-postgres-backups
        region: eu-central-1
        path: /cluster-main
        accessKeyId: ""
        secretAccessKey: ""
    databases:
      - name: production-db
        owner: app

Backup Configuration (Azure Blob Storage)

clusters:
  main:
    enabled: true
    instances: 3
    imageName: ghcr.io/cloudnative-pg/postgresql:17.2
    storage:
      size: 50Gi
    backup:
      enabled: true
      schedule: "0 0 0 * * *"  # Daily at midnight (6-term cron)
      retentionPolicy: "30d"
      azure:
        destinationPath: "https://mystorageaccount.blob.core.windows.net/backups/main"
        inheritFromAzureAD: true  # Use Azure AD Workload Identity
    databases:
      - name: production-db
        owner: app

Backup Configuration (Google Cloud Storage)

clusters:
  main:
    enabled: true
    instances: 3
    imageName: ghcr.io/cloudnative-pg/postgresql:17.2
    storage:
      size: 50Gi
    backup:
      enabled: true
      schedule: "0 0 0 * * *"  # Daily at midnight (6-term cron)
      retentionPolicy: "30d"
      gcs:
        bucket: my-postgres-backups
        path: cluster-main
        gkeEnvironment: true  # Use GKE Workload Identity
    databases:
      - name: production-db
        owner: app

Volume Snapshot Backups

When using method: volumeSnapshot with a schedule, the chart automatically creates a ScheduledBackup CRD that triggers backups according to the cron schedule.

⚠️ Important: Cron Schedule Format

CloudNativePG uses 6-term cron expressions (not standard 5-term cron):

FieldAllowed ValuesDescription
Seconds0-59When to trigger (usually 0)
Minutes0-59Minute of the hour
Hours0-23Hour of the day
Day1-31Day of the month
Month1-12Month of the year
Weekday0-6Day of the week (0 = Sunday)

Common Examples:

  • "0 0 * * * *" - Every hour at minute 0
  • "0 0 0 * * *" - Daily at midnight
  • "0 30 2 * * *" - Daily at 2:30 AM
  • "0 0 */6 * * *" - Every 6 hours
  • "0 0 2 * * 0" - Every Sunday at 2:00 AM

⚠️ Common Mistake:

Do NOT use standard 5-term cron like "0 * * * *" - this will be interpreted as running every minute (when seconds=0), not every hour!

For more details, see CloudNativePG Backup Documentation.

Basic Volume Snapshot Backup:

clusters:
  main:
    enabled: true
    instances: 3
    imageName: ghcr.io/cloudnative-pg/postgresql:17.2
    storage:
      size: 50Gi
    backup:
      enabled: true
      schedule: "0 0 */3 * * *"  # Every 3 hours (6-term cron)
      retentionPolicy: "30d"
      method: volumeSnapshot
      volumeSnapshot:
        className: longhorn-backup  # Your VolumeSnapshotClass
        online: true
        onlineConfiguration:
          immediateCheckpoint: false
          waitForArchive: true
    databases:
      - name: production-db
        owner: app

Advanced Configuration with Backup Target:

clusters:
  main:
    enabled: true
    instances: 3
    imageName: ghcr.io/cloudnative-pg/postgresql:17.2
    storage:
      size: 50Gi
    backup:
      enabled: true
      schedule: "0 0 */3 * * *"  # Every 3 hours (6-term cron)
      method: volumeSnapshot
      target: prefer-standby  # Use standby for backups (reduces primary load)
      immediate: true         # Create immediate backup on deployment
      volumeSnapshot:
        className: longhorn-backup
        online: true
    databases:
      - name: production-db
        owner: app

How it works:

  1. Chart creates the Cluster with backup.volumeSnapshot configuration
  2. Chart creates a ScheduledBackup CRD that triggers backups on schedule
  3. CloudNativePG operator creates Backup resources automatically
  4. Kubernetes CSI creates VolumeSnapshot resources
  5. Storage driver (Longhorn, etc.) backs up to configured target (S3/NFS)

Verify backups:

kubectl get scheduledbackup
kubectl get backup
kubectl get volumesnapshot

Disaster Recovery and Point-in-Time Recovery (PITR)

clusters:
  recovered:
    enabled: true
    instances: 3
    imageName: ghcr.io/cloudnative-pg/postgresql:17.2
    storage:
      size: 100Gi
    databases:
      - name: myapp
        owner: appuser
    recovery:
      enabled: true
      source: production-cluster
      recoveryTarget:
        targetTime: "2025-01-03T14:30:00Z"
        exclusive: false
    externalClusters:
      - name: production-cluster
        barmanObjectStore:
          destinationPath: "s3://my-backups/prod-cluster"
          endpointURL: "https://s3.amazonaws.com"
          s3Credentials:
            accessKeyId:
              name: backup-creds
              key: ACCESS_KEY_ID
            secretAccessKey:
              name: backup-creds
              key: ACCESS_SECRET_KEY
          wal:
            maxParallel: 8

Monitoring with Prometheus and PrometheusRule

clusters:
  main:
    enabled: true
    instances: 3
    imageName: ghcr.io/cloudnative-pg/postgresql:17.2
    storage:
      size: 50Gi
    monitoring:
      enabled: true
      podMonitor:
        enabled: true
      prometheusRule:
        enabled: true
        labels:
          team: platform
        # Optional: Custom alert rules (otherwise defaults are used)
        rules:
          - alert: CustomDatabaseSize
            annotations:
              summary: "Database size exceeds threshold"
            expr: |
              cnpg_pg_database_size_bytes > 100000000000
            for: 10m
            labels:
              severity: warning
    databases:
      - name: app-db
        owner: app

Image Catalog for Centralized Image Management

imageCatalogs:
  postgresql:
    enabled: true
    kind: ImageCatalog  # or ClusterImageCatalog for cluster-wide
    images:
      - major: 15
        image: ghcr.io/cloudnative-pg/postgresql:15.6
      - major: 16
        image: ghcr.io/cloudnative-pg/postgresql:16.2
      - major: 17
        image: ghcr.io/cloudnative-pg/postgresql:17.2

clusters:
  main:
    enabled: true
    instances: 3
    # Use imageCatalogRef instead of imageName for auto-updates
    imageCatalogRef:
      apiGroup: postgresql.cnpg.io
      kind: ImageCatalog
      name: postgresql
      major: 17
    storage:
      size: 50Gi
    databases:
      - name: app-db
        owner: app

Multiple Databases in One Cluster

clusters:
  main:
    enabled: true
    instances: 3
    imageName: ghcr.io/cloudnative-pg/postgresql:17.2
    storage:
      size: 50Gi
    databases:
      - name: keycloak
        owner: keycloak
      - name: paperless
        owner: paperless

PostgreSQL Extensions (Vector, PostGIS, etc.)

CloudNativePG supports declarative extension management through the Database CRD (requires CloudNativePG 1.26+). Extensions like pgvector, postgis, and pg_stat_statements are included in the default PostgreSQL images.

Simple Extension Setup (pgvector for Immich):

clusters:
  main:
    enabled: true
    instances: 3
    imageName: ghcr.io/cloudnative-pg/postgresql:17.2
    storage:
      size: 50Gi
    databases:
      - name: immich
        owner: immich
        extensions:
          - name: vector  # pgvector for AI/ML embeddings

Advanced Extension Configuration:

clusters:
  main:
    enabled: true
    instances: 3
    imageName: ghcr.io/cloudnative-pg/postgresql:17.2
    storage:
      size: 100Gi
    databases:
      - name: gis-app
        owner: gis
        extensions:
          - name: postgis
            version: "3.4.0"
            schema: public
          - name: postgis_topology
            schema: topology

      - name: timeseries-app
        owner: timeseries
        extensions:
          - name: timescaledb
          - name: pg_stat_statements

Common Extensions Available:

  • vector - pgvector for AI/ML embeddings (Immich, ChatGPT apps)
  • postgis - Geospatial data support
  • timescaledb - Time-series database
  • pg_stat_statements - Query performance tracking
  • uuid-ossp - UUID generation
  • hstore - Key-value storage
  • pg_trgm - Trigram matching for fuzzy search

Important Notes:

  • Extensions are managed per-database (not cluster-wide)
  • CloudNativePG automatically handles CREATE EXTENSION / DROP EXTENSION
  • Many extensions are pre-installed in the official CloudNativePG PostgreSQL images
  • For extensions requiring superuser privileges, CloudNativePG handles this automatically
  • Requires CloudNativePG operator version 1.26.0 or higher

Separate WAL Storage (Performance Optimization)

For performance-critical workloads, you can separate WAL (Write-Ahead Log) storage onto a different volume. This enables parallel I/O and better storage tier optimization.

Basic WAL Storage:

clusters:
  main:
    enabled: true
    instances: 3
    imageName: ghcr.io/cloudnative-pg/postgresql:17.2

    # Main PGDATA storage
    storage:
      size: 50Gi
      storageClass: longhorn-postgres

    # Separate WAL storage
    walStorage:
      enabled: true
      size: 10Gi
      storageClass: longhorn-postgres-wal  # Can be faster storage tier

Different Storage Classes:

clusters:
  main:
    enabled: true
    instances: 3
    imageName: ghcr.io/cloudnative-pg/postgresql:17.2

    # PGDATA on standard storage
    storage:
      size: 50Gi
      storageClass: standard-ssd

    # WAL on faster storage tier
    walStorage:
      enabled: true
      size: 10Gi
      storageClass: fast-nvme

Advanced PVC Template Configuration:

cluster

Deploy cnpg-database-manager with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has cnpg-database-manager on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartcnpg-database-manager is pinned at v0.5.0 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below
opentenant validate opentenant.yaml
terraform init && terraform apply