<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>If you are using the standard <a href="https://hub.docker.com/_/postgres" rel="nofollow noreferrer">Docker Hub <code>postgres</code> image</a> and you are just trying to create a database, the easiest thing to do is to use its environment-variable settings</p>
<pre class="lang-yaml prettyprint-override"><code>image: postgres:15
env:
  - name: POSTGRES_USER
    value: &lt;user&gt;
  - name: POSTGRES_DB
    value: my-db
</code></pre>
<p>For these settings you do not need a separate ConfigMap.  In the context of a Helm chart, if you want to make these values configurable, you can</p>
<pre class="lang-yaml prettyprint-override"><code>value: {{ .Values.postgres.db | default &quot;my-db&quot; }}
</code></pre>
<p>which will use a value from the configuration (<code>values.yaml</code>, <code>helm install --set</code> option, additional <code>helm install -f</code> files)</p>
<pre class="lang-yaml prettyprint-override"><code>postgres:
  db: database-name
</code></pre>
<p>or <code>my-db</code> if it's not set.</p>
<p>If you do specifically want to use an init script, but won't know the database user name until deploy time, you can ask Helm to inject this into the init script.  If you name the script <code>*.sql</code> then the image will run it under <code>psql</code> for you so you don't need the credentials.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Values.postgres.configmap }}
data:
  init_script.sql: |+
    CREATE DATABASE my-db;
    GRANT ALL PRIVILEGES ON DATABASE my-db to {{ .Values.postgres.user }}
    ...
</code></pre>
<p>Helm will substitute the templated value before creating the ConfigMap.</p>
<p>In all of these cases, note that the initialization scripts and environment variables are only considered the very first time the database is used, if the corresponding storage is empty.  If you change one of the Helm values, it will change the environment-variable or ConfigMap setting, but that won't actually cause a change in the database.</p>
<p>Practically, my experience has been that the best approach here is to use the environment variables to create a database and user, and then to use your application framework's database-migration system to actually create the tables.  You'll need the migrations in other contexts so it's good to have a path to run them, and they're useful if the database schema ever changes; you can't just re-run the <code>/docker-entrypoint-initdb.d</code> scripts.</p>
