PostgreSQL on Kubernetes

A hands-on introduction to CloudNativePG

Gabriele Bartolini
VP, Distinguished Engineer, EDB
Co-founder and Maintainer, CloudNativePG (CNCF)
19 November 2026 ยท Cloud Native Denmark 2026, Copenhagen

G. Bartolini ยท Cloud Native Denmark 2026

About me

Gabriele Bartolini

VP, Distinguished Engineer @ EDB

CloudNativePG maintainer

PostgreSQL Contributor

gabrielebartolini.it
G. Bartolini ยท Cloud Native Denmark 2026

Download the slides

Follow along, copy the commands, keep them for later

Slides
  • Open the slides on your laptop, or scan the QR code
  • Commands and manifests are one click away
G. Bartolini ยท Cloud Native Denmark 2026

1. Setup

G. Bartolini ยท Cloud Native Denmark 2026

Before you arrived

The cnpg-playground: you should already have it running

git clone https://github.com/cloudnative-pg/cnpg-playground.git
cd cnpg-playground
REGISTRY_CACHE=true ./scripts/setup.sh
export KUBECONFIG=$PWD/k8s/kube-config.yaml
  • Two Kind clusters (k8s-eu, k8s-na), one per "region", each with a RustFS S3-compatible object store
  • Needs Docker, Git, kubectl, the cnpg plugin, Kind and jq (optional: Headlamp Desktop)
  • Takes about 2 to 3 minutes (the first run also pulls images)
  • REGISTRY_CACHE=true runs pull-through registry caches
    • For docker.io, ghcr.io, quay.io and registry.k8s.io, so images are downloaded once, even across clusters
  • Nothing deployed on top of Kind yet
cnpg-playground
G. Bartolini ยท Cloud Native Denmark 2026

The playground architecture

Two regions, each with a Kind cluster and an object store

G. Bartolini ยท Cloud Native Denmark 2026

Exercise: deploy the full demo

Latest main, both regions

git checkout main && git pull
TRUNK=true ./demo/setup.sh
  • TRUNK=true: CloudNativePG and the Barman Cloud plugin from main
    • The 1.31 development line
  • Installs CloudNativePG, cert-manager, the Barman Cloud plugin and an image catalog in both clusters
  • Deploys pg-eu (primary) in the EU cluster and pg-na (replica cluster) in the NA cluster
  • Takes 10 to 15 minutes, depending on your connection
    • Leave it running, we'll check it after the intro
G. Bartolini ยท Cloud Native Denmark 2026

What the demo deploys

G. Bartolini ยท Cloud Native Denmark 2026

Be gentle with the demo

  • pg-eu, pg-na and the objectstore-eu / objectstore-na object stores belong to the demo
  • Read them, and write to the app database as much as you like
  • Don't switch over, delete or break pg-eu
    • pg-na follows it, and the demo can't be redeployed quickly
  • Break things in your own cluster, pg
G. Bartolini ยท Cloud Native Denmark 2026

What do you expect from this workshop,
and what would you like to cover?

G. Bartolini ยท Cloud Native Denmark 2026

A proposed agenda

We probably won't cover everything: the rest is here to continue at your own pace

  1. Setup: the full demo deployment, running while we talk
  2. Meet CloudNativePG, while it deploys
  3. One cluster: explore what the operator manages
  4. Extensions: image catalogs and extension images
  5. High availability: switchover and failover
  6. Continuous backup and recovery, including PITR
  7. From one cluster to two: the distributed topology
  8. What's new in 1.31, the book, where next
  9. Questions and answers
Slides
G. Bartolini ยท Cloud Native Denmark 2026

Ground rules

  • Work in pairs if you can; ask early, ask often
  • Each module ends with a checkpoint: if you fall behind, catch up there
  • Some manifests are on the slides' website: save them locally
    • Some need editing before you apply them
  • We probably won't cover everything
    • What's left is here for you to continue later, at your own pace
  • The slides are yours: scan the QR code to keep every link and manifest at hand
  • We'll finish with a Q&A session
Slides
G. Bartolini ยท Cloud Native Denmark 2026

2. Meet CloudNativePG

G. Bartolini ยท Cloud Native Denmark 2026

What is CloudNativePG?

A Kubernetes operator for PostgreSQL

  • Manages the full lifecycle of a PostgreSQL cluster: deploy, scale, update, back up, recover
  • Open source (Apache 2.0), created by EDB, now a CNCF Incubating project
  • Runs upstream, community PostgreSQL: no fork, no lock-in

