This example shows you how to connect to a PostgreSQL database on Supabase using Prisma, and use Prisma Client in a TypeScript script to read and write data.
Download this example:
curl https://codeload.github.com/prisma/prisma-examples/tar.gz/latest | tar -xz --strip=2 prisma-examples-latest/databases/postgresql-supabase
Install npm dependencies:
cd postgresql-supabase
npm install
Alternative: Clone the entire repo
Clone this repository:
git clone git@github.com:prisma/prisma-examples.git --depth=1
Install npm dependencies:
cd prisma-examples/databases/postgresql-supabase
npm install
Create a .env
file at the root of your folder. Copy and update the following environment variables in the .env
file:
touch .env
If you have the Supabase CLI locally installed and have logged in, run the following command to start up Supabase
npx supabase start
# .env
DATABASE_URL="postgresql://postgres:postgres@localhost:54322/postgres"
If you're using the hosted version, create another database that will serve as the shadow database.
postgres=> CREATE DATABASE postgres_shadow;
postgres=> exit
Next, update, your .env
file with your DATABASE_URL
and SHADOW_DATABASE_URL
variables accordingly. Be sure to update the password ([YOUR-PASSWORD]
) and project reference ([YOUR-PROJECT-REF]
):
# .env
DATABASE_URL="postgres://postgres:[YOUR-PASSWORD]@db.[YOUR-PROJECT-REF].supabase.co:5432/postgres"
SHADOW_DATABASE_URL="postgres://postgres:[YOUR-PASSWORD]@db.[YOUR-PROJECT-REF].supabase.co:5432/postgres_shadow"
Run the following command to create a migration file with the SQL necessary to create the database schema:
npx prisma migrate dev --name init
You should see the following output:
Your database is now in sync with your schema.
For the script to work, you first need to execute the seed script to seed your database. You can do that using the following command:
npx prisma db seed
Then run the script script.ts
, using the following command:
npm run dev
As a next step, explore the script.ts
file to see how to use Prisma Client to read and write data in the database.
- Check out the Prisma docs
- Join our community on Discord to share feedback and interact with other users.
- Subscribe to our YouTube channel for live demos and video tutorials.
- Follow us on X for the latest updates.
- Report issues or ask questions on GitHub.