Kubernetes storageClass for Postgresql database
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
For PostgreSQL on Kubernetes, a StorageClass is not just a way to get "some disk." It defines how persistent volumes are provisioned, what performance profile they have, how they are bound to nodes, and what happens to the underlying storage when claims are deleted.
What Matters for PostgreSQL
A database cares about storage behavior more than stateless applications do. The key concerns are:
- low and predictable latency
- durable writes
- correct access mode
- safe reclaim behavior
- the ability to resize or snapshot volumes
That is why the best StorageClass for PostgreSQL depends on the storage backend and workload, not on PostgreSQL alone.
A Typical StorageClass Example
Here is a reasonable AWS EBS CSI example for a single-instance PostgreSQL workload:
The important parts are:
- '
Retainso the disk is not automatically destroyed when the claim is removed' - '
WaitForFirstConsumerso scheduling and volume placement happen together' - '
allowVolumeExpansionso the PVC can grow later'
For PostgreSQL, Retain is usually safer than Delete because accidental data loss is expensive.
Use a StatefulSet, Not a Deployment
For PostgreSQL, you usually want a StatefulSet with a volume claim template rather than a plain Deployment.
That ensures PostgreSQL gets stable storage and predictable volume naming across restarts.
Choose the Access Mode Carefully
For most block-storage-backed PostgreSQL deployments, ReadWriteOnce is the correct access mode. A single PostgreSQL instance writes to one volume from one node. Trying to force a shared writable filesystem is often the wrong design for a primary database pod.
If you are running a replicated PostgreSQL setup, each replica should normally have its own volume rather than multiple pods writing to the same data directory.
Backend Choice Matters
The right parameters differ by provider:
- AWS EBS and GCE PD are common for single-node block storage
- Ceph or other CSI drivers may offer snapshots and advanced policies
- network filesystems are usually less ideal for primary PostgreSQL data files unless the platform is tuned specifically for database workloads
Do not assume a default cluster StorageClass is good enough for a write-heavy database. Check latency, throughput, and failure behavior.
Reclaim Policy Is a Real Safety Decision
For disposable workloads, Delete is convenient. For PostgreSQL, it can be catastrophic if an operator removes a PVC by mistake. Retain means extra manual cleanup, but it also means the data can survive an application-level mistake.
That tradeoff is usually worth it for databases.
Common Pitfalls
- Using a
Deploymentfor PostgreSQL when aStatefulSetis the better fit. - Keeping the default
StorageClasswithout checking its latency and reclaim behavior. - Using
Deletereclaim policy on important database data without realizing the risk. - Trying to share one writable volume across multiple PostgreSQL pods.
- Forgetting
WaitForFirstConsumer, which can lead to poor volume placement in multi-zone clusters.
Summary
- PostgreSQL needs a deliberately chosen
StorageClass, not just any persistent volume. - Prefer a
StatefulSetwith a PVC template andReadWriteOncestorage. - '
Retain,WaitForFirstConsumer, and volume expansion are strong defaults for many database setups.' - Choose storage parameters based on actual database workload and backend behavior.
- Treat reclaim policy and backend latency as data-safety decisions, not minor YAML details.
Related reading
- Kubernetes support for Internal Load Balancers in AWS
- Kubernetes TLS Ingress route with cert-manager and SelfSigned ClusterIssuer not working
- Kubernetes Tolerations - why do we need to defined Effect on the pod
- kubernetes unhealthy ingress backend
- Kubernetes Version Support Period
- Kubernetes vs. CloudFoundry
- Laravel-5 'LIKE' equivalent Eloquent
- Laravel Migration Error Syntax error or access violation 1071 Specified key was too long; max key length is 767 bytes

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.