The complete flow of using Prisma+ Postgres in a Mono repo using Docker

This article will save your hours of researching here and there.

The complete flow of using Prisma+ Postgres in a Mono repo using Docker

From the step 7: you can also use neondb(Postgres on cloud) from neon.tech if you don’t want to use docker.

Here are the steps you need to follow to get started:

  1. Install Docker from official docker website

  2. After installation open Docker desktop, make an account. Use docker --version to verify docker is installed.

  3. Setup a Mono repo: npx create-turbo@latest

  4. Make a DB folder inside the packages folder in your Mono repo

  5. Now run npm init -y to generate package.json and run npx tsc --init to generate tsconfig.json. Go to the tsconfig file and setup rootdir to ./src and outdir to ./dist/index.js (Make sure you are in the db directory in terminal before running the command of this step 5)

  6. Now run npm i prisma to install prisma and npx prisma generate to generate schema.prisma.

  7. Now open your terminal and write: docker run --name postgres-container -e POSTGRES_PASSWORD=password -e POSTGRES_USER=postgres -e POSTGRES_DB=yourdatabase -p 5432:5432 -d postgres

  8. Now go the .env file and replace the url with:

    DATABASE_URL="postgresql://postgres:mysecretpassword@loca.."

  9. Now go to schema.prisma, write your modals (leaving this empty will be meaningless atleast create a table)

  10. Now run npx prisma migrate dev --init to generate the migration folder and actual tables in your local Postgres database

  11. Now run docker ps. This will show you the container ids. Copy the container id of the postgres db (You’ll probably have only one container)

    1. Now run: docker exec -it postgres-container psql -U postgres -d your database . You’ll go inside postgres cli.
  12. Run \l to list all databases, \c to switch to the specific postgres, /dt to see all the tables.