title: Docker compose postgresql service - can't create user and database during build?
body: <p>I have wasted an entire day on this, and to say I'm not impressed by the unnecessary complexity of what should be a simple task - would be a gross understatement.</p>
  
  <p>Ok, having got that off my chest, I am building a django application using docker-machine, docker-compose, postgresql and redis - by following <a href="https://realpython.com/blog/python/django-development-with-docker-compose-and-machine/" rel="noreferrer">this tutorial</a>.</p>
  
  <p>I have managed to get the basic tutorial to work - but it does not suit my needs, as I need to create a user and a database for my application - as opposed to using 'postgres' for both.</p>
  
  <p>I have used the answer from @dnephin to a <a href="https://stackoverflow.com/questions/33309121/using-docker-compose-to-create-tables-in-postgresql-database">similar question</a>, and modified my code as follows:</p>
  
  <p>I created a new Dockerfile in a new directory <code>./database/</code>:</p>
  
  <pre><code>FROM postgres:9.6
  COPY . /fixtures
  WORKDIR /fixtures
  RUN /fixtures/setup.sh
  </code></pre>
  
  <p><code>./database/setup.sh</code> contents:</p>
  
  <pre><code>#!/bin/bash
  set -e
  
  pg_createcluster 9.6 main --start
  /etc/init.d/postgresql start
  su - postgres # makes no effing difference ...
  psql -f create_fixtures.sql    
  /etc/init.d/postgresql stop
  </code></pre>
  
  <p><code>./database/create_fixtures.sql</code> contents:</p>
  
  <pre><code>CREATE DATABASE mydatabase WITH ENCODING 'UTF8';
  CREATE USER webuser ENCRYPTED PASSWORD 'deadbeefsnaf0' NOSUPERUSER NOCREATEDB NOCREATEROLE;
  GRANT ALL PRIVILEGES ON mydatabase TO webuser;
  </code></pre>
  
  <p>and finally my postgres service in the docker_compose.yml is modified to use build:</p>
  
  <pre><code>postgres:
      build: ./database/
      ...
  </code></pre>
  
  <p>When I run <code>docker-compose build</code>, the build goes through the motions and then <em>barfs</em> at where I'm importing the SQL fixtures file via <strong>psql</strong>:</p>
  
  <pre><code>frothing@themouth:~/path/to/directory$ docker-compose build
  redis uses an image, skipping
  Building postgres
  Step 1/4 : FROM postgres:9.6
   ---&gt; ff0943ecbb3c
  Step 2/4 : COPY . /fixtures
   ---&gt; fae19dc88da8
  Removing intermediate container 84b860aee55c
  Step 3/4 : WORKDIR /fixtures
   ---&gt; aa88438dc69f
  Removing intermediate container b801ddc3b374
  Step 4/4 : RUN /fixtures/setup.sh
   ---&gt; Running in ca3e89ec2460
  Creating new cluster 9.6/main ...
    config /etc/postgresql/9.6/main
    data   /var/lib/postgresql/9.6/main
    locale en_US.utf8
    socket /var/run/postgresql
    port   5432
  Starting PostgreSQL 9.6 database server: main.
  psql: FATAL:  role "root" does not exist
  ERROR: Service 'postgres' failed to build: The command '/bin/sh -c /fixtures/setup.sh' returned a non-zero code: 2
  </code></pre>
  
  <p>I tried to solve this using the <a href="https://docs.docker.com/engine/examples/postgresql_service/" rel="noreferrer">useless documentation on docker for postgresql service</a> - but got no where. </p>
  
  <p><strong>How can I solve this?</strong></p>