cloudnative-pg.io
G. Bartolini ยท Cloud Native Denmark 2026

Main features

  • High availability: primary and replicas, automated failover, planned switchover
  • Backup and recovery: continuous WAL archiving, base backups, PITR
  • Replica clusters: distributed topologies across regions
  • Declarative management of Postgres global objects
    • database roles and databases (dedicated CRDs)
    • tablespaces (as PVCs)
  • Security: TLS everywhere by default
  • Operations: rolling updates, connection pooling, Prometheus metrics, structured logs
  • Extensions: pluggable images, no custom builds (PostgreSQL 18 or later)
Capability levels
G. Bartolini ยท Cloud Native Denmark 2026

The ecosystem

One organisation, many sub-projects

Sub-projects
G. Bartolini ยท Cloud Native Denmark 2026

Plugins

CNPG-I: the plugin interface

  • Barman Cloud: the reference plugin, backup and WAL archive to object storage
  • Klio: multi-tier backup and recovery (documentation: cloudnative-pg.io/klio)
  • Plugins run beside the operator; the Cluster references them by name (CNPG-I)
  • The playground demo uses Barman Cloud (and Klio optionally)
Barman Cloud
Klio
G. Bartolini ยท Cloud Native Denmark 2026

A graphical view: Headlamp

The CNPG plugin for Headlamp

  • A new CloudNativePG project: we're launching it with this workshop
  • Browse Cluster, Pooler, Backup, ScheduledBackup and Database resources
  • Health at a glance: phase, WAL archiving, last backup, replication lag
  • Per-instance logs, a psql terminal and live metrics
  • Scale, switchover, fence and hibernate from the UI
  • Optional: install it from the Headlamp Desktop Plugin Catalog
    • Needs Headlamp 0.42 or later; search for cnpg-headlamp-plugin
    • Use it beside kubectl
cnpg-headlamp-plugin
G. Bartolini ยท Cloud Native Denmark 2026

Checkpoint: setup

  • Both Kind clusters are up (./scripts/info.sh)
  • KUBECONFIG is set and kubectl --context kind-k8s-eu get nodes shows six Ready nodes
  • The operator is running (kubectl get pods -n cnpg-system)
  • pg-eu and pg-na are healthy (kubectl cnpg status pg-eu)
  • From here on: kubectl config use-context kind-k8s-eu
G. Bartolini ยท Cloud Native Denmark 2026

3. One cluster

G. Bartolini ยท Cloud Native Denmark 2026

How CloudNativePG thinks

  • Declarative: a Cluster resource describes the desired state
  • Reconciliation: the operator converges reality to it
  • No external HA tooling: the operator talks to the instance manager in each Pod
    • PostgreSQL runs as its child
  • Kubernetes primitives first: a PVC per instance, native streaming replication, Services, Secrets
G. Bartolini ยท Cloud Native Denmark 2026

Exercise: deploy your own cluster

Preparing for the high availability section (pg.yaml)

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: pg
spec:
  instances: 2
  storage:
    size: 1Gi
kubectl apply -f pg.yaml
kubectl cnpg status pg
  • We create pg now to prepare for the high availability section
    • It starts in the background
  • In this section we explore the existing pg-eu cluster instead
G. Bartolini ยท Cloud Native Denmark 2026

Exercise: look at the demo cluster

Read only: just look

kubectl apply view-last-applied cluster pg-eu
kubectl get cluster pg-eu -o yaml
kubectl get mutatingwebhookconfigurations
  • The first command shows the manifest the demo applied; the second, what the operator filled in
  • What's the difference? That's convention over configuration
    • The defaults come from a mutating admission webhook in the operator
G. Bartolini ยท Cloud Native Denmark 2026

Exercise: what the operator created

Read only, on the demo cluster pg-eu

kubectl get pods -o wide -l cnpg.io/cluster=pg-eu \
  -L cnpg.io/instanceRole,topology.kubernetes.io/zone

kubectl get pvc,services,endpointslices,secrets,pdb -l cnpg.io/cluster=pg-eu
  • The operator labels everything it creates with cnpg.io/cluster=<cluster name>: use -l to select it
  • Which Pod is the primary? On which node, in which zone?
  • Which Service always points at it? The EndpointSlices show which Pod IPs each Service targets
  • Which Secrets hold credentials and certificates?
  • Or browse the cluster and its resources in Headlamp, with the CNPG plugin
