Kubegres is a Kubernetes operator allowing to deploy one or many clusters of PostgreSql instances and manage databases replication, failover and backup.
set -e
# This script assumes that the env-var $POSTGRES_MY_DB_PASSWORD contains the password of the custom user to create.
# You can add any env-var in your Kubegres resource config YAML.
dt=$(date '+%d/%m/%Y %H:%M:%S');
echo "$dt - Running init script the 1st time Primary PostgreSql container is created...";
customDatabaseName="my_app_db"
customUserName="my_username"
echo "$dt - Running: psql -v ON_ERROR_STOP=1 --username $POSTGRES_USER --dbname $POSTGRES_DB ...";
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE DATABASE $customDatabaseName;
CREATE USER $customUserName WITH PASSWORD '$POSTGRES_MY_DB_PASSWORD';
GRANT ALL PRIVILEGES ON DATABASE "$customDatabaseName" to $customUserName;
EOSQL
echo "$dt - Init script is completed";
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/kubernetes ClusterIP 10.2.0.1 443/TCP 54m
service/mypostgres ClusterIP None 5432/TCP 28m app=mypostgres,replicationRole=primary
service/mypostgres-replica ClusterIP None 5432/TCP 28m app=mypostgres,replicationRole=replica
NAME READY AGE CONTAINERS IMAGES
statefulset.apps/mypostgres-1 1/1 28m mypostgres-1 postgres:14.1
statefulset.apps/mypostgres-2 1/1 28m mypostgres-2 postgres:14.1
statefulset.apps/mypostgres-3 1/1 28m mypostgres-3 postgres:14.1
5. I run an ubuntu pod: ```k run ubuntu --image=ubuntu sleep 99999999; k exec -it ubuntu -- /bin/bash```
6. Install psql: ```apt-get install -y postgresql-client```
7. Try to jump in my created db: ```psql postgresql://my_username:54WvEAhp1VTI0MpzlUkvJI65SIjPfTV5Hqcqc7gY3z8ZD03tRqL10OwTctStNgx5TL8s1wyI02C9Q02ewZddVnUTV0ZRorqrK6@argo-postgres.argo.svc.cluster.local:5432/my_app_db```
8. Get error: ```psql: error: connection to server at "argo-postgres.argo.svc.cluster.local" (10.1.54.212), port 5432 failed: FATAL: password authentication failed for user "my_username"```
9. Using superuser works: ```psql postgresql://postgres:54WvEAhp1VTI0MpzlUkvJI65SIjPfTV5Hqcqc7gY3z8ZD03tRqL10OwTctStNgx5TL8s1wyI02C9Q02ewZddVnUTV0ZRorqrK6@mypostgres.default.svc.cluster.local:5432/postgres```
10. requesting all present tables:
Trying to override primary init does not work at all. It does result in a postgres db running, but not with the requested db and user in place.
procedure followed:
data:
primary_init_script.sh: |
!/bin/bash
apiVersion: kubegres.reactive-tech.io/v1 kind: Kubegres metadata: name: mypostgres namespace: default
spec:
replicas: 3 image: postgres:14.1 port: 5432
database: size: 200Mi
customConfig: mypostgres-conf
env:
name: POSTGRES_PASSWORD valueFrom: secretKeyRef: name: mysecretresource key: superUserPassword
name: POSTGRES_REPLICATION_PASSWORD valueFrom: secretKeyRef: name: mysecretresource key: replicationUserPassword
name: POSTGRES_MY_DB_PASSWORD valueFrom: secretKeyRef: name: mysecretresource key: myDbUserPassword
name: MY_OTHER_VAR value: "any_value"
EOF
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pod/mypostgres-1-0 1/1 Running 1 (28m ago) 28m 10.1.54.215 w3.k108
pod/mypostgres-2-0 1/1 Running 0 28m 10.1.177.152 w7.k108
pod/mypostgres-3-0 1/1 Running 0 28m 10.1.25.210 w1.k108
pod/ubuntu 1/1 Running 0 52m 10.1.54.214 w3.k108
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR service/kubernetes ClusterIP 10.2.0.1 443/TCP 54m
service/mypostgres ClusterIP None 5432/TCP 28m app=mypostgres,replicationRole=primary
service/mypostgres-replica ClusterIP None 5432/TCP 28m app=mypostgres,replicationRole=replica
NAME READY AGE CONTAINERS IMAGES statefulset.apps/mypostgres-1 1/1 28m mypostgres-1 postgres:14.1 statefulset.apps/mypostgres-2 1/1 28m mypostgres-2 postgres:14.1 statefulset.apps/mypostgres-3 1/1 28m mypostgres-3 postgres:14.1
\l Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+----------------------- postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres (3 rows)
\du Role name | Attributes | Member of -------------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} replication | Replication | {}