Running PostgreSQL on Kubernetes is now widespread practice, and with it comes a question that leaves no room for improvisation: how do we back up the data and, above all, how do we recover it? In this article we go over the fundamentals of database backups in Kubernetes, the most common options for PostgreSQL, and the points that usually fail in real life. There are no shortcuts: a backup that has never been restored is, for all practical purposes, a hypothesis.
A StatefulSet is not a backup
It is common to confuse persistence with data protection. A Kubernetes cluster usually mounts PersistentVolumeClaims (PVCs) on storage that, with most providers, is replicated. That replication protects against the failure of a physical disk, but not against a DROP TABLE, logical corruption, accidental deletion, or an application error that overwrites records.
Kubernetes documentation warns that persistent volumes have a lifecycle independent of the pod, but that independence is not the same as a recoverable point-in-time history. If you need to go back to yesterday's state at 14:03, you need a backup system with retention, not just persistent storage.
The practical consequence is a separation of responsibilities:
- The platform (Kubernetes) guarantees that the pod reattaches to its volume.
- The database engine (PostgreSQL) guarantees logical and transactional integrity.
- The backup system must be a component of its own, with its own lifecycle, verification, and rotation.
What to back up: the PostgreSQL model
PostgreSQL offers two main backup approaches, and it is worth understanding them before choosing a tool, because they condition the design in Kubernetes:
- Logical dumps (
pg_dump/pg_dumpall). They produce an SQL file or a custom format that is restored withpg_restore. They are portable across versions and architectures, but they do not offer point-in-time recovery and, on large databases, the backup window and the restoration cost grow. - Physical backup and WAL archiving (
pg_basebackupplus continuous archiving of the Write-Ahead Log). They allow restoring to a specific instant (Point-In-Time Recovery, PITR) by replaying the WAL from a base backup up to the desired moment. This is the model used by production-grade backup tools.
The official PostgreSQL documentation describes both approaches in its backup and restore chapter, and recommends WAL archiving for point-in-time recovery. The choice between one and the other is not ideological: it depends on the RPO (how much data you can lose) and the RTO (how long it takes you to come back).
A point that is often overlooked: the backup must include the relevant roles and configuration. A pg_dump of a single database does not include the cluster roles or necessarily the global objects; pg_dumpall does cover them. Design what you back up and document what is left out.
Operators: the common pattern in Kubernetes
Instead of orchestrating pg_dump or pg_basebackup by hand with CronJobs, the operator pattern has become popular in Kubernetes. An operator extends the Kubernetes API with its own resource (for example, a CustomResource that describes the cluster) and a controller that reconciles the actual state with the desired state.
CloudNativePG is an open-source operator for PostgreSQL, currently a Cloud Native Computing Foundation project, which manages the cluster lifecycle and offers backups to S3-compatible object storage, with support for point-in-time recovery. Its documentation describes both on-demand and scheduled backups and the recovery process.
The advantage of the pattern is the same as in the rest of Kubernetes: the backup is a declarative, versionable, and auditable object, instead of a script hidden on a server. The disadvantage is the usual one: if the operator is misconfigured, the failure is silent. A backup resource can report success without the artifact being restorable.
Physical backup tools: pgBackRest and similar
For physical copies with WAL archiving, pgBackRest is a widely adopted tool in the PostgreSQL ecosystem. It supports full, incremental, and differential backups, backup verification, compression and encryption, and is designed to integrate with repositories in object storage. Its documentation details the backup, check, and restore operations.
The typical combination in Kubernetes is an operator that orchestrates pgBackRest against an external bucket. That detail — the bucket outside the cluster — matters: if the backup storage lives on the same PVC or with the same provider as the data, an incident on that platform can take both down. The golden rule is that the copy must reside in a system with a different failure domain than the database.
Backup tools at the Kubernetes cluster level, such as Velero, cover another case: protection of API objects (manifests, PVCs depending on configuration) and migration between clusters. They are complementary, not substitutes for a consistent backup of the database engine: a volume snapshot without coordination with PostgreSQL can capture an inconsistent state.
Verify the restoration, not just the backup
Here is the point that separates a backup system from an illusion. A backup is considered valid when it has been restored and verified in a separate environment. The practices that reduce risk are concrete:
- Periodic test restoration. Schedule restore drills and record the actual time it takes, not the time you estimate. That figure is your measured RTO.
- Repository verification. Tools such as
pgBackRestinclude commands to check the integrity of the repository; run them on a scheduled basis. - Logical validation after the restore. It is not enough for the engine to start: check counts of key tables, checksums, or known business queries.
- Alerts on the backup, not just on the database. If a backup job fails silently for a week, you will find out at the worst moment.
- Credential separation. Access to the backup bucket should not be the same as operational access to the database cluster.
An operational detail in Kubernetes: restoration processes usually require creating a new cluster or a temporary pod pointing at the repository. Document that procedure step by step and keep it versioned alongside the platform code, not in the on-call engineer's head.
Design checklist for your first serious backup
Before considering the design closed, answer these questions in writing:
- What RPO and RTO does the business accept? From that come the frequency, the method (logical vs. physical+PITR), and the repository size.
- Where does the backup live? It must be a different failure domain than the PVC and than the original provider.
- Who restores it and how? A documented procedure, with segregated credentials and a test environment.
- How do I know it worked? Alert on the job, repository verification, and scheduled test restore.
- What am I not backing up? Roles, configurations, secrets, and global objects; document it explicitly.
The lesson that repeats in real incidents is not technological but procedural: the team only discovers that its backup was useless on the day it tries to restore. Designing with operators, external storage, and periodic verification turns a promise into a demonstrable capability. That is what makes PostgreSQL backups in Kubernetes stop causing scares.
Article prepared by the LanciaTech technical team. Statements about tools and products refer to the public documentation of each project; always consult the version you use in production.
Sources
- PostgreSQL Global Development Group — Backup and Restore (official documentation): https://www.postgresql.org/docs/current/backup.html
- PostgreSQL Global Development Group — Continuous Archiving and Point-in-Time Recovery (PITR): https://www.postgresql.org/docs/current/continuous-archiving.html
- PostgreSQL Global Development Group — pg_dump: https://www.postgresql.org/docs/current/app-pgdump.html
- CloudNativePG — official documentation: https://cloudnative-pg.io/documentation/
- pgBackRest — official documentation: https://pgbackrest.org/
- Kubernetes — Persistent Volumes (official documentation): https://kubernetes.io/docs/concepts/storage/persistent-volumes/
- Velero — official documentation: https://velero.io/docs/