G. Bartolini ยท Cloud Native Denmark 2026

Exercise: connect and look around

Read only, on the demo cluster pg-eu

kubectl cnpg psql pg-eu -- -c '\du' -c '\l'
kubectl run psql --rm -it --restart=Never \
  --image=ghcr.io/cloudnative-pg/postgresql:18-minimal-trixie -- \
  psql "$(kubectl get secret pg-eu-app -o jsonpath='{.data.uri}' | base64 -d)"
kubectl cnpg logs cluster pg-eu -f | kubectl cnpg logs pretty
  • The first command runs psql inside the primary's Pod, so it requires pods/exec permission
  • The second connects through the pg-eu-rw Service, like an application would
    • With the credentials in the pg-eu-app Secret
  • Keep the log window (the last command) open while you explore
  • Logs go to standard output as JSON, never to a file
G. Bartolini ยท Cloud Native Denmark 2026

Checkpoint: one cluster

kubectl cnpg status pg
kubectl get pods,pvc,services,secrets -l cnpg.io/cluster=pg-eu
  • pg is a healthy two-instance cluster
  • You can connect through the read-write service of pg-eu
    • With the psql Pod of the previous exercise
  • You know which objects the operator created for pg-eu
G. Bartolini ยท Cloud Native Denmark 2026

4. Extensions

G. Bartolini ยท Cloud Native Denmark 2026

Container images

Operand images, extension images and image catalogs

  • Operand images: ghcr.io/cloudnative-pg/postgresql
    • Tags such as 18-minimal-trixie and 18-standard-trixie
    • Based on Debian, available for amd64 and arm64
  • Extension images:
  • Image catalogs: a ClusterImageCatalog maps a major version to an image
    • Clusters say "PostgreSQL 18" and not an image tag
    • They also list the extensions compatible with that PostgreSQL version
    • And the component images for tools like PgBouncer
Operand images
Extension images
G. Bartolini ยท Cloud Native Denmark 2026

Exercise: look at the image catalog

Read only: just look

kubectl get clusterimagecatalogs.postgresql.cnpg.io
kubectl get clusterimagecatalog postgresql-minimal-trixie -o yaml
  • The playground deploys the postgresql-minimal-trixie image catalog
    • With the latest published extensions
  • Which PostgreSQL major versions does it cover? Which extensions?
G. Bartolini ยท Cloud Native Denmark 2026

Extensions with an image catalog

pgvector on PostgreSQL 18, no custom image (pgvector-cluster.yaml)

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: pgvector
spec:
  imageCatalogRef:
    apiGroup: postgresql.cnpg.io
    kind: ClusterImageCatalog
    name: postgresql-minimal-trixie
    major: 18
  instances: 1
  storage:
    size: 1Gi
  postgresql:
    extensions:
      - name: pgvector
kubectl apply -f pgvector-cluster.yaml
CNPG Recipe 26
G. Bartolini ยท Cloud Native Denmark 2026

Extensions: the Database resource

Then enable it in the app database (pgvector-database.yaml)

apiVersion: postgresql.cnpg.io/v1
kind: Database
metadata:
  name: pgvector-app
spec:
  name: app
  owner: app
  cluster:
    name: pgvector
  extensions:
  - name: vector
    version: '0.8.7'
kubectl apply -f pgvector-database.yaml
CNPG Recipe 26
G. Bartolini ยท Cloud Native Denmark 2026

Checkpoint: extensions

kubectl cnpg status pgvector
kubectl cnpg psql pgvector -- app -c '\dx'
  • pgvector is a healthy cluster
  • The vector extension is installed in the app database
G. Bartolini ยท Cloud Native Denmark 2026

5. High availability

G. Bartolini ยท Cloud Native Denmark 2026

Switchover and failover

  • Switchover: planned, controlled promotion of a replica
  • Failover: unplanned, the operator promotes the most advanced replica
  • Applications keep using the same read-write service
  • The primary is guarded by a Lease named after the cluster: whoever holds it runs as the primary
G. Bartolini ยท Cloud Native Denmark 2026

Exercise: a controlled switchover

In two other terminals, keep watching the lease and the logs of pg

