<p>I am working on adding an <code>init-container</code> in my app's <code>deployment.yaml</code> so I can decouple my postgres db bootstrap or schema evolution.</p>
<p>As V1 I am trying to mount a sql file that will include the bootstraping of the db ( Database, Schema , Users) from the local directory and then by using a Postgres client image to run the commands in the script.</p>
<p>As of now I have the below</p>
<pre><code>initContainers:
    - name: init-myservice
      image: jbergknoff/postgresql-client
      command: ['sh', '-c', 'psql -a -f /bootstrap.sql']
      env:
        - name: PGUSER
          ....
          ..... 
    volumeMounts:
      - mountPath: /bootstrap.sql
        name: bootstrap.sql
</code></pre>
<p>From my search results I am guessing I have to use an empty dir volume type to mount the file but I can not get it to work as of now.</p>
<p>I need to also pass the db credentials but not sure how to use them from env variables inside the sql script is there any syntax for that ?</p>
<p>Could you help connect the dots together ? Mainly how to mount the file in the init container and how to grab the env variables from the shell script and use them in the sql script to create for example the user with a password ?</p>
<p>Thanks in advance.</p>

kostas.kapasakis
<p>You will have to do the following:</p>
<ol>
<li><p>Make the content of the SQL file as a ConfigMap like this, for example:</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: mariadb-config
data:
  mariadb-schema: &quot;DROP DATABASE IF EXISTS test;\n\nCREATE DATABASE IF NOT EXISTS test;
</code></pre>
</li>
<li><p>Make a volume from this config map in your deployment yaml like this:</p>
<pre><code> volumes:
 - name: mariadb-schema-config-vol
   configMap:
     name: mariadb-config
     defaultMode: 420
     items:
     - key: mariadb-schema
       path: mariadb-schema.sql
</code></pre>
<p>And volume mount like this:</p>
<pre><code> volumeMounts:
 - mountPath: /var/db/config
   name: mariadb-schema-config-vol
</code></pre>
</li>
</ol>
<p>Then your init container command will be like:</p>
<pre><code>    ['sh', '-c', 'psql -a -f /var/db/config/mariadb-config.sql']
</code></pre>
<p>For your second question, make a shell script that reads the env variables (The db credentials - I am presuming that you are having them in secrets and using them as env variables) and then invoke this command:</p>
<pre><code>   psql -a -f /var/db/config/mariadb-config.sql
</code></pre>
<p>So to make this happen the content of this script should be in a config map and execute the script from a volume mount, just like the above example.</p>
<p>Hope this helps.</p>
