Skip to content

Commit f489364

Browse files
Update docs to perform migrations with connection pooling and Prisma config (#7266)
1 parent 5c857d7 commit f489364

File tree

8 files changed

+126
-78
lines changed

8 files changed

+126
-78
lines changed

content/200-orm/050-overview/500-databases/880-supabase.mdx

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Many aspects of using Prisma ORM with Supabase are just like using Prisma ORM wi
2626

2727
## Specific considerations
2828

29-
If you'd like to use the [connection pooling feature](https://supabase.com/docs/guides/database/connecting-to-postgres#connection-pooling-in-depth) available with Supabase, you will need to use the connection pooling connection string available via your [Supabase database settings](https://supabase.com/dashboard/project/_/settings/database) with `?pgbouncer=true` appended to the end of your `DATABASE_URL` environment variable:
29+
If you'd like to use the [connection pooling feature](https://supabase.com/docs/guides/database/connecting-to-postgres#connection-pooling-in-depth) available with Supabase, you will need to use the connection pooling connection string available via your [Supabase database settings](https://supabase.com/dashboard/project/_/settings/database) with `?pgbouncer=true` appended to the end of the environment variable that Prisma Client reads when you instantiate it with a driver adapter:
3030

3131
```env file=.env
3232
# Connect to Supabase via connection pooling with Supavisor.
@@ -41,33 +41,41 @@ Supabase provides three types of connection strings for each database:
4141

4242
3. **Session Pooler Connection string**`postgresql://postgres.[your-project-ref]:password@aws-0-[region].pooler.supabase.com:5432/postgres`
4343

44-
If you would like to use the Prisma CLI in order to perform other actions on your database (e.g. migrations), your setup depends on the connection string used:
44+
Prisma CLI commands (for example, migrations and introspection) now read the direct, non-pooled connection string from `prisma.config.ts`. Configure two environment variables — the pooled connection string for Prisma Client (`DATABASE_URL`) and a direct connection string for the Prisma CLI (`DIRECT_URL`):
4545

46-
- If your `DATABASE_URL` uses the Transaction Pooler (connection string 2), you must provide a `DIRECT_URL` with either the Direct Database (1) or Session Pooler (3) connection string in order for Prisma Migrate to work.
47-
48-
- If your `DATABASE_URL` already uses the Session Pooler (connection string 3), you do not need to provide a `DIRECT_URL`. Prisma Migrate will work without it.
49-
50-
```env file=.env highlight=4-5;add showLineNumbers
46+
```env file=.env highlight=5-7;add showLineNumbers
5147
# Connect to Supabase via connection pooling with Supavisor.
5248
DATABASE_URL="postgres://postgres.[your-supabase-project]:[password]@aws-0-[aws-region].pooler.supabase.com:6543/postgres?pgbouncer=true"
5349
54-
//add-start
55-
# Direct connection to the database. Used for migrations.
50+
# Direct connection to the database used by the Prisma CLI.
5651
DIRECT_URL="postgres://postgres.[your-supabase-project]:[password]@aws-0-[aws-region].pooler.supabase.com:5432/postgres"
5752
# or
5853
DIRECT_URL="postgresql://postgres:password@db.[your-project-ref].supabase.co:5432/postgres"
59-
//add-end
6054
```
6155

62-
You can then update your `schema.prisma` to use the new direct URL:
56+
Point `prisma.config.ts` to the direct connection string:
57+
58+
```ts file=prisma.config.ts showLineNumbers
59+
import 'dotenv/config'
60+
import { defineConfig, env } from 'prisma/config'
6361

64-
```prisma file=schema.prisma highlight=4;add showLineNumbers
65-
datasource db {
66-
provider = "postgresql"
67-
}
62+
export default defineConfig({
63+
schema: 'prisma/schema.prisma',
64+
datasource: {
65+
url: env('DIRECT_URL'),
66+
},
67+
})
6868
```
6969

70-
More information about the `directUrl` field can be found [here](/orm/reference/prisma-schema-reference#fields).
70+
At runtime, instantiate Prisma Client with a driver adapter using the pooled `DATABASE_URL`. This keeps the direct connection string scoped to Prisma CLI workflows while your application connections continue to flow through Supavisor.
71+
72+
```ts file=src/db/client.ts showLineNumbers
73+
import { PrismaClient } from '../prisma/generated/client'
74+
import { PrismaPg } from '@prisma/adapter-pg'
75+
76+
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL })
77+
export const prisma = new PrismaClient({ adapter })
78+
```
7179

7280
:::info
7381

content/200-orm/050-overview/500-databases/890-neon.mdx

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,46 @@ There are a few differences between Neon and PostgreSQL you should be aware of t
3939
## How to use Neon's connection pooling
4040

4141
If you would like to use the [connection pooling](https://neon.tech/docs/guides/prisma#use-connection-pooling-with-prisma) available in Neon, you will
42-
need to add `-pooler` in the hostname of your `DATABASE_URL` environment variable used in the `url` property of the `datasource` block of your Prisma schema:
42+
need to add `-pooler` in the hostname of the connection string that Prisma Client uses via a driver adapter:
4343

4444
```bash file=.env
4545
# Connect to Neon with Pooling.
4646
DATABASE_URL=postgres://daniel:<password>@ep-mute-rain-952417-pooler.us-east-2.aws.neon.tech:5432/neondb?sslmode=require
4747
```
4848

49-
If you would like to use Prisma CLI in order to perform other actions on your database (e.g. for migrations) you will need to add a `DIRECT_URL` environment variable to use in the `directUrl` property of the `datasource` block of your Prisma schema so that the CLI will use a direct connection string (without PgBouncer):
49+
Prisma CLI commands (for example, `prisma migrate` or `prisma db pull`) now read the direct connection string from `prisma.config.ts`. Configure both a pooled and non-pooled environment variable:
5050

51-
```env file=.env highlight=4-5;add showLineNumbers
52-
# Connect to Neon with Pooling.
51+
```env file=.env highlight=5-6;add showLineNumbers
52+
# Connect to Neon with pooling (used by Prisma Client via the adapter).
5353
DATABASE_URL=postgres://daniel:<password>@ep-mute-rain-952417-pooler.us-east-2.aws.neon.tech/neondb?sslmode=require
5454
55-
//add-start
56-
# Direct connection to the database used by Prisma CLI for e.g. migrations.
55+
# Direct connection to the database used by the Prisma CLI.
5756
DIRECT_URL="postgres://daniel:<password>@ep-mute-rain-952417.us-east-2.aws.neon.tech/neondb"
58-
//add-end
5957
```
6058

61-
You can then update your `schema.prisma` to use the new direct URL:
59+
Point `prisma.config.ts` to the direct connection string:
60+
61+
```ts file=prisma.config.ts showLineNumbers
62+
import 'dotenv/config'
63+
import { defineConfig, env } from 'prisma/config'
6264

63-
```prisma file=schema.prisma highlight=4;add showLineNumbers
64-
datasource db {
65-
provider = "postgresql"
66-
}
65+
export default defineConfig({
66+
schema: 'prisma/schema.prisma',
67+
datasource: {
68+
url: env('DIRECT_URL'),
69+
},
70+
})
6771
```
6872

69-
More information about the `directUrl` field can be found [here](/orm/reference/prisma-schema-reference#fields).
73+
At runtime, instantiate Prisma Client with the pooled connection string using `@prisma/adapter-neon`:
74+
75+
```ts file=src/db/client.ts showLineNumbers
76+
import { PrismaClient } from '../prisma/generated/client'
77+
import { PrismaNeon } from '@prisma/adapter-neon'
78+
79+
const adapter = new PrismaNeon({ connectionString: process.env.DATABASE_URL })
80+
export const prisma = new PrismaClient({ adapter })
81+
```
7082

7183
:::info
7284

content/200-orm/200-prisma-client/000-setup-and-configuration/050-databases-connections/200-pgbouncer.mdx

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,37 @@ Error: undefined: Database error
6565
Error querying the database: db error: ERROR: prepared statement "s0" already exists
6666
```
6767

68-
To work around this issue, you must connect directly to the database rather than going through PgBouncer. To achieve this, you can use the [`directUrl`](/orm/reference/prisma-schema-reference#fields) field in your [`datasource`](/orm/reference/prisma-schema-reference#datasource) block.
68+
To work around this issue, configure a **direct** connection for Prisma CLI commands in `prisma.config.ts`, while Prisma Client continues to use the PgBouncer URL via a driver adapter.
6969

70-
For example, consider the following `datasource` block:
70+
```env file=.env highlight=4-6;add showLineNumbers
71+
# PgBouncer (pooled) connection string used by Prisma Client.
72+
DATABASE_URL="postgres://USER:PASSWORD@HOST:PORT/DATABASE?pgbouncer=true"
7173
72-
```prisma
73-
datasource db {
74-
provider = "postgresql"
75-
url = "postgres://USER:PASSWORD@HOST:PORT/DATABASE?pgbouncer=true"
76-
directUrl = "postgres://USER:PASSWORD@HOST:PORT/DATABASE"
77-
}
74+
# Direct database connection string used by Prisma CLI.
75+
DIRECT_URL="postgres://USER:PASSWORD@HOST:PORT/DATABASE"
7876
```
7977

80-
The block above uses a PgBouncer connection string as the primary URL using `url`, allowing Prisma Client to take advantage of the PgBouncer connection pooler.
78+
```ts file=prisma.config.ts showLineNumbers
79+
import 'dotenv/config'
80+
import { defineConfig, env } from 'prisma/config'
8181

82-
It also provides a connection string directly to the database, without PgBouncer, using the `directUrl` field. This connection string will be used when commands that require a single connection to the database, such as `prisma migrate dev` or `prisma db push`, are invoked.
82+
export default defineConfig({
83+
schema: 'prisma/schema.prisma',
84+
datasource: {
85+
url: env('DIRECT_URL'),
86+
},
87+
})
88+
```
89+
90+
```ts file=src/db/client.ts showLineNumbers
91+
import { PrismaClient } from '../prisma/generated/client'
92+
import { PrismaPg } from '@prisma/adapter-pg'
93+
94+
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL })
95+
export const prisma = new PrismaClient({ adapter })
96+
```
97+
98+
With this setup, PgBouncer stays in the path for runtime traffic, while Prisma CLI commands (`prisma migrate dev`, `prisma db push`, `prisma db pull`, and so on) always use the direct connection string defined in `prisma.config.ts`.
8399

84100
### PgBouncer with different database providers
85101

content/200-orm/200-prisma-client/000-setup-and-configuration/050-databases-connections/index.mdx

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -336,29 +336,39 @@ datasource db {
336336

337337
Connection poolers like [Prisma Accelerate](/accelerate) and PgBouncer prevent your application from exhausting the database's connection limit.
338338

339-
If you would like to use the Prisma CLI in order to perform other actions on your database ,e.g. migrations and introspection, you will need to add an environment variable that provides a direct connection to your database in the `datasource.directUrl` property in your Prisma schema:
339+
To keep Prisma Client on the pooled connection while allowing Prisma CLI commands (for example, migrations or introspection) to connect directly, define two environment variables:
340340

341-
```env file=.env highlight=4,5;add showLineNumbers
341+
```env file=.env highlight=4-6;add showLineNumbers
342342
# Connection URL to your database using PgBouncer.
343343
DATABASE_URL="postgres://root:[email protected]:54321/postgres?pgbouncer=true"
344344
345-
//add-start
346-
# Direct connection URL to the database used for migrations
345+
# Direct connection URL to the database used for Prisma CLI commands.
347346
DIRECT_URL="postgres://root:[email protected]:5432/postgres"
348-
//add-end
349347
```
350348

351-
You can then update your `schema.prisma` to use the new direct URL:
349+
Configure `prisma.config.ts` to point to the direct connection string. Prisma CLI commands always read from this configuration.
352350

353-
```prisma file=schema.prisma highlight=4;add showLineNumbers
354-
datasource db {
355-
provider = "postgresql"
356-
//add-next-line
357-
directUrl = env("DIRECT_URL")
358-
}
351+
```ts file=prisma.config.ts showLineNumbers
352+
import 'dotenv/config'
353+
import { defineConfig, env } from 'prisma/config'
354+
355+
export default defineConfig({
356+
schema: 'prisma/schema.prisma',
357+
datasource: {
358+
url: env('DIRECT_URL'),
359+
},
360+
})
359361
```
360362

361-
More information about the `directUrl` field can be found [here](/orm/reference/prisma-schema-reference#fields).
363+
At runtime, instantiate Prisma Client with a driver adapter (for example, `@prisma/adapter-pg`) that uses the pooled connection string:
364+
365+
```ts file=src/db/client.ts showLineNumbers
366+
import { PrismaClient } from '../prisma/generated/client'
367+
import { PrismaPg } from '@prisma/adapter-pg'
368+
369+
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL })
370+
export const prisma = new PrismaClient({ adapter })
371+
```
362372

363373
### Prisma Accelerate
364374

content/200-orm/200-prisma-client/100-queries/037-relation-queries.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ Here's a visual representation of how a nested create operation can write to sev
640640

641641
#### Using nested `createMany`
642642

643-
The following query uses a nested [`createMany`](/orm/reference/prisma-client-reference#create) to create:
643+
The following query uses a nested [`createMany`](/orm/reference/prisma-client-reference#createmany) to create:
644644

645645
- One user
646646
- Two posts

content/200-orm/200-prisma-client/150-using-raw-sql/100-typedsql.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ TypedSQL does not work with MongoDB, as it is specifically designed for SQL data
244244

245245
### Active Database Connection Required
246246

247-
TypedSQL requires an active database connection to function properly. This means you need to have a running database instance that Prisma can connect to when generating the client with the `--sql` flag. If a `directUrl` is provided in your Prisma configuration, TypedSQL will use that for the connection.
247+
TypedSQL requires an active database connection to function properly. This means you need to have a running database instance that Prisma can connect to when generating the client with the `--sql` flag. TypedSQL uses the connection string defined in `prisma.config.ts` (`datasource.url`) to establish this connection.
248248

249249
### Dynamic SQL Queries with Dynamic Columns
250250

content/200-orm/200-prisma-client/500-deployment/301-edge/485-deploy-to-vercel.mdx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,14 @@ model User {
170170
If you are using Vercel Postgres, you need to:
171171

172172
- use the `@prisma/adapter-neon` database adapter because Vercel Postgres uses [Neon](https://neon.tech/) under the hood
173-
- be aware that Vercel by default calls the environment variable for the database connection string `POSTGRES_PRISMA_URL` while the default name used in the Prisma docs is typically `DATABASE_URL`; using Vercel's naming, you need to configure these in `prisma.config.ts`:
173+
- be aware that Vercel by default calls the pooled connection string `POSTGRES_PRISMA_URL` while the direct, non-pooled connection string is exposed as `POSTGRES_URL_NON_POOLING`. Configure `prisma.config.ts` so that the Prisma CLI uses the direct connection string:
174174
```ts file=prisma.config.ts
175175
import { defineConfig, env } from 'prisma/config'
176176

177177
export default defineConfig({
178178
schema: 'prisma/schema.prisma',
179179
datasource: {
180-
url: env('POSTGRES_PRISMA_URL'), // uses connection pooling
181-
directUrl: env('POSTGRES_URL_NON_POOLING'), // uses a direct connection
180+
url: env('POSTGRES_URL_NON_POOLING'), // direct connection for Prisma CLI
182181
},
183182
})
184183
```
@@ -211,8 +210,7 @@ import { defineConfig, env } from 'prisma/config'
211210
export default defineConfig({
212211
schema: 'prisma/schema.prisma',
213212
datasource: {
214-
url: env('POSTGRES_PRISMA_URL'), // uses connection pooling
215-
directUrl: env('POSTGRES_URL_NON_POOLING'), // uses a direct connection
213+
url: env('POSTGRES_URL_NON_POOLING'), // direct connection for Prisma CLI
216214
},
217215
})
218216
```

content/300-accelerate/200-getting-started.mdx

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,7 @@ DATABASE_URL="prisma://accelerate.prisma-data.net/?api_key=__API_KEY__"
4141
# DATABASE_URL="postgresql://user:password@host:port/db_name?schema=public"
4242
```
4343

44-
Your updated connection string will be used as the datasource `url` in your Prisma schema file;
45-
46-
```prisma
47-
datasource db {
48-
provider = "postgresql"
49-
url = env("DATABASE_URL")
50-
}
51-
```
44+
Prisma Client reads the `prisma://` URL from `DATABASE_URL` at runtime, while Prisma CLI commands use the connection string defined in `prisma.config.ts`.
5245

5346
Prisma Migrate and Introspection do not work with a `prisma://` connection string. In order to continue using these features add a new variable to the `.env` file named `DIRECT_DATABASE_URL` whose value is the direct database connection string:
5447

@@ -57,15 +50,18 @@ DATABASE_URL="prisma://accelerate.prisma-data.net/?api_key=__API_KEY__"
5750
DIRECT_DATABASE_URL="postgresql://user:password@host:port/db_name?schema=public"
5851
```
5952

60-
Then in your Prisma schema's `datasource` block add a field named `directUrl` with the following:
53+
Then point `prisma.config.ts` to the direct connection string:
6154

62-
```prisma
63-
datasource db {
64-
provider = "postgresql"
65-
url = env("DATABASE_URL")
66-
//add-next-line
67-
directUrl = env("DIRECT_DATABASE_URL")
68-
}
55+
```ts file=prisma.config.ts showLineNumbers
56+
import 'dotenv/config'
57+
import { defineConfig, env } from 'prisma/config'
58+
59+
export default defineConfig({
60+
schema: 'prisma/schema.prisma',
61+
datasource: {
62+
url: env('DIRECT_DATABASE_URL'),
63+
},
64+
})
6965
```
7066

7167
Migrations and introspections will use the `directUrl` connection string rather than the one defined in `url` when this configuration is provided.
@@ -134,7 +130,9 @@ Add the following to extend your existing Prisma Client instance with the Accele
134130
import { PrismaClient } from '@prisma/client'
135131
import { withAccelerate } from '@prisma/extension-accelerate'
136132

137-
const prisma = new PrismaClient().$extends(withAccelerate())
133+
const prisma = new PrismaClient({
134+
accelerateUrl: process.env.DATABASE_URL,
135+
}).$extends(withAccelerate())
138136
```
139137

140138
If you are going to deploy to an edge runtime (like Cloudflare Workers, Vercel Edge Functions, Deno Deploy, or Supabase Edge Functions), use our edge client instead:
@@ -143,7 +141,9 @@ If you are going to deploy to an edge runtime (like Cloudflare Workers, Vercel E
143141
import { PrismaClient } from '@prisma/client/edge'
144142
import { withAccelerate } from '@prisma/extension-accelerate'
145143

146-
const prisma = new PrismaClient().$extends(withAccelerate())
144+
const prisma = new PrismaClient({
145+
accelerateUrl: process.env.DATABASE_URL,
146+
}).$extends(withAccelerate())
147147
```
148148

149149
If VS Code does not recognize the `$extends` method, refer to [this section](/accelerate/faq#vs-code-does-not-recognize-the-extends-method) on how to resolve the issue.
@@ -155,7 +155,11 @@ Since [extensions are applied one after another](/orm/prisma-client/client-exten
155155
If you are using [Prisma Optimize](/optimize) in your application, make sure you apply it _before_ the Accelerate extension. For example:
156156

157157
```ts
158-
const prisma = new PrismaClient().$extends(withOptimize()).$extends(withAccelerate())
158+
const prisma = new PrismaClient({
159+
accelerateUrl: process.env.DATABASE_URL,
160+
})
161+
.$extends(withOptimize())
162+
.$extends(withAccelerate())
159163
```
160164

161165
### 2.5. Use Accelerate in your database queries

0 commit comments

Comments
 (0)