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

In this blog post, we'll explore the process of setting up and configuring a [Postgres](https://www.postgresql.org/) database using [Docker](https://www.docker.com/) 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](https://www.docker.com/) 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.

## TL;DR

You can grab the source for this project from the [Github Repository](https://github.com/justinramel/postgres-docker). 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

## Docker Setup

We have a `docker-compose.yml` that setups up the database environment:

```typescript
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:

```typescript
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/*
```

## Database Schema

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.

```sql
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:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1678132027708/3daf6b7d-51ab-4ab1-bf6b-1ec7095dd2c5.png align="center")

## Seeding the Database

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:

```sql
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);
```

## Commands

Now that everything is in place you can start the database with:

```bash
docker-compose up --build -d
```

## Configuration

* Host = localhost
    
* Port = 5432
    
* Database Name = postgres
    
* User = postgres
    
* Password = postgres
    

## Conclusion

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](https://github.com/justinramel/postgres-docker).