kubectl get lease -w pg
kubectl cnpg logs cluster pg -f | kubectl cnpg logs pretty
kubectl cnpg status pg
kubectl cnpg promote pg <replica-pod-name>
kubectl cnpg status pg
kubectl get lease pg -o jsonpath='{.metadata.annotations.cnpg\.io/position}'
  • Which Pod was the primary before, and which one is now?
  • Who holds the Lease, and when did it change?
  • The cnpg.io/position annotation: LSN, timeline, state and Pod. What happened to the timeline?
  • What happened to the old primary?
G. Bartolini ยท Cloud Native Denmark 2026

Exercise: remove the primary

Keep the lease and log terminals open

kubectl cnpg status pg
kubectl delete pod --now -l cnpg.io/cluster=pg,cnpg.io/instanceRole=primary
kubectl cnpg status pg
  • Watch the log window: how long did recovery take?
  • Watch the lease terminal: who holds the Lease now, and when did it change?
  • Where's the primary now? What did the old one do?
  • Delete a replica instead: what's different?
G. Bartolini ยท Cloud Native Denmark 2026

Checkpoint: high availability

kubectl cnpg status pg
kubectl get lease pg
  • You promoted a new primary on purpose
  • You removed one and watched the operator recover
  • The old primary rejoined as a replica
  • The Lease holder is the current primary
G. Bartolini ยท Cloud Native Denmark 2026

6. Backup and recovery

G. Bartolini ยท Cloud Native Denmark 2026

Continuous backup to object storage

  • Base backups plus continuous WAL archiving
  • Recovery to any point within the retention window
  • A backup you haven't restored is only a hope
G. Bartolini ยท Cloud Native Denmark 2026

Exercise: look at the demo's backup setup

kubectl get objectstore objectstore-eu -o yaml
kubectl get cluster pg-eu -o json | jq '.spec.plugins'
  • Where does pg-eu point at the Barman Cloud plugin?
  • Which bucket, which endpoint, which serverName?
G. Bartolini ยท Cloud Native Denmark 2026

Exercise: take a backup

On the demo cluster pg-eu (imperative backups)

Imperative backups
kubectl cnpg backup pg-eu --method=plugin \
  --plugin-name=barman-cloud.cloudnative-pg.io
kubectl get backup -w
  • Wait until the backup is completed before the next exercise
  • Add --dry-run to the first command to see the Backup manifest
    • It is a normal resource, so you can also write it as YAML
G. Bartolini ยท Cloud Native Denmark 2026

Exercise: simulate a mistake

In the app database of pg-eu

kubectl cnpg psql pg-eu -- app -c 'create table t (id int)'
kubectl cnpg psql pg-eu -- app -c 'insert into t values (1), (2)'
kubectl cnpg psql pg-eu -- app -c 'select now()'
kubectl cnpg psql pg-eu -- app -c 'delete from t where id = 2'
kubectl cnpg psql pg-eu -- app -c 'select pg_switch_wal()'
  • Two rows, then note the time: that's the recovery target
  • Deleting the row with id = 2 is the "mistake"
  • pg_switch_wal() closes the current WAL segment
    • So it gets archived and the recovery can reach the time you noted
G. Bartolini ยท Cloud Native Denmark 2026

Exercise: point-in-time recovery

A new cluster, pg-recall, recovered to the noted time (abridged: full manifest)

spec:
  bootstrap:
    recovery:
      source: pg-eu
      recoveryTarget:
        targetTime: "<the time you noted>"
  externalClusters:
  - name: pg-eu
    plugin:
      name: barman-cloud.cloudnative-pg.io
      parameters:
        barmanObjectName: objectstore-eu
        serverName: pg-eu
# first save the full manifest and replace targetTime with the time you noted
kubectl apply -f pg-recall.yaml
kubectl get cluster pg-recall -w
G. Bartolini ยท Cloud Native Denmark 2026

Checkpoint: backup and recovery

kubectl get backup pg-eu-backup
kubectl cnpg status pg-eu
kubectl cnpg status pg-recall
kubectl cnpg psql pg-recall -- app -c 'select * from t'
  • The base backup is completed
  • WAL archiving for pg-eu works
  • pg-recall recovered and is healthy
  • The deleted row (id = 2) is back in pg-recall: both rows are there
G. Bartolini ยท Cloud Native Denmark 2026

7. From one cluster to two

G. Bartolini ยท Cloud Native Denmark 2026

Distributed topologies

  • A replica cluster in the second region
  • Bootstrapped from object storage or by streaming
  • The basis for disaster recovery and migration
