How to create a role and a database with kubectl and the chart of postgresql?

4/29/2021

I have just installed the chart of postgresql and now I need a kubectl script to create the role and the database. In psql I use this script:

psql -U postgres -d postgres <<EOF
CREATE ROLE aaa WITH LOGIN PASSWORD 'bbb';
CREATE DATABASE ccc OWNER aaa;
EOF

But how can I do this with kubectl?

-- xRobot
kubectl
kubernetes

1 Answer

4/29/2021

You can use kubectl exec to run this command within the postgres container:

The flag -i opens a terminal in the container so you can see the command output.

kubectl exec -i postgres-pod -- psql -d postgres -U postgres  <<EOF
CREATE ROLE aaa WITH LOGIN PASSWORD 'bbb';
CREATE DATABASE ccc OWNER aaa;
EOF
-- Daniel Marques
Source: StackOverflow