How to use a PostgreSQL container with existing data?
<p>I am trying to set up a PostgreSQL container (<a href="https://hub.docker.com/_/postgres/" rel="noreferrer">https://hub.docker.com/_/postgres/</a>). I have some data from a current PostgreSQL instance. I copied it from <code>/var/lib/postgresql/data</code> and want to set it as a volume to a PostgreSQL container.</p>

<p>My part from docker-compose.yml file about PostgreSQL:</p>

<pre><code>db:
    image: postgres:9.4
    ports:
        - 5432:5432
    environment:
        POSTGRES_PASSWORD: postgres
        POSTGRES_USER: postgres
        PGDATA : /var/lib/postgresql/data
    volumes:
        - /projects/own/docker_php/pgdata:/var/lib/postgresql/data
</code></pre>

<p>When I make  docker-compose up I get this message:</p>

<pre><code>db_1  | initdb: directory "/var/lib/postgresql/data" exists but is not empty
db_1  | If you want to create a new database system, either remove or empty
db_1  | the directory "/var/lib/postgresql/data" or run initdb
db_1  | with an argument other than "/var/lib/postgresql/data".
</code></pre>

<p>I tried to create my own image from the container, so my Dockerfile is:</p>

<pre><code>FROM postgres:9.4
COPY pgdata /var/lib/postgresql/data
</code></pre>

<p>But I got the same error, what am I doing wrong? </p>

<h2>Update</h2>

<p>I got SQL using pg_dumpall and put it in /docker-entrypoint-initdb.d, but this file executes every time I do <code>docker-compose up</code>.</p>