G. Bartolini ยท Cloud Native Denmark 2026

Exercise: look at the demo's two regions

kubectl --context kind-k8s-eu get cluster pg-eu -o jsonpath='{.spec.replica}'
kubectl --context kind-k8s-na get cluster pg-na -o jsonpath='{.spec.replica}'
kubectl --context kind-k8s-na cnpg status pg-na
  • Which cluster is the primary? Which is a replica cluster?
  • Where does pg-na get its data from?
  • Is cascading replication in use?
G. Bartolini ยท Cloud Native Denmark 2026

Checkpoint: two regions

kubectl --context kind-k8s-na cnpg status pg-na
G. Bartolini ยท Cloud Native Denmark 2026

8. What's new in 1.31

G. Bartolini ยท Cloud Native Denmark 2026

1.31: declarative Postgres

Due 1 December 2026

  • Database privileges: GRANT / REVOKE CONNECT, CREATE, TEMPORARY on a Database (#10826, in progress)
  • Schema privileges: CREATE and USAGE on managed schemas (#7872)
  • Managed roles: auto-generated password Secrets (#3788)
  • Role client certificates: duration and renewBefore on DatabaseRole (#11312)
G. Bartolini ยท Cloud Native Denmark 2026

1.31: generated passwords

A DatabaseRole that creates and rotates its own password (abridged: full manifest)

spec:
  cluster:
    name: pg
  name: angus
  login: true
  password:
    mode: generate
    duration: 720h
    renewBefore: 24h
  databaseRoleReclaimPolicy: delete
kubectl apply -f angus-role.yaml
kubectl get secret angus-password
  • The operator writes the password to the angus-password Secret and rotates it before it expires
  • Rotate now: kubectl annotate databaserole angus cnpg.io/rotatePassword=now
CNPG Recipe 28
G. Bartolini ยท Cloud Native Denmark 2026

1.31: resilience and operations

  • Primary Lease hardening for failover and switchover (#11721, in progress)
    • The lease was introduced in 1.30
  • In-place Pod resource resize, using the Kubernetes 1.33 feature (#8070)
  • OpenTelemetry log streaming, with no DaemonSet (#11085, in progress)
  • Beside the operator: Klio, the multi-tier backup and recovery plugin (experimental, v0.0.21)
G. Bartolini ยท Cloud Native Denmark 2026

The book

PostgreSQL on Kubernetes

Build, operate and scale production-grade databases with CloudNativePG

Gabriele Bartolini and Leonardo Cecchi, Packt

  • The examples in this workshop come from the book
    • Chapters 3, 5 and 8
  • Available for preorder, and in Early Access on Packt
packtpub.com
G. Bartolini ยท Cloud Native Denmark 2026

Clean up

When you're done, at the end or later from home

# either: remove the clusters, keep the registry caches
./scripts/teardown.sh

# or: remove the clusters and the registry caches
REMOVE_REGISTRY_CACHE=true ./scripts/teardown.sh
  • Both remove the Kind clusters and the object stores
  • Keeping the registry caches makes the next setup faster
    • Remove them only if you don't plan to use the playground again
G. Bartolini ยท Cloud Native Denmark 2026

Where to look next

cloudnative-pg.io
G. Bartolini ยท Cloud Native Denmark 2026

Join the community

You don't need to be an expert in operator development

Contributing
G. Bartolini ยท Cloud Native Denmark 2026

Key takeaways

  • Postgres on Kubernetes is declarative: the operator reconciles your Cluster
  • Extensions are images chosen from a catalog: no custom builds
  • High availability is built in: switchover, failover and the primary Lease
  • Backup and recovery work through plugins
    • Continuous WAL archiving, base backups and PITR
  • Replica clusters across regions are the basis for disaster recovery
  • Everything you did today uses upstream, open tooling
  • Keep going: the slides and manifests are yours, and the community needs you
G. Bartolini ยท Cloud Native Denmark 2026

Get these slides

Speaker notes stripped, safe to share

Slides
G. Bartolini ยท Cloud Native Denmark 2026

Thank you. Questions?

Gabriele Bartolini

VP, Distinguished Engineer, EDB

gabrielebartolini.it

Postgres, PostgreSQL, and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with permission. CloudNativePG is a trademark of The Linux Foundation.
cnpg-playground
Slides
G. Bartolini ยท Cloud Native Denmark 2026