How to Deploy PostgreSQL on Kubernetes?

PostgreSQL is an open-source RDBMS known for handling complex datasets. Kubernetes automates deployment, scaling, and management of containerized applications. Deploying PostgreSQL on Kubernetes provides automated scaling, rolling updates, and improved reliability through replicas and failover.

Deployment Steps

Cluster Image Manifest Deploy Monitor

Step 1: Set Up Kubernetes Cluster

Use a cloud provider (AWS EKS, GCP GKE, Azure AKS) or set up locally with Minikube. Install kubectl and optionally Helm.

Step 2: Create Deployment Manifest

Define PostgreSQL deployment in a YAML file with environment variables, volumes, and ports ?

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres:16
        ports:
        - containerPort: 5432
        env:
        - name: POSTGRES_USER
          value: "admin"
        - name: POSTGRES_PASSWORD
          value: "secretpass"
        - name: POSTGRES_DB
          value: "mydb"
        volumeMounts:
        - mountPath: /var/lib/postgresql/data
          name: postgres-storage
      volumes:
      - name: postgres-storage
        persistentVolumeClaim:
          claimName: postgres-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: postgres-svc
spec:
  selector:
    app: postgres
  ports:
  - port: 5432
    targetPort: 5432
  type: ClusterIP

Step 3: Deploy and Monitor

# Deploy
kubectl apply -f postgres-manifest.yaml

# Check pod status
kubectl get pods

# View logs
kubectl logs <pod-name>

# Monitor resources
kubectl top pods

Step 4: Scale and Backup

# Scale replicas
kubectl scale deployment postgres --replicas=3

# Backup using pg_dump
kubectl exec <pod-name> -- pg_dump -U admin mydb > backup.sql

Conclusion

Deploying PostgreSQL on Kubernetes combines robust database management with container orchestration benefits automated scaling, rolling updates, persistent storage, and failover. Define your deployment in a YAML manifest, deploy with kubectl apply, and monitor with standard Kubernetes commands.

Updated on: 2026-03-14T22:53:23+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements