title: Create multiple databases at PostgreSQL container startup
body: <p>I would like to create multiple databases at container startup. I use <code>postgres:11.2</code> as base image.</p>
  
  <p>After reading the doc here <a href="https://hub.docker.com/_/postgres" rel="nofollow noreferrer">https://hub.docker.com/_/postgres</a> and this post <a href="https://stackoverflow.com/questions/26598738/how-to-create-user-database-in-script-for-docker-postgres">How to create User/Database in script for Docker Postgres</a>, I am still unable to create multiple databases when the container starts.</p>
  
  <p>I tried these solutions:</p>
  
  <p>1st with <code>init.sh</code>:</p>
  
  <pre><code>#!/bin/bash
  set -e
  
  psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" &lt;&lt;-EOSQL
      CREATE USER owner;
  
      CREATE DATABASE productservice;
      GRANT ALL PRIVILEGES ON DATABASE productservice TO owner;
  
      CREATE DATABASE usersservice;
      GRANT ALL PRIVILEGES ON DATABASE usersservice TO owner;
  
      CREATE DATABASE financeservice;
      GRANT ALL PRIVILEGES ON DATABASE financeservice TO owner;
  
      CREATE DATABASE relationshipservice;
      GRANT ALL PRIVILEGES ON DATABASE relationshipservice TO owner;
  
      CREATE DATABASE securityservice;
      GRANT ALL PRIVILEGES ON DATABASE securityservice TO owner;
  EOSQL
  </code></pre>
  
  <p>And in the <code>Dockerfile</code></p>
  
  <pre><code>ENV INIT_DIR /docker-entrypoint-initdb.d
  RUN mkdir -p ${INIT_DIR}
  COPY init.sh ${INIT_DIR}
  </code></pre>
  
  <p>I don't get any errors but neither the databases nor the user are created, only the default <code>postgres</code> database is there.</p>
  
  <p>2nd with <code>init.sql</code></p>
  
  <pre><code>CREATE USER owner;
  
  CREATE DATABASE productservice;
  GRANT ALL PRIVILEGES ON DATABASE productservice TO owner;
  
  CREATE DATABASE chicoweursservice;
  GRANT ALL PRIVILEGES ON DATABASE usersservice TO owner;
  
  CREATE DATABASE financeservice;
  GRANT ALL PRIVILEGES ON DATABASE financeservice TO owner;
  
  CREATE DATABASE relationshipservice;
  GRANT ALL PRIVILEGES ON DATABASE relationshipservice TO owner;
  
  CREATE DATABASE securityservice;
  GRANT ALL PRIVILEGES ON DATABASE securityservice TO owner;
  </code></pre>
  
  <p>And changed the <code>Dockerfile</code> to</p>
  
  <pre><code>ENV INIT_DIR /docker-entrypoint-initdb.d
  RUN mkdir -p ${INIT_DIR}
  COPY init.sql ${INIT_DIR}
  </code></pre>
  
  <p>The result is the same as the first case.</p>
  
  <p>Thanks for your help.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>The complete <code>Dockerfile</code></p>
  
  <pre><code>FROM postgres:11.2
  ENV PGDATA /var/lib/postgresql/data/pgdata
  ENV INIT_DIR /docker-entrypoint-initdb.d
  RUN mkdir -p ${INIT_DIR}
  COPY init.sql ${INIT_DIR}
  RUN localedef -i fr_FR -c -f UTF-8 -A /usr/share/locale/locale.alias fr_FR.UTF-8
  ENV LANG en_US.UTF-8
  VOLUME ${PGDATA}
  </code></pre>