How to persist data using a postgres database, Docker, and Kubernetes?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Persisting PostgreSQL data across container restarts requires explicit storage design in both Docker and Kubernetes. Without mounted volumes, data in /var/lib/postgresql/data disappears when containers are replaced. A production-ready setup uses durable volumes, proper initialization, and clear backup procedures.
Persistence in Docker With Named Volumes
A simple and robust local setup uses a named Docker volume.
With this mount, data survives container recreation:
The database starts with previous data because the volume remains.
Docker Compose Example
Compose makes repeatable local environments easier.
Add initialization SQL by mounting docker-entrypoint-initdb.d for first startup only.
Kubernetes Persistence With PVC
In Kubernetes, persistence is usually handled with a PersistentVolumeClaim and either a StatefulSet or a single Deployment for non-critical environments.
StatefulSet gives stable identity and is the standard choice for databases in Kubernetes.
Data Safety Beyond Volumes
Persistent volumes protect against pod restarts, not all failures. You still need:
- logical backups with
pg_dump - periodic restore tests
- monitoring for disk and replication lag
Backup example:
For clusters, run backup jobs as scheduled workloads and copy artifacts to object storage.
Migration Path From Docker to Kubernetes
Teams often start in Docker and move to Kubernetes later. Keep configuration portable:
- same major PostgreSQL version across environments
- same schema migration pipeline
- environment variables managed through secrets, not hardcoded values
Use migrations at startup only with strong locking guarantees to avoid parallel migration race conditions.
Health Checks and Startup Ordering
Persistence alone is not enough if application pods start before PostgreSQL is ready. Add readiness probes and dependency retry logic so services do not fail permanently on first boot.
For application deployments, include connection retry with exponential backoff. This keeps startup stable during node rescheduling and storage attach delays.
Common Pitfalls
- Running PostgreSQL in Kubernetes without persistent volume claims.
- Using
Deploymentwith random pod identity for stateful production workloads. - Storing credentials in plain manifests instead of secrets.
- Assuming volume persistence replaces backups.
- Upgrading major PostgreSQL versions without migration rehearsal.
Summary
- Data persistence requires mounting PostgreSQL data directory to durable storage.
- Docker named volumes are sufficient for local and small environments.
- Kubernetes should use PVC-backed
StatefulSetfor stable stateful behavior. - Persistence and backups solve different risks and both are required.
- Standardized versioning and migration workflows make Docker to Kubernetes transitions safer.
Related reading
- How to prevent scale down of newly scaled up pod for specific period of time which was created by HPA in Kubernetes?
- How to properly recover a K8s cluster after reboot?
- How to publicly expose Traefik ingress controller on Google Cloud Container Engine?
- How to pull docker images hosted on Google Container Registry via Kubernetes kubernetes included on docker for desktop
- How to prevent docker from starting a container automatically on system startup?
- How to publish ports in docker files?
- How to pg_dump an RDS Postgres database?
- How to pick a Kafka transaction.id

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.