--- id: 'prisma' title: 'Prisma' description: 'Prisma Quickstart' breadcrumb: 'ORM Quickstarts' hideToc: true --- This quickly shows how to connect your Prisma application to Supabase Postgres. If you encounter any problems, reference the [Prisma troubleshooting docs](/docs/guides/database/prisma-error-management). If you plan to solely use Prisma instead of the Supabase Postgres API, turn it off in the [API Settings](https://supabase.com/dashboard/project/_/settings/api). - In the [SQL Editor](https://supabase.com/dashboard/project/_/sql/new), 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](https://supabase.com/dashboard/project/_/database/query-performance) and [Log Explorer](https://supabase.com/dashboard/project/_/logs/explorer). For security, consider using a [password generator](https://bitwarden.com/password-generator/) for the Prisma role ```sql -- Create custom user create user "prisma" with password 'custom_password' bypassrls createdb; -- extend prisma's priveleges to postgres (necessary to view changes in Dashboard) grant "prisma" to "postgres"; -- Grant it necessary permissions over the relevant schemas (public) grant usage on schema public to prisma; grant create on schema public to prisma; grant all on all tables in schema public to prisma; grant all on all routines in schema public to prisma; grant all on all sequences in schema public to prisma; alter default privileges for role postgres in schema public grant all on tables to prisma; alter default privileges for role postgres in schema public grant all on routines to prisma; alter default privileges for role postgres in schema public grant all on sequences to prisma; ``` Create a new prisma Project on your computer Create a new directory ```bash Terminal mkdir hello-prisma cd hello-prisma ``` Initiate a new Prisma project ```bash npm init -y npm install prisma typescript ts-node @types/node --save-dev npx tsc --init npx prisma init ``` - Visit the [Database Settings](https://supabase.com/dashboard/project/_/settings/database) ![Session Mode](/docs/img/database/session_mode.png) - Find your Supavisor Session Mode string. It should end with 5432. If you're in an [IPv6 environment](https://github.com/orgs/supabase/discussions/27034) 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. ![Session Mode](/docs/img/guides/database/transaction-mode.png) - The string is identical to the session mode string but uses port 6543 at the end. ```text .env # Used for Prisma Migrations and within your application DIRECT_URL="postgres://[DB-USER].qjqzlvutjxbyodxrrvhb:[YOUR-PASSWORD]@aws-0-us-east-1.pooler.supabase.com:5432/postgres" ``` Change your string's `[DB-USER]` to `prisma` ```md postgres://prisma.qjqzlvutjxbyodxrrvhb... ``` Add a DATABASE_URL variable to your .env file. This is for your Supavisor Transaction mode string, which is necessary for managing serverless environments. It uses port 6543 and requires a flag appended to the the end of the string: "pgbouncer=true" ```text .env # Used in your application (use transaction mode) DATABASE_URL="postgres://[DB-USER].qjqzlvutjxbyodxrrvhb:[YOUR-PASSWORD]@aws-0-us-east-1.pooler.supabase.com:6543/postgres?pgbouncer=true" # Used for Prisma Migrations (use session mode or direct connection) DIRECT_URL="postgres://[DB-USER].qjqzlvutjxbyodxrrvhb:[YOUR-PASSWORD]@aws-0-us-east-1.pooler.supabase.com:5432/postgres" ``` Change both your strings' `[DB-USER]` to `prisma` ```md postgres://prisma.qjqzlvutjxbyodxrrvhb... ``` In your schema.prisma file, edit your datasource db configs to reference your DATABASE_URL ```text schema.prisma datasource db { provider = "postgresql" url = env("DATABASE_URL") directUrl = env("DIRECT_URL") } ``` 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 ```ts prisma/schema.prisma model Post { id Int @id @default(autoincrement()) title String content String? published Boolean @default(false) author User? @relation(fields: [authorId], references: [id]) authorId Int? } model User { id Int @id @default(autoincrement()) email String @unique name String? posts Post[] } ``` commit your migration ```bash npx prisma migrate dev --name first_prisma_migration ``` Synchronize your changes from your project: ```bash npx prisma db pull ``` Create a migraton file ```bash mkdir -p prisma/migrations/0_init_supabase ``` Synchronize the migrations ```bash npx prisma migrate diff \ --from-empty \ --to-schema-datamodel prisma/schema.prisma \ --script > prisma/migrations/0_init_supabase/migration.sql ``` If there are any conflicts, reference [Prisma's official doc](https://www.prisma.io/docs/orm/prisma-migrate/getting-started#work-around-features-not-supported-by-prisma-schema-language) or the [trouble shooting guide](/docs/guides/database/prisma-error-management) for more details ```bash npx prisma migrate resolve --applied 0_init_supabase ``` install the prisma client and generate its model ```sh npm install @prisma/client npx prisma generate ``` Create a index.ts file and run it to test your connection ```ts index.ts const { PrismaClient } = require('@prisma/client'); const prisma = new PrismaClient(); async function main() { //change to reference a table in your schema const val = await prisma..findMany({ take: 10, }); console.log(val); } main() .then(async () => { await prisma.$disconnect(); }) .catch(async (e) => { console.error(e); await prisma.$disconnect(); process.exit(1); }); ```