<p>I've setup a PostgreSQL database with Helm Chart using this ConfigMap, that contains the initial script which allows to create the databases and schemas I need:</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Values.postgres.configmap }}
  namespace: {{ .Release.Namespace }}
data:
  init_script.sh: |+
    #!/bin/bash
    psql -v &lt;&lt;-EOSQL
    CREATE DATABASE my-db;
    GRANT ALL PRIVILEGES ON DATABASE my-db to &lt;user&gt;
    ...
</code></pre>
<p>I want to add an environmental variable for the user and one for the database name in order to:</p>
<ul>
<li>use them when I connect to <code>psql</code></li>
<li>use the user name to give the correct rights to access the Database created with the script.</li>
</ul>
<p>I don't know how to set them.</p>

<p>Update your script with the Postgress User and database name to pass to the psql script as environmental variables.</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Values.postgres.configmap.name }}
  namespace: {{ .Release.Namespace }}
data:
  init_script.sh: |+
    #!/bin/bash
    set -e
    psql -v ON_ERROR_STOP=1 -U $POSTGRES_USER --dbname $POSTGRES_DB &lt;&lt;-EOSQL
    CREATE DATABASE my-db;
    GRANT ALL PRIVILEGES ON DATABASE my-db to $POSTGRES_USER;
    ...
    EOSQL
</code></pre>
<p>Then, add into your Deloyment file the two enviroment variables in the <code>Env</code> section.</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
spec:
    template:
        spec:
            containers:
                - name: {{ .Values.postgres.image }}
                  env:
                  - name: POSTGRES_USER
                    value: {{ .Values.postgres.user }}
                  - name: POSTGRES_DB
                    value: {{ .Values.postgres.db }}
</code></pre>
