how to connect to psql using password on kubernetes

8/8/2019

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','admin@admin.com',NULL,true);'" 

but these command did not work.

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

-- strangedeveloper
kubernetes
postgresql
psql

3 Answers

8/14/2019

As you asking only for command I assume you have all Deployments, Services, etc.

Please execute command below:

$ kubectl exec -ti <postgresspod-name> -- psql -h <node/host IP> -U postgresadmin --password -p <port> postgresdb
Password for user postgresadmin:

After you will type password you will be connected to Postgres

-- PjoterS
Source: StackOverflow

8/8/2019

You can try this way :

kubectl exec -i <plsql podname> -- psql -h <host or service name or localhost> -u <username> -p<password>
-- Harsh Manvar
Source: StackOverflow

8/14/2019

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>\`

-- Maicon Santana
Source: StackOverflow