Skip to content
Menu

DocsFrameworks

View as Markdown

Prisma

Use Prisma ORM with KeelDB, with the app on the pooled host and Prisma Migrate on the direct host, for Prisma 7 and Prisma 6.

Prisma ORM works with KeelDB like with any PostgreSQL server. Two things are specific:

  • Two connection strings. Your app connects through the pooled host, and the Prisma CLI (migrations, introspection) through the direct host, because the pooler runs PgBouncer in transaction mode and migrations need a session of their own.
  • No CREATE DATABASE. Your KeelDB role owns its database but can't create others, so prisma migrate dev can't create its temporary shadow database on KeelDB. Run migrate dev against a local database, or point it at a second KeelDB database (see Shadow database). prisma migrate deploy needs no shadow database.

Copy both strings from the database's Overview page:

# .env
DATABASE_URL="postgresql://shop_owner:<password>@k7m2x9qa4pz1-pooler.us-east.db.keeldb.com:5432/shop?sslmode=require&connect_timeout=10"
DIRECT_URL="postgresql://shop_owner:<password>@k7m2x9qa4pz1.us-east.db.keeldb.com:5432/shop?sslmode=require&connect_timeout=10"

Prisma 7

Prisma 7 reads the CLI's connection from prisma.config.ts and connects your app through a driver adapter.

npm install @prisma/client @prisma/adapter-pg pg dotenv
npm install --save-dev prisma @types/pg
// prisma.config.ts: the CLI (migrate, introspect) uses the direct host
import "dotenv/config";
import { defineConfig, env } from "prisma/config";

export default defineConfig({
  schema: "prisma/schema.prisma",
  migrations: { path: "prisma/migrations" },
  datasource: { url: env("DIRECT_URL") },
});
// prisma/schema.prisma
generator client {
  provider = "prisma-client"
  output   = "../src/generated/prisma"
}

datasource db {
  provider = "postgresql"
}

model Note {
  id        Int      @id @default(autoincrement())
  title     String
  createdAt DateTime @default(now())
}
// src/lib/prisma.ts: the app uses the pooled host
import { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient } from "../generated/prisma/client";

const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL! });

const globalForPrisma = globalThis as unknown as { prisma?: PrismaClient };
export const prisma = globalForPrisma.prisma ?? new PrismaClient({ adapter });
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;

Keeping the client on globalThis in development stops hot reloads (Next.js, for example) from opening a new pool on every edit.

Prisma 6

Prisma 6 keeps both connection strings in the schema. Add pgbouncer=true to the pooled string so Prisma's query engine adapts to transaction mode:

DATABASE_URL="postgresql://shop_owner:<password>@k7m2x9qa4pz1-pooler.us-east.db.keeldb.com:5432/shop?sslmode=require&pgbouncer=true&connect_timeout=10"
datasource db {
  provider  = "postgresql"
  url       = env("DATABASE_URL")
  directUrl = env("DIRECT_URL")
}

Migrations

Create migrations on your machine, apply them to KeelDB in deployment:

# Development: creates a migration from schema changes (needs a shadow database)
npx prisma migrate dev --name add_notes

# Deployment and CI: applies pending migrations, no shadow database
npx prisma migrate deploy

Run migrate deploy in your release step or CI, not in every serverless function start.

Shadow database

prisma migrate dev checks your migrations in a temporary shadow database. On a local PostgreSQL, Prisma creates and drops it for you. To develop against KeelDB instead, create a second, empty database (a Free database works) and give its direct string to Prisma as the shadow database:

// prisma.config.ts (Prisma 7)
datasource: {
  url: env("DIRECT_URL"),
  shadowDatabaseUrl: env("SHADOW_DATABASE_URL"),
},

With Prisma 6, add shadowDatabaseUrl = env("SHADOW_DATABASE_URL") to the datasource block. Prisma resets the shadow database each time, so never point it at a database with data you need.

Branches for every pull request

A branch is a full copy of a database with its own hostnames and password, ready in seconds on Free and Launch. Run prisma migrate deploy against a branch's direct string to test a migration on real data before it reaches production. Preview environments per pull request shows a GitHub Actions workflow that does this for every pull request.

Troubleshooting

SymptomCause and fix
prepared statement "s0" already exists (Prisma 6)The pooled string is missing pgbouncer=true.
permission denied to create database from migrate devPrisma tried to create a shadow database. Use a local database, or set shadowDatabaseUrl.
Migrations fail or hangThe CLI is using the pooled host, where the session lock Prisma Migrate takes doesn't hold. Point the CLI at the direct host (Prisma 7: datasource.url in prisma.config.ts; Prisma 6: directUrl).
Can't reach database server on the first request after idle timeA sleeping Free or Launch database is waking. Raise connect_timeout to 30; see Waking a sleeping database.