How to init several PostgreSQL DBs on bare metal Kubernetes via Helm

7/14/2020

I'm trying to create several databases after installation of Helm chart bitnami/postgresql. I added setting initdbScripts into helm install command:

helm install postgresql bitnami/postgresql --namespace=namespace_name \
    --set global.postgresql.postgresqlUsername=username,global.postgresql.postgresqlPassword=password123 \
    --set initdbScripts."init\.sql"="CREATE DATABASE db1;
GRANT ALL PRIVILEGES ON DATABASE db1 TO username;
CREATE DATABASE db2;
GRANT ALL PRIVILEGES ON DATABASE db2 TO username;
CREATE DATABASE db3;
GRANT ALL PRIVILEGES ON DATABASE db3 TO username;"

The pod starts failing with CrashLoopBackOff, and there's a line in the log: touch: cannot touch '/bitnami/postgresql/.user_scripts_initialized': Permission denied

My assumption why it does not work is that I have installed the chart to a bare-metal cluster and since it has no dynamic provisioning set up, I created a volume first:

kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgresql-data
  labels:
    type: local
    app: postgresql
  annotations:
    volume.alpha.kubernetes.io/storage-class: postgresql-data
spec:
  storageClassName: postgresql-data
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Recycle
  hostPath:
    path: "/opt/postgresql-data"

The volume is successfully claimed by PVC which I don't create manually.

Is there a way to initialize several databases using initdbScripts on a BM Kubernetes cluster? Any bare SQL or combined with Bash script solution would work.

Version of Helm and Kubernetes: helm version:

version.BuildInfo{Version:"v3.0.2", GitCommit:"19e47ee3283ae98139d98460de796c1be1e3975f", GitTreeState:"clean", GoVersion:"go1.13.5"}

kubectl version:

Client Version: version.Info{Major:"1", Minor:"16+", GitVersion:"v1.16.6-beta.0", GitCommit:"e7f962ba86f4ce7033828210ca3556393c377bcc", GitTreeState:"clean", BuildDate:"2020-01-15T08:26:26Z", GoVersion:"go1.13.5", Compiler:"gc", Platform:"windows/amd64"}
Server Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.5", GitCommit:"e6503f8d8f769ace2f338794c914a96fc335df0f", GitTreeState:"clean", BuildDate:"2020-06-26T03:39:24Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}

My solution is based on the best response in this thread: https://stackoverflow.com/questions/55499984/postgresql-in-helm-initdbscripts-parameter

-- Alex A
bash
kubernetes
kubernetes-helm
postgresql

0 Answers