DEV Community

Madhav Jha
Madhav Jha

Posted on

Dockerized postgres for local development

This post describes a quick setup which allows one to develop a postgres sql backed application locally. (This is taken from this eggheadio video.)

Create a docker-compose.yml file as follows.

version: "3.8"
services:
  db:
    image: "postgres:12"
    ports:
      - "54320:5432"
    volumes:
      - ./pgdata:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=alice
      - POSTGRES_PASSWORD=wonderland
      - POSTGRES_DB=myawesomedb

Enter fullscreen mode Exit fullscreen mode

Create a local folder which will persist data and start the service.

mkdir pgdata
docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

Once it is up and running, verify you can log into postgres shell.

docker-compose run db bash
Enter fullscreen mode Exit fullscreen mode

Inside the docker container run the following and enter password wonderland defined above.

psql --host=db --username=alice --dbname=myawesomedb
Enter fullscreen mode Exit fullscreen mode

You should see psql shell. You can exit by pressing Ctrl+D twice.

At this point most likely you already have prisma setup locally by some variation of the following commands.

npx prisma init
npx prisma generate
Enter fullscreen mode Exit fullscreen mode

Now you can run the following command to create and apply migrations.

npx prisma migrate dev --name init
Enter fullscreen mode Exit fullscreen mode

Top comments (0)