Create k8s pod with Postgres client, run a command, and delete pod

Elvis Ciotti
Aug 2, 2023

--

# create a pod with with "postgres" alpine image
kubectl -n <namespace> get pods/psql-temp-elvis || ( kubectl -n <namespace> run psql-temp-elvis - image=postgres:13-alpine - env="POSTGRES_USER=setme" - env="POSTGRES_PASSWORD=setme" - env="POSTGRES_DB=setme" - image=postgres:13-alpine - restart=Never; sleep 10; );
# launch psql client into it
kubectl -n <namespace> exec -it pod/psql-temp-elvis - sh -c "PGPASSWORD=<password> psql -U <postgres user> postgresql://<postgres url>:5432/<postgres db>";
# delete the pod unless cancelled
echo "Press any key to delete the pod"
read
kubectl -n <namespace> delete pods/psql-temp-elvis

--

--

Elvis Ciotti
Elvis Ciotti

Written by Elvis Ciotti

Software Contractor — Java, Spring, k8s, AWS, Javascript @ London - hire me at https://elvisciotti.github.io/

No responses yet