PostgreSQL in Helm: initdbScripts Parameter

4/3/2019

The default Helm Chart for PostgreSQL (i.e. stable/postgresql) defines an initdbScripts parameter that allows initialization scripts to be run. However, I can't seem to get the format correct on how to issue it via the command line.

Could someone provide an example of how to populate this command line parameter?

Here's what I'm issuing, minus a working version of the initdbScripts parameter.

helm install stable/postgresql -n testpg \
    --set global.postgresql.postgresqlDatabase=testpg \
    --set global.postgresql.postgresqlUsername=testpg \
    --set global.postgresql.postgresqlPassword=testpg \
    --set global.postgresql.servicePort=5432 \
    --set initdbScripts=(WHAT GOES HERE TO RUN "sql/init.sql"??) \
    --set service.type=LoadBalancer
-- jeffmaher
kubernetes
kubernetes-helm
postgresql

1 Answer

4/3/2019

According to stable/postgresql helm chart, initdbScripts is a dictionary of init script names which are multi-line variables:

## initdb scripts
## Specify dictionary of scripts to be run at first boot
## Alternatively, you can put your scripts under the files/docker-entrypoint-initdb.d directory
##
# initdbScripts:
#   my_init_script.sh:|
#      #!/bin/sh
#      echo "Do something."

Let's assume that we have the following init.sql script:

CREATE USER helm;
CREATE DATABASE helm;
GRANT ALL PRIVILEGES ON DATABASE helm TO helm;

When we are going to inject a multi-line text into values we need to deal with indentation in YAML.

For above particular case it is:

helm install stable/postgresql -n testpg \
--set global.postgresql.postgresqlDatabase=testpg \
--set global.postgresql.postgresqlUsername=testpg \
--set global.postgresql.postgresqlPassword=testpg \
--set global.postgresql.servicePort=5432 \
--set initdbScripts."init\.sql"="CREATE USER helm;
CREATE DATABASE helm;
GRANT ALL PRIVILEGES ON DATABASE docker TO helm;" \
--set service.type=LoadBalancer

There is some explanation to above example:

  1. If script's name has . it should be escaped, like "init\.sql".
  2. Script's content is in double quotes, because it's multi-line string variable.
-- nickgryg
Source: StackOverflow