- First, let’s set up a repo which will hold our test db:
pnpm initpnpm install prisma --save-devpnpm install typescript ts-node @types/node --save-devnpx tsc --initnpx prisma init --datasource-provider postgresql
- Add a dockerfile for the test db:
version: '3.8'
services: postgres: image: postgres:14 ports: - '54320:5432' environment: - POSTGRES_USER=postgres - POSTGRES_PASSWORD=postgres - POSTGRES_DB=dump-test
- Start the DB container:
docker-compose up -d postgres
- Copy the data over from the remote DB to your docker DB:
export PGPASSWORD=remote-db-pw
# Before | creates the backup# After | pipes the DB dump into the docker dbpg_dump "connection string for remote DB" | docker exec -i postgres-container-name /bin/bash -c "PGPASSWORD=postgres psql --username db-username docker-db-name"
Now you have your DB set up, check it out in your favorite DB application (pg admin, Postico, Prisma Studio etc)
- Given we want to replicate our Prisma setup, not just the DB itself, we need to copy over the Prisma schema and migrations:
- Paste your real prisma schema into the test db schema
- Paste the migrations folder too
- Run
prisma generate
Extras
If you want pg_dump and psql without the full postgres install you can do the following:
brew install libpq# For Mac M1echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrcsource ~/.zshrc