Skip to content

Instantly share code, notes, and snippets.

@katzefudder
Last active April 2, 2026 11:13
Show Gist options
  • Select an option

  • Save katzefudder/5628db9b5f11cceda4d2549ad8f887e8 to your computer and use it in GitHub Desktop.

Select an option

Save katzefudder/5628db9b5f11cceda4d2549ad8f887e8 to your computer and use it in GitHub Desktop.
How to access a postgres database through a pod on Kubernetes

How to access a Postgres availale through a Kubernetes cluster from your local machine

Create a socat pod on your default namespace (or whatever namespace you see fit) This pod will then create a bytestream to the postgres' instance and port -> set postgresql-host accordingly

apiVersion: v1
kind: Pod
metadata:
  name: postgresql-socat
  labels:
    name: postgresql-socat
spec:
  containers:
  - name: postgresql-socat
    image: alpine/socat
    command: ["socat", "-dd", "tcp4-listen:5432,fork,reuseaddr", "tcp4:postgresql-host:5432"]
    resources:
      limits:
        memory: "64Mi"
        cpu: "50m"
    ports:
      - containerPort: 5432



Then forward the pod's container... 

`kubectl port-forward postgresql-socat 5432:5432`

... and open up a connection to your upstream's postgres connection

`psql -h localhost -p 5432 ...`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment