Step-by-Step: Setting Up a Postgres Database with Docker and Seeding Test Data

Senior Full-Stack Developer with over 20 years of experience building web applications for startups, government, and higher education.
Search for a command to run...

Senior Full-Stack Developer with over 20 years of experience building web applications for startups, government, and higher education.
No comments yet. Be the first to comment.
Writing this up so I have somewhere to quickly grab basic Jest setup. This information is copied from the Jest Documentation Site compressed into just the parts I'm interested in. Create folders mkdir -p <project>/src cd <project> Install and init n...

Continuing from last time, we will explore the concepts behind Create and Update functionality and provide a step-by-step guide on how to implement it in your SvelteKit application. Let's get started! Server & Client Code In SvelteKit, the server-sid...

Introduction In this post, I will explore how to create a REST API using Spring Boot and Kotlin. We will dive into the fundamentals of Spring Boot and Kotlin, explain how to set up the project, and guide you through the process of building the API st...

Introduction Welcome to the latest post in my series of Koan Battles using Kotlin and TypeScript. In this series, we'll complete a Koan in both languages and then discuss the differences between the two solutions. My goal is to help you gain a deeper...

In this blog post, we'll explore the process of setting up and configuring a Postgres database using Docker and seeding it with test data.
This approach can be used for a variety of apps and can be easily adapted for your own needs. We'll cover all the necessary steps, from the Docker configuration to creating and seeding the database.
By the end of this post, you'll have a Postgres database up and running, and be able to seed it with test data, allowing you to test your setup quickly and efficiently.
You can grab the source for this project from the Github Repository. Copy the docker-compose.yml file and postgres folder to your project. Then run docker-compose up --build -d. Viola, you have a fully seeded Postgres database running
We have a docker-compose.yml that setups up the database environment:
version: '3.8'
services:
db:
restart: always
build:
context: .
dockerfile: postgres/Dockerfile
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- '5432:5432'
networks:
- postgres-network
networks:
postgres-network:
driver: bridge
Notice the build step includes the postgres/Dockerfile which is used to build the Postgres database, create the test schema and seed the database:
FROM postgres:alpine
ADD /postgres/migrations/1_schema.sql /docker-entrypoint-initdb.d
ADD /postgres/migrations/2_data.sql /docker-entrypoint-initdb.d
RUN chmod a+r /docker-entrypoint-initdb.d/*
This is the database schema I use for my example apps, you can change the file /postgres/migrations/1_schema.sql to any schema you like.
CREATE TABLE top3
(
id SERIAL NOT NULL,
created_at DATE NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT top3_pkey PRIMARY KEY (id)
);
CREATE TABLE todo
(
id SERIAL NOT NULL,
title TEXT NOT NULL,
complete BOOLEAN NOT NULL DEFAULT false,
top3_id INTEGER NOT NULL,
CONSTRAINT todo_pkey PRIMARY KEY (id)
);
ALTER TABLE todo
ADD CONSTRAINT todo_top3_id_fkey
FOREIGN KEY (top3_id) REFERENCES top3 (id)
ON DELETE CASCADE ON UPDATE CASCADE;
This will create the Schema:

I then seed the data with test data, again this file /postgres/migrations/2_data.sql can be changed to any seed data you like:
INSERT INTO public.top3 (created_at) VALUES(CURRENT_TIMESTAMP);
INSERT INTO public.todo (id, title, complete, top3_id) VALUES(1, 'Research tech', false, 1);
INSERT INTO public.todo (id, title, complete, top3_id) VALUES(2, 'Write article', false, 1);
INSERT INTO public.todo (id, title, complete, top3_id) VALUES(3, 'Publish article', false, 1);
Now that everything is in place you can start the database with:
docker-compose up --build -d
Host = localhost
Port = 5432
Database Name = postgres
User = postgres
Password = postgres
In conclusion, setting up and configuring a Postgres database with Docker is a great way to get your project up and running quickly and easily. With the steps outlined in this post, you can quickly and efficiently set up a Postgres database with Docker, as well as seed it with test data. This approach can save you time and effort in the long run and can be easily adapted to your own needs.
I hope this post has been useful, you can grab the source from the Github Repository.