Prisma
This quickly shows how to connect your Prisma application to Supabase Postgres. If you encounter any problems, reference the Prisma troubleshooting docs.
If you plan to solely use Prisma instead of the Supabase Data API (PostgREST), turn it off in the API Settings.
Create a custom user for Prisma
- In the SQL Editor, create a Prisma db-user with full privileges on the public schema.
- This gives you better control over Prisma's access and makes it easier to monitor using Supabase tools like the Query Performance Dashboard and Log Explorer.
password manager
For security, consider using a password generator for the Prisma role.
_15-- Create custom user_15create user "prisma" with password 'custom_password' bypassrls createdb;_15_15-- extend prisma's privileges to postgres (necessary to view changes in Dashboard)_15grant "prisma" to "postgres";_15_15-- Grant it necessary permissions over the relevant schemas (public)_15grant usage on schema public to prisma;_15grant create on schema public to prisma;_15grant all on all tables in schema public to prisma;_15grant all on all routines in schema public to prisma;_15grant all on all sequences in schema public to prisma;_15alter default privileges for role postgres in schema public grant all on tables to prisma;_15alter default privileges for role postgres in schema public grant all on routines to prisma;_15alter default privileges for role postgres in schema public grant all on sequences to prisma;
_10-- alter prisma password if needed_10alter user "prisma" with password 'new_password';
Create a Prisma Project
Create a new prisma Project on your computer
Create a new directory
Initiate a new Prisma project
_10npm init -y_10npm install prisma typescript ts-node @types/node --save-dev_10_10npx tsc --init_10_10npx prisma init
Add your connection information to your .env file
- Visit the Database Settings
- Find your Supavisor Session Mode string. It should end with 5432. It will be used in your
.env
file.
If you're in an IPv6 environment or have the IPv4 Add-On, you can use the direct connection string instead of Supavisor in Session mode.
- If you plan on deploying Prisma to a serverless or auto-scaling environment, you'll also need your Supavisor transaction mode string.
- The string is identical to the session mode string but uses port 6543 at the end.
In your .env file, set the DATABASE_URL variable to your connection string
Change your string's [DB-USER]
to prisma
and add the password you created in step 1
_10postgres://prisma.[PROJECT-REF]...
Create your migrations
If you have already modified your Supabase database, synchronize it with your migration file. Otherwise create new tables for your database
Create new tables in your prisma.schema file
commit your migration
_10npx prisma migrate dev --name first_prisma_migration
Install the prisma client
Install the prisma client and generate its model
_10npm install @prisma/client_10npx prisma generate
Test your API
Create a index.ts file and run it to test your connection