PostgreSQL on Kubernetes with CloudNativePG

February 13, 2026 | PostgreSQL Kubernetes

Production PostgreSQL on K8s with operator.

Running PostgreSQL on Kubernetes with CloudNativePG

CloudNativePG (CNPG) is a Kubernetes operator that manages the full lifecycle of PostgreSQL clusters — from provisioning and scaling to backup, recovery, and major version upgrades. It's designed for production workloads and is CNCF-certified.

Why Use an Operator?

  • Automated failover with zero data loss (synchronous replication)
  • Continuous backup to S3/Azure Blob with barman-cloud
  • Rolling updates and major version upgrades
  • Connection pooling with built-in PgBouncer
  • Monitoring integration with Prometheus

Installation

# Install CloudNativePG operator
kubectl apply -f https://raw.githubusercontent.com/cloudnative-pg/cloudnative-pg/release-1.22/releases/cnpg-1.22.0.yaml

# Verify operator is running
kubectl get pods -n cnpg-system

Create a PostgreSQL Cluster

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: my-postgres
spec:
  instances: 3
  primaryUpdateStrategy: unsupervised
  
  storage:
    size: 50Gi
    storageClass: gp3
  
  postgresql:
    parameters:
      shared_buffers: "2GB"
      effective_cache_size: "6GB"
      max_connections: "200"
  
  backup:
    barmanObjectStore:
      destinationPath: s3://my-backups/postgres/
      s3Credentials:
        accessKeyId:
          name: aws-creds
          key: ACCESS_KEY_ID
        secretAccessKey:
          name: aws-creds
          key: SECRET_ACCESS_KEY
    retentionPolicy: "30d"
  
  monitoring:
    enablePodMonitor: true

Accessing the Database

# Get connection credentials
kubectl get secret my-postgres-app -o jsonpath='{.data.uri}' | base64 -d

# Port-forward for local access
kubectl port-forward svc/my-postgres-rw 5432:5432

Scheduled Backups

apiVersion: postgresql.cnpg.io/v1
kind: ScheduledBackup
metadata:
  name: daily-backup
spec:
  schedule: "0 2 * * *"
  cluster:
    name: my-postgres
  backupOwnerReference: self

Eazy SaaS Tip: CloudNativePG is our recommended approach for PostgreSQL on Kubernetes. It handles the complex operational tasks (failover, backup, upgrades) that would otherwise require manual intervention and custom scripting.