Nextcloud’s PostgreSQL database ran as a StatefulSet with a Longhorn PVC. When the Proxmox host rebooted for a kernel update, Longhorn volumes took 90 seconds to reconstruct, and during that window Nextcloud returned 500 errors. It was the same Longhorn Multi-Attach problem that pushed everything else off distributed storage on this single-host cluster. The fix: CloudNativePG (CNPG), a Kubernetes operator that manages PostgreSQL streaming replication, automatic failover and backups without depending on a distributed storage layer.
View the complete homelab infrastructure source on GitHub
Update (November 2026): the manifests below describe the two-instance pattern, not what my cluster runs today. In the repo, CNPG manages the Authelia, Firefly III, n8n, Scanopy and Synapse databases in the
databasenamespace, each withinstances: 1onlocal-path, so there is no replica to fail over to. Their base backups and WAL go to a dedicatedcnpg-backupsbucket on Garage, scheduled by aScheduledBackup. Nextcloud’s Postgres stayed a plain StatefulSet on an NFS-backed PVC and was never moved to CNPG.
Why Not Just StatefulSets?
A StatefulSet with a PersistentVolumeClaim works until the node dies. In a single-host homelab (one BMAX Mini PC* running Proxmox with k3s VMs), the usual way that happens is a Proxmox host reboot. Longhorn then reconstructs the volume from replicas, and that takes time. Meanwhile the PostgreSQL pod sits in ContainerCreating or CrashLoopBackOff.
CNPG handles this at the database level instead of the storage level. A primary and a replica run on different k3s nodes, each on plain local storage. If one k3s VM dies, CNPG promotes the replica to primary, with no storage reconstruction. A full host reboot still takes both instances down, since every VM lives on the same box, but local-path volumes come back as soon as the VMs do, with no rebuild step in the way.
Installing CNPG
The CNPG operator is deployed with a Helm chart:
helm repo add cnpg https://cloudnative-pg.github.io/charts
helm install cnpg cnpg/cloudnative-pg \
--namespace cnpg-system \
--create-namespace
The operator runs in the cnpg-system namespace and watches for Cluster CRDs.
The Cluster Definition
A CNPG PostgreSQL cluster with streaming replication:
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: nextcloud-db
namespace: nextcloud
spec:
instances: 2
postgresql:
parameters:
max_connections: "200"
shared_buffers: "256MB"
pg_hba:
- host all all 10.0.0.0/8 md5
storage:
size: 10Gi
storageClass: local-path
backup:
barmanObjectStore:
destinationPath: "s3://cnpg-backups/nextcloud-db"
endpointURL: "http://garage.apps.svc.cluster.local:3900"
s3Credentials:
accessKeyId:
name: cnpg-garage-backup
key: access-key-id
secretAccessKey:
name: cnpg-garage-backup
key: secret-access-key
retentionPolicy: "30d"
resources:
requests:
memory: "512Mi"
cpu: "250m"
limits:
memory: "1Gi"
cpu: "1"
instances: 2 means one primary and one replica, and CNPG sets up streaming replication between them automatically. The pg_hba entry allows password connections from 10.0.0.0/8, which covers both the homelab VLANs and the k3s pod network (10.42.0.0/16).
Backups with Barman
CNPG uses Barman Cloud for backups. The barmanObjectStore section points to the in-cluster Garage S3 endpoint, but to its own cnpg-backups bucket, kept deliberately separate from the bucket Velero uses: when CNPG’s archive shared Velero’s bucket, its unknown top-level directory made Velero intermittently mark the BackupStorageLocation unavailable. With it configured, CNPG archives WAL continuously, but base backups don’t run on their own: there is no default schedule, so daily backups need a ScheduledBackup resource. retentionPolicy: "30d" keeps a 30-day recovery window.
One version note: since CNPG 1.26 the in-tree barmanObjectStore integration is deprecated in favor of the Barman Cloud plugin, and the project plans to remove it from the core. It still works, but new setups should start on the plugin.
To trigger a manual backup:
kubectl cnpg backup nextcloud-db -n nextcloud
There is no in-place restore command. Recovery in CNPG means creating a new Cluster that bootstraps from the object store, optionally to a point in time:
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: nextcloud-db-restore
namespace: nextcloud
spec:
instances: 2
storage:
size: 10Gi
storageClass: local-path
bootstrap:
recovery:
source: nextcloud-db
# recoveryTarget:
# targetTime: "2026-11-01 02:30:00+00"
externalClusters:
- name: nextcloud-db
barmanObjectStore:
destinationPath: "s3://cnpg-backups/nextcloud-db"
endpointURL: "http://garage.apps.svc.cluster.local:3900"
s3Credentials:
accessKeyId:
name: cnpg-garage-backup
key: access-key-id
secretAccessKey:
name: cnpg-garage-backup
key: secret-access-key
Once it is healthy, point the application at the new cluster’s -rw service.
Failover in Practice
When the primary node fails:
- CNPG detects the primary is unreachable (timeout: 30 seconds)
- CNPG promotes the replica to primary
- CNPG moves the
-rwservice to the new primary, which accepts writes - When the failed node comes back, the old primary’s pod restarts on its local PVC and rejoins as a replica
The failover takes 30-60 seconds. During that window PostgreSQL connections drop, and Nextcloud’s application-level retries cover the gap.
Instance pods are numbered from 1 (nextcloud-db-1, nextcloud-db-2), not from 0 like a StatefulSet. The kubectl plugin shows which one is primary after the switch:
kubectl cnpg status nextcloud-db -n nextcloud
# "Current Primary" moves from nextcloud-db-1 to nextcloud-db-2
Monitoring CNPG
CNPG exposes Prometheus metrics on each instance, scraped via a PodMonitor:
# From CNPG's default monitoring queries and built-in collector
cnpg_backends_total{datname="nextcloud",state="active"} # connections by state
cnpg_pg_replication_lag # seconds, reported per instance
cnpg_collector_last_available_backup_timestamp # last base backup, Unix time
Grafana alert: cnpg_pg_replication_lag > 5. Replication is asynchronous by default, so a replica that far behind means a failover would lose the most recent writes.
The Trade-off
CNPG adds complexity: an operator, Barman for backups and streaming replication to keep an eye on. The benefit is PostgreSQL that survives a node failure without depending on Longhorn’s storage reconstruction. For a single-host homelab this is the right trade-off. The BMAX Mini PC* has enough RAM for two PostgreSQL instances, and failover is faster than waiting for Longhorn to rebuild.