Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to connect to psql using password on kubernetes

I want to connect to psql using password on kubectl exec command on kubernetes like

kubectl exec -it postgres -- bash \`psql -h $IP -U admin --password password -p $PORT dbname\`

I tried to command

kubectl exec -it $podid -- bash \`psql -h $IP -U admin --password -p $PORT postgresdb\`

and

kubectl exec -it $podid -- bash -c "psql -- dbname=postgresql://postgres:password@$ip:$port/postgresdb -c 'INSERT INTO public."user" (user_id,user_pw,user_nm,email,phone,is_admin) VALUES ('admin','admin','admin','[email protected]',NULL,true);'" 

but these command did not work.

How can I connect to psql using kubernetes commands and password?

like image 519
strangedeveloper Avatar asked Aug 08 '19 05:08

strangedeveloper


People also ask

How do I log into my PostgreSQL pod?

Accessing a Pod with Kubectl Use the kubectl tool to run utilities directly in a Postgres pod. This psql command connects to the default Postgres database, postgres .


2 Answers

try
kubectl exec -it <postgresspod-name> bash or sh
when you inside the container you can just do
PGPASSWORD=<password> psql -h <host or service name or localhost> -U <username> <dbname>

another option is kubectl exec -it postgres -- bash \`PGPASSWORD=<password> psql -h <host or service name or localhost> -U <username> <dbname>\`

like image 191
Maicon Santana Avatar answered Oct 07 '22 13:10

Maicon Santana


The accepted answer did not work for me (command PGPASSWORD=<...> not found).

This worked :

kubectl  exec -ti postgres -- env PGPASSWORD=$PASSWD psql psql -h <host> -U <username> <dbname>
like image 3
moogly81 Avatar answered Oct 07 '22 11:10

moogly81