Search code examples
postgresqlkubernetespsql

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

but these command did not work.

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


Solution

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