The cloud-native approach runs PostgreSQL as containers managed by orchestration tools, enabling easy scaling, self-healing, and DevOps-friendly workflows.
Running PostgreSQL in containers lets you version infrastructure, apply rolling updates, and replicate environments quickly. Kubernetes or Docker Swarm handle health checks, fail-over, and horizontal scaling, freeing you from manual server care.
Use the official postgres:alpine
tag for a small footprint or postgres:XX.X
for version-locked deployments.Pin tags to avoid unexpected upgrades.
docker run -d --name pg-ecom -e POSTGRES_PASSWORD=secret -e PGDATA=/var/lib/postgresql/data/pgdata -v pg_vol:/var/lib/postgresql/data -p 5432:5432 postgres:15-alpine
The volume pg_vol
ensures persistent storage even if the container restarts.
Set DATABASE_URL=postgresql://postgres:secret@pg-ecom:5432/ecommerce?sslmode=disable
.Use Kubernetes Service
names (e.g., pg-ecom.default.svc
) instead of container names in clusters.
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: pg-ecom
spec:
serviceName: pg-ecom
replicas: 1
selector:
matchLabels:
app: pg-ecom
template:
metadata:
labels:
app: pg-ecom
spec:
containers:
- name: postgres
image: postgres:15-alpine
env:
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: pg-secret
key: password
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: pg-data
volumeClaimTemplates:
- metadata:
name: pg-data
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 10Gi
Attach to the pod and run psql -f seed_ecommerce.sql
.Maintain SQL migrations in a GitOps workflow for repeatability.
Use sidecar containers running pg_dump
to an object store or integrate the wal-g
image for continuous archiving. Schedule CronJobs for logical dumps if point-in-time recovery isn’t required.
Add replicas to the StatefulSet, then configure replicaCount
in Helm charts or set --replicas
via kubectl.Route read-only traffic with PgBouncer or application-level replica hosts.
• Pin image tags
• Store passwords in Secrets
• Use StatefulSet
over Deployment
• Monitor with pg_stat_statements
and Prometheus
• Perform regular disaster-recovery tests
.
Yes. Create a new container with the target version, replicate WAL via streaming or logical replication, then switch application traffic after verification.
Connection pooling is still critical because Kubernetes restarts pods and microservices often. PgBouncer smooths connection spikes and short-lived connections.
Estimate 2× your current database size plus projected six-month growth and WAL. Kubernetes allows online PVC expansion for most storage classes.