Docker Compose Seed Postgres

In this article, you will learn how to do Postgres seeding using Docker Compose.

Image for post

Video Link

Let’s Start Creating Docker Seeding

Hope you are familiar with “Docker-Compose”

Folder structure

Project
├── docker-compose.yml (File)
├── docker_postgres_init.sql (File)
Image for post
Image for post

Create a new file docker-compose.yml

Above file, we have created a Postgres Docker container with Port No: 5432

As well as set environment variables such as default username, password of Postgres container.

if you check the entire file we have also created volumes that point to the `docker_postgres_init.sql` file.

- ./docker_postgres_init.sql:/docker-entrypoint initdb.d/docker_postgres_init.sql

The script inside “docker_postgres_init.sql” will create a new table student and insert the records as well

When you run the following command in the root directory for the project.

docker-compose up

then you will see in the console docker_postgres_init.sql script will be executed as the following screenshot

Image for post

that means your script executed successfully.

How to test

For testing, we used Pgadmin4 configured already in the docker-compose file.

Image for post

Try to run the following URL in the browser

http://localhost:16543

and enter username and password as the following screenshot.

username: test@gmail.com
password: test123!
Image for post
Pgadmin4

after successful login, you will see the following screen.

Image for post

Then click on to server -> create -> server

You will see the following pop up then in the General tab type

name: root_user

Image for post

Then click on the Connection tab as the following screenshot then input Hostname

Image for post

You can get Hostname by typing the following command

$ ifconfig |grep inet
Image for post

then type

Database: root

Username: root

Password: root

Image for post

then click on to Save Button

then you will see the connection successful on the left side of the Pgadmin 4 navigation as shown below.

Image for post

then go to Databases->root->Schema->Tables->students you will see there are 3 records as shown below in the screenshot.

Image for post

Github

Thank you!

Written by

We teach & develop for startups

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store