Connect psql to Kubernetes Postgres

Here are commands to connect local psql to Postgres in Kubernetes.

Find Postgres Pods

First, find all your kubernetes pods:

kubectl get pods

Which will output something like:

NAME                                                READY   STATUS      RESTARTS   AGE
noice-postgresql-0                                  1/1     Running     0          19h

Then determine which one is the Postgres pod. Describing it:

kubectl describe pod noice-postgresql-0

Will look something like:

Name:         noice-postgresql-0
Namespace:    default
    Container ID:   docker://6546189f1758f2337f24ddb2becc263bc8ddcf77bb7bbab8f0329177ecdc1abc
    Image ID:       docker-pullable://bitnami/postgresql@sha256:78bc8de2b0487be3d343a03d37352002fd87aec33b8be5d317841b77a7bf0abc
    Port:           5432/TCP

Port Forward Kubernetes Postgres

Now you need to expose the Postgres pod to outside of the k8s cluster:

kubectl port-forward pod/noice-postgresql-0 5432:5432

Connect psql

Now that it's port forwarded, you can connect your psql tool to it on localhost:

psql --host localhost --username postgres

This should get you a Postgres prompt on which to run sql commands.