The complete flow of using Prisma+ Postgres in a Mono repo using Docker
This article will save your hours of researching here and there.
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:
Install Docker from official docker website
After installation open Docker desktop, make an account. Use
docker --version
to verify docker is installed.Setup a Mono repo:
npx create-turbo@latest
Make a DB folder inside the packages folder in your Mono repo
Now run
npm init -y
to generate package.json and runnpx 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)Now run
npm i prisma
to install prisma andnpx prisma generate
to generate schema.prisma.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
Now go the .env file and replace the url with:
DATABASE_URL="postgresql://postgres:mysecretpassword@loca.."
Now go to schema.prisma, write your modals (leaving this empty will be meaningless atleast create a table)
Now run
npx prisma migrate dev --init
to generate the migration folder and actual tables in your local Postgres databaseNow 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)- Now run:
docker exec -it postgres-container psql -U postgres -d your database
. You’ll go inside postgres cli.
- Now run:
Run \l to list all databases, \c to switch to the specific postgres, /dt to see all the tables.