Deploy to Vercel Edge Functions & Middleware
This page covers everything you need to know to deploy an app that uses Prisma Client for talking to a database in Vercel Edge Middleware or a Vercel Function deployed to the Vercel Edge Runtime.
Questions answered in this page
- How to deploy Prisma on Vercel Edge?
- Which database drivers are supported?
- How to configure env and postinstall?
Vercel supports both Node.js and edge runtimes for Vercel Functions. The Node.js runtime is the default and recommended for most use cases.
General considerations when deploying to Vercel Edge Functions & Edge Middleware
Section titled “General considerations when deploying to Vercel Edge Functions & Edge Middleware”You can use Prisma Postgres in Vercel's edge runtime. Follow this guide for an end-to-end tutorial on deploying an application to Vercel using Prisma Postgres.
Vercel's Edge Runtime currently only supports a limited set of database drivers:
- Neon Serverless uses HTTP to access the database (also compatible with Vercel Postgres)
- PlanetScale Serverless uses HTTP to access the database
@libsql/clientis used to access Turso databases
Note that node-postgres (pg) is currently not supported on Vercel Edge Functions.
When deploying a Vercel Edge Function that uses Prisma ORM, you need to use one of these edge-compatible drivers and its respective driver adapter for Prisma ORM.
Setting your database connection URL as an environment variable
Section titled “Setting your database connection URL as an environment variable”First, ensure that your datasource block in your Prisma schema is configured correctly. Database connection URLs are configured in prisma.config.ts:
datasource db {
provider = "postgresql" // this might also be `mysql` or another value depending on your database
}import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
datasource: {
url: env("DATABASE_URL"),
},
});When in development, you can configure your database connection via the DATABASE_URL environment variable (e.g. using .env files).
When deploying your Edge Function to production, you'll need to set the database connection using the vercel CLI:
bunx vercel env add DATABASE_URLpnpm dlx vercel env add DATABASE_URLyarn dlx vercel env add DATABASE_URLnpx vercel env add DATABASE_URLThis command is interactive and will ask you to select environments and provide the value for the DATABASE_URL in subsequent steps.
Alternatively, you can configure the environment variable via the UI of your project in the Vercel Dashboard.
This command is interactive and will ask you to select environments and provide the value for the DATABASE_URL in subsequent steps.
Alternatively, you can configure the environment variable via the UI of your project in the Vercel Dashboard.
Generate Prisma Client in postinstall hook
Section titled “Generate Prisma Client in postinstall hook”In your package.json, you should add a "postinstall" section as follows:
{
// ...,
"postinstall": "prisma generate"
}Vercel has a size limit of 1 MB on free accounts. If your application bundle with Prisma ORM exceeds that size, use an engine-less Prisma Client with a compatible driver adapter or upgrade to a plan with a larger limit.
Database-specific considerations & examples
Section titled “Database-specific considerations & examples”This section provides database-specific instructions for deploying a Vercel Edge Functions with Prisma ORM.
As a prerequisite for the following section, you need to have a Vercel Edge Function (which typically comes in the form of a Next.js API route) running locally and the Prisma and Vercel CLIs installed.
If you don't have that yet, you can run these commands to set up a Next.js app from scratch (following the instructions of the Vercel Functions Quickstart):
bun add --global vercelpnpm add -g vercelyarn global add vercelnpm install -g vercelbunx create-next-app@latestpnpm dlx create-next-app@latestyarn dlx create-next-app@latestnpx create-next-app@latestbun add prisma@prev --dev&& bun add @prisma/client@7pnpm add prisma@prev --save-dev&& pnpm add @prisma/client@7yarn add prisma@prev --dev&& yarn add @prisma/client@7npm install prisma@prev --save-dev && npm install @prisma/client@7bunx --bun prisma init --output ../app/generated/prismapnpm prisma init --output ../app/generated/prismayarn prisma init --output ../app/generated/prismanpx prisma init --output ../app/generated/prismaWe'll use the default User model for the example below:
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
}If you are using Vercel Postgres, you need to:
-
use the
@prisma/adapter-neondatabase adapter because Vercel Postgres uses Neon under the hood -
be aware that Vercel by default calls the pooled connection string
POSTGRES_PRISMA_URLwhile the direct, non-pooled connection string is exposed asPOSTGRES_URL_NON_POOLING. Configureprisma.config.tsso that the Prisma CLI uses the direct connection string:title="prisma.config.ts" import { defineConfig, env } from "prisma/config"; export default defineConfig({ schema: "prisma/schema.prisma", datasource: { url: env("POSTGRES_URL_NON_POOLING"), // direct connection for Prisma CLI }, });
1. Configure Prisma schema & database connection
Section titled “1. Configure Prisma schema & database connection”First, ensure that the database connection is configured properly. Database connection URLs are configured in prisma.config.ts:
generator client {
provider = "prisma-client"
output = "./generated"
}
datasource db {
provider = "postgresql"
}import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
datasource: {
url: env("POSTGRES_URL_NON_POOLING"), // direct connection for Prisma CLI
},
});Next, you need to set the POSTGRES_PRISMA_URL and POSTGRES_URL_NON_POOLING environment variable to the values of your database connection.
If you ran npx prisma init, you can use the .env file that was created by this command to set these:
POSTGRES_PRISMA_URL="postgres://user:password@host-pooler.region.postgres.vercel-storage.com:5432/name?pgbouncer=true&connect_timeout=15"
POSTGRES_URL_NON_POOLING="postgres://user:password@host.region.postgres.vercel-storage.com:5432/name"Next, install the required packages:
bun add @prisma/adapter-neonpnpm add @prisma/adapter-neonyarn add @prisma/adapter-neonnpm install @prisma/adapter-neonNext, add a new key to the scripts section in your package.json:
{
// ...
"scripts": {
// ...
"postinstall": "prisma generate"
}
}4. Migrate your database schema (if applicable)
Section titled “4. Migrate your database schema (if applicable)”If you ran npx prisma init above, you need to migrate your database schema to create the User table that's defined in your Prisma schema (if you already have all the tables you need in your database, you can skip this step):
bunx prisma migrate dev --name initpnpm prisma migrate dev --name inityarn prisma migrate dev --name initnpx prisma migrate dev --name init5. Use Prisma Client in your Vercel Edge Function to send a query to the database
Section titled “5. Use Prisma Client in your Vercel Edge Function to send a query to the database”If you created the project from scratch, you can create a new edge function as follows.
First, create a new API route, e.g. by using these commands:
mkdir src/app/api
mkdir src/app/api/edge
touch src/app/api/edge/route.tsHere is a sample code snippet that you can use to instantiate PrismaClient and send a query to your database in the new app/api/edge/route.ts file you just created:
import { NextResponse } from "next/server";
import { PrismaClient } from "./generated/client";
import { PrismaNeon } from "@prisma/adapter-neon";
export const runtime = "nodejs"; // can also be set to 'edge'
export async function GET(request: Request) {
const adapter = new PrismaNeon({ connectionString: process.env.POSTGRES_PRISMA_URL });
const prisma = new PrismaClient({ adapter });
const users = await prisma.user.findMany();
return NextResponse.json(users, { status: 200 });
}Run the app with the following command:
bun run devpnpm run devyarn devnpm run devYou can now access the Edge Function via http://localhost:3000/api/edge.
7. Set the POSTGRES_PRISMA_URL environment variable and deploy the Edge Function
Section titled “7. Set the POSTGRES_PRISMA_URL environment variable and deploy the Edge Function”Run the following command to deploy your project with Vercel:
bunx vercel deploypnpm dlx vercel deployyarn dlx vercel deploynpx vercel deployNote that once the project was created on Vercel, you will need to set the POSTGRES_PRISMA_URL environment variable (and if this was your first deploy, it likely failed). You can do this either via the Vercel UI or by running the following command:
Note that once the project was created on Vercel, you will need to set the POSTGRES_PRISMA_URL environment variable (and if this was your first deploy, it likely failed). You can do this either via the Vercel UI or by running the following command:
bunx vercel env add POSTGRES_PRISMA_URLpnpm dlx vercel env add POSTGRES_PRISMA_URLyarn dlx vercel env add POSTGRES_PRISMA_URLnpx vercel env add POSTGRES_PRISMA_URLAt this point, you can get the URL of the deployed application from the Vercel Dashboard and access the edge function via the /api/edge route.
At this point, you can get the URL of the deployed application from the Vercel Dashboard and access the edge function via the /api/edge route.
If you are using a PlanetScale database, you need to:
- use the
@prisma/adapter-planetscaledatabase adapter (learn more in PlanetScale)
1. Configure Prisma schema & database connection
Section titled “1. Configure Prisma schema & database connection”First, ensure that the database connection is configured properly. Database connection URLs are configured in prisma.config.ts:
generator client {
provider = "prisma-client"
output = "./generated"
}
datasource db {
provider = "mysql"
relationMode = "prisma" // required for PlanetScale (as by default foreign keys are disabled)
}import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
datasource: {
url: env("DATABASE_URL"),
},
});Next, you need to set the DATABASE_URL environment variable in your .env file that's used both by Prisma and Next.js to read your env vars:
DATABASE_URL="mysql://32qxa2r7hfl3102wrccj:password@us-east.connect.psdb.cloud/demo-cf-worker-ps?sslaccept=strict"Next, install the required packages:
bun add @prisma/adapter-planetscalepnpm add @prisma/adapter-planetscaleyarn add @prisma/adapter-planetscalenpm install @prisma/adapter-planetscaleNext, add a new key to the scripts section in your package.json:
{
// ...
"scripts": {
// ...
"postinstall": "prisma generate"
}
}4. Migrate your database schema (if applicable)
Section titled “4. Migrate your database schema (if applicable)”If you ran npx prisma init above, you need to migrate your database schema to create the User table that's defined in your Prisma schema (if you already have all the tables you need in your database, you can skip this step):
bunx prisma db pushpnpm prisma db pushyarn prisma db pushnpx prisma db push5. Use Prisma Client in an Edge Function to send a query to the database
Section titled “5. Use Prisma Client in an Edge Function to send a query to the database”If you created the project from scratch, you can create a new edge function as follows.
First, create a new API route, e.g. by using these commands:
mkdir src/app/api
mkdir src/app/api/edge
touch src/app/api/edge/route.tsHere is a sample code snippet that you can use to instantiate PrismaClient and send a query to your database in the new app/api/edge/route.ts file you just created:
import { NextResponse } from "next/server";
import { PrismaClient } from "./generated/client";
import { PrismaPlanetScale } from "@prisma/adapter-planetscale";
export const runtime = "nodejs"; // can also be set to 'edge'
export async function GET(request: Request) {
const adapter = new PrismaPlanetScale({ url: process.env.DATABASE_URL });
const prisma = new PrismaClient({ adapter });
const users = await prisma.user.findMany();
return NextResponse.json(users, { status: 200 });
}Run the app with the following command:
bun run devpnpm run devyarn devnpm run devYou can now access the Edge Function via http://localhost:3000/api/edge.
7. Set the DATABASE_URL environment variable and deploy the Edge Function
Section titled “7. Set the DATABASE_URL environment variable and deploy the Edge Function”Run the following command to deploy your project with Vercel:
bunx vercel deploypnpm dlx vercel deployyarn dlx vercel deploynpx vercel deployNote that once the project was created on Vercel, you will need to set the DATABASE_URL environment variable (and if this was your first deploy, it likely failed). You can do this either via the Vercel UI or by running the following command:
Note that once the project was created on Vercel, you will need to set the DATABASE_URL environment variable (and if this was your first deploy, it likely failed). You can do this either via the Vercel UI or by running the following command:
bunx vercel env add DATABASE_URLpnpm dlx vercel env add DATABASE_URLyarn dlx vercel env add DATABASE_URLnpx vercel env add DATABASE_URLAt this point, you can get the URL of the deployed application from the Vercel Dashboard and access the edge function via the /api/edge route.
At this point, you can get the URL of the deployed application from the Vercel Dashboard and access the edge function via the /api/edge route.
If you are using a Neon database, you need to:
- use the
@prisma/adapter-neondatabase adapter (learn more in Using driver adapters)
1. Configure Prisma schema & database connection
Section titled “1. Configure Prisma schema & database connection”First, ensure that the database connection is configured properly. Database connection URLs are configured in prisma.config.ts:
generator client {
provider = "prisma-client"
output = "./generated"
}
datasource db {
provider = "postgresql"
}import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
datasource: {
url: env("DATABASE_URL"),
},
});Next, you need to set the DATABASE_URL environment variable in your .env file that's used both by Prisma and Next.js to read your env vars:
DATABASE_URL="postgresql://janedoe:password@ep-nameless-pond-a23b1mdz.eu-central-1.aws.neon.tech/neondb?sslmode=require"Next, install the required packages:
bun add @prisma/adapter-neonpnpm add @prisma/adapter-neonyarn add @prisma/adapter-neonnpm install @prisma/adapter-neonNext, add a new key to the scripts section in your package.json:
{
// ...
"scripts": {
// ...
"postinstall": "prisma generate"
}
}4. Migrate your database schema (if applicable)
Section titled “4. Migrate your database schema (if applicable)”If you ran npx prisma init above, you need to migrate your database schema to create the User table that's defined in your Prisma schema (if you already have all the tables you need in your database, you can skip this step):
bunx prisma migrate dev --name initpnpm prisma migrate dev --name inityarn prisma migrate dev --name initnpx prisma migrate dev --name init5. Use Prisma Client in an Edge Function to send a query to the database
Section titled “5. Use Prisma Client in an Edge Function to send a query to the database”If you created the project from scratch, you can create a new edge function as follows.
First, create a new API route, e.g. by using these commands:
mkdir src/app/api
mkdir src/app/api/edge
touch src/app/api/edge/route.tsHere is a sample code snippet that you can use to instantiate PrismaClient and send a query to your database in the new app/api/edge/route.ts file you just created:
import { NextResponse } from "next/server";
import { PrismaClient } from "./generated/client";
import { PrismaNeon } from "@prisma/adapter-neon";
export const runtime = "nodejs"; // can also be set to 'edge'
export async function GET(request: Request) {
const adapter = new PrismaNeon({ connectionString: process.env.DATABASE_URL });
const prisma = new PrismaClient({ adapter });
const users = await prisma.user.findMany();
return NextResponse.json(users, { status: 200 });
}Run the app with the following command:
bun run devpnpm run devyarn devnpm run devYou can now access the Edge Function via http://localhost:3000/api/edge.
7. Set the DATABASE_URL environment variable and deploy the Edge Function
Section titled “7. Set the DATABASE_URL environment variable and deploy the Edge Function”Run the following command to deploy your project with Vercel:
bunx vercel deploypnpm dlx vercel deployyarn dlx vercel deploynpx vercel deployNote that once the project was created on Vercel, you will need to set the DATABASE_URL environment variable (and if this was your first deploy, it likely failed). You can do this either via the Vercel UI or by running the following command:
Note that once the project was created on Vercel, you will need to set the DATABASE_URL environment variable (and if this was your first deploy, it likely failed). You can do this either via the Vercel UI or by running the following command:
bunx vercel env add DATABASE_URLpnpm dlx vercel env add DATABASE_URLyarn dlx vercel env add DATABASE_URLnpx vercel env add DATABASE_URLAt this point, you can get the URL of the deployed application from the Vercel Dashboard and access the edge function via the /api/edge route.
At this point, you can get the URL of the deployed application from the Vercel Dashboard and access the edge function via the /api/edge route.
Fluid compute is Vercel's compute model for dynamic workloads such as streaming data and AI APIs. Vercel's Fluid compute supports both edge and Node.js runtimes. A common challenge in traditional serverless platforms is leaked database connections when functions are suspended and pools can't close idle connections. Fluid provides attachDatabasePool to ensure idle connections are released before a function is suspended.
Use attachDatabasePool together with Prisma's driver adapters to safely manage connections in Fluid:
import { Pool } from "pg";
import { attachDatabasePool } from "@vercel/functions";
import { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient } from "./generated/client";
const pool = new Pool({ connectionString: process.env.POSTGRES_URL });
attachDatabasePool(pool);
const prisma = new PrismaClient({
adapter: new PrismaPg(pool),
});