<p>Have a psql script run to load the database at startup:</p>
<pre><code>  dc-patients-db:
    image: postgres:16.2
    ports:
      - &quot;8005:5432&quot;
    volumes:
      - data-volume:/var/lib/postgresql/data
      - ./seed.sql:/tmp/seed.sql:rw
    command: sh -c &quot;psql -p 5432 postgres &lt; /tmp/seed.sql&quot;
    env_file:
      - ./variables.env
</code></pre>
<p>So it does not keep reloading the same data, you might want <code>seed.sql</code> to include steps to drop the existing database then re-create it.</p>

CC BY-SA 4.0
2024-04-09T22:20:33.500Z
I don't know what I'm missing, but I'm getting the following error: `~/code/dc/patients main* ❯ docker logs d97
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
        Is the server running locally and accepting connections on that socket?`

It is like it can't connect to psql inside the container!
J86
CC BY-SA 4.0
2024-04-10T01:42:02.290Z
@J86 maybe environment variables need to be set? I see the variables.env--does that include settings for `POSTGRES_USER` and `POSTGRES_PASSWORD`? You might need to log into the container and find out what exactly is happening. Also, see https://stackoverflow.com/questions/31645550/postgresql-why-psql-cant-connect-to-server
mikequentel
CC BY-SA 4.0
2024-04-08T13:58:41.217Z
2024-04-08T13:58:41.217Z
mikequentel
<p>I have the following <code>docker-compose.yml</code> file:</p>
<pre><code>version: &quot;3.7&quot;

services:
  dc-patients:
    build:
      context: .
      dockerfile: ./Dockerfile
      labels:
        service: &quot;patients&quot;
    image: hubaccount/patients:latest
    ports:
      - &quot;8081:8080&quot;
    env_file:
      - ./variables.env
    depends_on:
      - dc-patients-db
  dc-patients-db:
    image: postgres:16.2
    ports:
      - &quot;8005:5432&quot;
    volumes:
      - data-volume:/var/lib/postgresql/data
    env_file:
      - ./variables.env

volumes:
  data-volume:
</code></pre>
<p>When I run <code>docker compose up -d</code> this runs fine. My API in the <code>dc-patients</code> container runs fine and so does my database container.</p>
<p>The issue is, the database is empty and because I'm in the development phase, I want some test data in my <code>patients</code> table (e.g. 100 records). Right now I have to manually connect to the database and manually seed some test data in there.</p>
<p>Is it possible to have <code>docker compose up -d</code> run the container but also seed the <code>patients</code> table in the Postgres container with a 100 records (I can get a CSV file that has the test data).</p>
<p>And have <code>docker compose down</code> clear the data.</p>
<p>This way I can schedule a corn job on my VPS to run <code>docker compose down</code> followed by <code>docker compose up -d</code> every night so that my env always resets and I have data to work with?</p>
<p>I'm not really sure if such a thing is even doable, but I thought I'd ask!</p>

<docker-compose>
Is it possible to seed data as part of docker compose up -d
J86
# Is it possible to seed data as part of docker compose up -d

(Asked by: username_0 on 2024-04-07)

I have the following docker-compose.yml file:
version: "3.7"

services:
  dc-patients:
    build:
      context: .
      dockerfile: ./Dockerfile
      labels:
        service: "patients"
    image: hubaccount/patients:latest
    ports:
      - "8081:8080"
    env_file:
      - ./variables.env
    depends_on:
      - dc-patients-db
  dc-patients-db:
    image: postgres:16.2
    ports:
      - "8005:5432"
    volumes:
      - data-volume:/var/lib/postgresql/data
    env_file:
      - ./variables.env

volumes:
  data-volume:

When I run docker compose up -d this runs fine. My API in the dc-patients container runs fine and so does my database container.
The issue is, the database is empty and because I'm in the development phase, I want some test data in my patients table (e.g. 100 records). Right now I have to manually connect to the database and manually seed some test data in there.
Is it possible to have docker compose up -d run the container but also seed the patients table in the Postgres container with a 100 records (I can get a CSV file that has the test data).
And have docker compose down clear the data.
This way I can schedule a corn job on my VPS to run docker compose down followed by docker compose up -d every night so that my env always resets and I have data to work with?
I'm not really sure if such a thing is even doable, but I thought I'd ask!

**Tags:** docker-compose


---

## 1 answers

### Answer by username_1 on 2024-04-08. Score: 2 | Accepted answer

Have a psql script run to load the database at startup:
  dc-patients-db:
    image: postgres:16.2
    ports:
      - "8005:5432"
    volumes:
      - data-volume:/var/lib/postgresql/data
      - ./seed.sql:/tmp/seed.sql:rw
    command: sh -c "psql -p 5432 postgres < /tmp/seed.sql"
    env_file:
      - ./variables.env

So it does not keep reloading the same data, you might want seed.sql to include steps to drop the existing database then re-create it.

> **Comments:**
> **username_0**: I don't know what I'm missing, but I'm getting the following error: `~/code/dc/patients main* ❯ docker logs d97 psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory         Is the server running locally and accepting connections on that socket?`  It is like it can't connect to psql inside the container!  
> **username_1**: @username_0 maybe environment variables need to be set? I see the variables.env--does that include settings for `POSTGRES_USER` and `POSTGRES_PASSWORD`? You might need to log into the container and find out what exactly is happening. Also, see https://stackoverflow.com/questions/31645550/postgresql-why-psql-cant-connect-to-server  

---

