Read more

HowTo: Get postgres shell in kubernetes

Florian Heinle
August 25, 2022Software engineer at makandra GmbH

If your postgres database is only accessible from inside a kubernetes cluster, e.g. if it's configured in AWS RDS and not available to the public (as it should be!), here's how to open a psql shell inside Kubernetes and connect to the database. Make sure to replace the variables appropriately.

$ kubectl run postgresql-client \
  --image=postgres      \
  --namespace=$NAMESPACE \
  --stdin=true --tty=true \
  --rm=true                \
  --env="PGPASSWORD=$PASSWORD_FOR_POSTGRES \
  --command -- \
  psql --host=$HOSTNAME_FOR_POSTGRES --username=$USERNAME_FOR_POSTGRES $DATABASE_FOR_POSTGRES
Illustration money motivation

Opscomplete powered by makandra brand

Save money by migrating from AWS to our fully managed hosting in Germany.

  • Trusted by over 100 customers
  • Ready to use with Ruby, Node.js, PHP
  • Proactive management by operations experts
Read more Show archive.org snapshot

You need to specify the password as an env variable for the container since you can't answer the password prompt in the container.

If you don't immediately see a postgres shell, wait a few seconds and press enter.

The pod will automatically be deleted when the shell closes.

Posted by Florian Heinle to makandra Operations (2022-08-25 13:54)