Run psql on Kubernetes Postgres

Here's how to connect to a postgres instance on kubernetes with psql cli.

psql is a cli tool that allows you to query your postgres database.

If you put a postgres image on a kubernetes pod and deploy it as a service, you can access it with psql later with this command:

kubectl exec -it  -- psql --username 

kubectl is a tool to manage the k8s cluster.

exec allows you to execute commands on that cluster.

-i says the command will come from stdin -- what you type in the terminal.

-t says that the output of the command will pipe back to the terminal output.

Enter your own pod name -- the one that contains postgres. See pods with kubectl get pods.

-- is the delimiter for your own command to execute. Whatever comes after this delimiter is what gets executed on the pod.

psql is the cli for querying postgres. It should be installed on the image with postgres already. The flags that come after that are specific to postgres. Depending on your setup you may need more or less than this.