Seeding Postgres with Docker

Seeding postgres container during local development can be bit cumbersome, at least during the initial setup. Usually, we connect to the container and execute the seed script.

But now, let’s see how we can automate that by using docker-entrypoint-init…


This content originally appeared on DEV Community and was authored by Karan Pratap Singh

Seeding postgres container during local development can be bit cumbersome, at least during the initial setup. Usually, we connect to the container and execute the seed script.

But now, let's see how we can automate that by using docker-entrypoint-initdb.d, which runs all the *.sh or *.sql scripts on initialization.

Setup

Let's assume we have this folder structure:

├── Dockerfile
├── docker-compose.yml
└── scripts
    └── db
        ├── dump.sql
        └── init.sh

Here, dump.sql can be sql script that we want to seed the db with

-- CreateTable
CREATE TABLE IF NOT EXISTS users (
  "id" TEXT NOT NULL,
  "name" TEXT NOT NULL,
  "email" TEXT NOT NULL,

  PRIMARY KEY ("id")
);

-- Seed
INSERT INTO users (id, name, email) VALUES ('userid', 'Gopher', 'hello@gopher.com');

In the init.sh, we will just execute our *.sql script.

#!/bin/bash

psql -U $POSTGRES_USER -d $POSTGRES_DB -a -f /app/scripts/db/dump.sql

In the Dockerfile, we will copy our init.sh to docker-entrypoint-initdb.d directory.

FROM postgres:12 as db
WORKDIR /app
COPY ./scripts/db/init.sh /docker-entrypoint-initdb.d
COPY ./scripts/db/dump.sql ./scripts/db/dump.sql

We will define our docker-compose.yml like below:

version: "3.8"

services:
  db:
    image: db
    container_name: db
    build:
      context: .
      target: db
    environment:
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=database
    ports:
      - 5432:5432

Let's start our containers!

$ docker compose up

Output

output

Conclusion

I hope this was helpful, feel free to reach out to me on twitter if you face any issues. Thanks for reading!


This content originally appeared on DEV Community and was authored by Karan Pratap Singh


Print Share Comment Cite Upload Translate Updates
APA

Karan Pratap Singh | Sciencx (2021-11-12T19:50:19+00:00) Seeding Postgres with Docker. Retrieved from https://www.scien.cx/2021/11/12/seeding-postgres-with-docker/

MLA
" » Seeding Postgres with Docker." Karan Pratap Singh | Sciencx - Friday November 12, 2021, https://www.scien.cx/2021/11/12/seeding-postgres-with-docker/
HARVARD
Karan Pratap Singh | Sciencx Friday November 12, 2021 » Seeding Postgres with Docker., viewed ,<https://www.scien.cx/2021/11/12/seeding-postgres-with-docker/>
VANCOUVER
Karan Pratap Singh | Sciencx - » Seeding Postgres with Docker. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/12/seeding-postgres-with-docker/
CHICAGO
" » Seeding Postgres with Docker." Karan Pratap Singh | Sciencx - Accessed . https://www.scien.cx/2021/11/12/seeding-postgres-with-docker/
IEEE
" » Seeding Postgres with Docker." Karan Pratap Singh | Sciencx [Online]. Available: https://www.scien.cx/2021/11/12/seeding-postgres-with-docker/. [Accessed: ]
rf:citation
» Seeding Postgres with Docker | Karan Pratap Singh | Sciencx | https://www.scien.cx/2021/11/12/seeding-postgres-with-docker/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.