Neon with Accelerate (Prisma ORM v7)
Learn how to set up PostgreSQL on Neon with Prisma Accelerate's Connection Pool
Introduction
Section titled “Introduction”This guides teaches you how to add connection pooling to a PostgreSQL database hosted on Neon using Prisma Accelerate.
Prisma Accelerate is a connection pooler that keeps your database within its connection limits during traffic spikes and high load. Check out this video demonstrating how it performs in a load test or learn why connection pooling is important.
Prerequisites
Section titled “Prerequisites”To successfully complete this guide, you need a connection string for a PostgreSQL instance hosted on Neon. It typically looks similar to this:
postgresql://neondb_owner:[YOUR-PASSWORD]@ep-lingering-hat-a2e7tkt3.eu-central-1.aws.neon.tech/neondb?sslmode=requireIf you already have a project using Prisma ORM, you can skip the first two steps and jump ahead to Step 3. Install the Accelerate extension.
1. Set up Prisma ORM
Section titled “1. Set up Prisma ORM”Start by installing the Prisma CLI in your project:
bun add prisma@prev --devpnpm add prisma@prev --save-devyarn add prisma@prev --devnpm install prisma@prev --save-devThen, run the following command to initialize a new project:
bunx --bun prisma initpnpm prisma inityarn prisma initnpx prisma initThis will create a new prisma directory with a schema.prisma file and add a .env file with the DATABASE_URL environment variable.
Update the file and set the DATABASE_URL to your Neon connection string:
DATABASE_URL="postgresql://neondb_owner:[YOUR-PASSWORD]@ep-lingering-hat-a2e7tkt3.eu-central-1.aws.neon.tech/neondb?sslmode=require"Create a prisma.config.ts file to configure Prisma:
import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
},
datasource: {
url: env("DATABASE_URL"),
},
});```bash bun add dotenv ```
```bash pnpm add dotenv ```
```bash yarn add dotenv ```
```bash npm install dotenv ```
2. Introspect your database
Section titled “2. Introspect your database”Next, run the following command to introspect your database and create your data model:
bunx prisma db pullpnpm prisma db pullyarn prisma db pullnpx prisma db pullThis command reads your database schema and creates new models in your schema.prisma file that match the tables in your database.
3. Install the Accelerate extension
Section titled “3. Install the Accelerate extension”Install the Prisma Client extension for Accelerate:
bun add @prisma/extension-acceleratepnpm add @prisma/extension-accelerateyarn add @prisma/extension-acceleratenpm install @prisma/extension-accelerateThis is needed to access Prisma Accelerate's connection pool.
4. Set up Accelerate in the Prisma Console
Section titled “4. Set up Accelerate in the Prisma Console”To set up Accelerate in the Prisma Console, follow these steps:
- Log into the Prisma Console.
- Select New project
- Choose a Name for your project
- In the Choose your starting product section, find the Accelerate card and click Get started
- In the field for your Database connection string, paste your Neon connection string
- Select the Region that's closest to your database
- Click Create project
- On the next screen, click Enable Accelerate
Once you went through these steps, you'll be redirected to another page where you need to the click the Generate API key button.
You'll then be shown a new connection URL which enables you to connect to Prisma Accelerate's connection pool. This needs to be set as the new DATABASE_URL in your .env file:
DATABASE_URL="prisma://accelerate.prisma-data.net/?api_key=ey..."5. Generate Prisma Client
Section titled “5. Generate Prisma Client”With your Prisma schema in place, generate Prisma Client:
bunx prisma generatepnpm prisma generateyarn prisma generatenpx prisma generate6. Send queries through the connection pool
Section titled “6. Send queries through the connection pool”In your application code, you now need to apply the Accelerate extension to your Prisma Client instance:
import { PrismaClient } from "./generated/prisma/client";
import { withAccelerate } from "@prisma/extension-accelerate";
const prisma = new PrismaClient({
accelerateUrl: process.env.DATABASE_URL,
}).$extends(withAccelerate());At this point, you can now start sending queries which will be routed through the connection pool to your database.