title: How to make PostgresQL use environment variables from .env file after re-creating the container (without removing the volume)
body: <p>There is a message in <a href="https://hub.docker.com/_/postgres" rel="nofollow noreferrer">Docker PostgresQL docs</a>:</p>
  
  <blockquote>
    <p>Warning: the Docker specific variables will only have an effect if you
    start the container with a data directory that is empty; any
    pre-existing database will be left untouched on container startup.</p>
  </blockquote>
  
  <p>So when I recreate my postgres container with <code>docker-compose down</code> and <code>docker-compose up -d --remove-orphans</code>, it throws me errors like: </p>
  
  <blockquote>
    <p>FATAL:  password authentication failed for user "myuser"</p>
  </blockquote>
  
  <p>It happens because postgres sees that there is the volume, so it skips all scripts and .env files (where I set <code>POSTGRES_USER</code> and <code>POSTGRES_PASSWORD</code>).</p>
  
  <p><strong>How this can be solved? (I mean provide vars from .env file everytime I re-create postgres container)</strong> </p>
  
  <p>p.s. It is impossible for me to delete old volume as I have data there.</p>
  
  <p>Here is my <code>docker-compose.yml</code> file:</p>
  
  <pre><code>postgres:
      image: postgres:alpine
      restart: always
      expose:
        - 5432
      volumes:
        - "./init/postgres:/docker-entrypoint-initdb.d"
        - "./data/postgres_data:/var/lib/postgresql/data"
      environment:
        POSTGRES_DB:       ${POSTGRES_DB}
        POSTGRES_USER:     ${POSTGRES_USR}
        POSTGRES_PASSWORD: ${POSTGRES_PWD}
  </code></pre>
  
  <p>My .env file:</p>
  
  <pre><code># Postgres
  export POSTGRES_USR="someuser"
  export POSTGRES_PWD="somepwd"
  export POSTGRES_DB="somedb"
  export POSTGRES_URL="postgres://${POSTGRES_USR}:${POSTGRES_PWD}@postgres:5432/${POSTGRES_DB}?sslmode=disable"
  </code></pre>