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?
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 .
trykubectl exec -it <postgresspod-name> bash or sh
when you inside the container you can just doPGPASSWORD=<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>\`
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With