12

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

3 Answers 3

13

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 helm 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.
Sign up to request clarification or add additional context in comments.

1 Comment

extra thanks for mentioning escaping the "." in the key name, e.g. init\.sql
8

Here's what @nickgryg's answer looks like with values.yaml instead of command line switches.

primary:
  initdb:
    scripts:
      init.sql: |
        CREATE USER helm;
        CREATE DATABASE helm;
        GRANT ALL PRIVILEGES ON DATABASE helm TO helm;

2 Comments

Is it possible to use some sort of templating/replacements with that? Let's say, if using helm secrets and you want to set the password with the user?
@McFlurriez I had similer thoughts since I wanted to use Hashicorp Vault for secrets. I include the password during helm install: "helm install postgresql . --namespace foo --set global.postgresql.auth.password=kubectl exec --namespace foo vault-0 -- vault kv get -field=password secret/db-pass"
0

If you are using "hashicorp/helm" provider on terraform you can use something like this:

resource "helm_release" "postgres" {
  name      = "postgres"
  chart     = "bitnami/postgresql"
  version   = "13.2.0"
  set {
    name  = "primary.initdb.scripts"
    value = "init_dbs.sql: CREATE USER my_user; CREATE DATABASE my_db; GRANT ALL PRIVILEGES ON DATABASE my_db TO my_user;"
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.