Prisma

Integrate Better Auth with Prisma.

Prisma ORM is an open-source database toolkit that simplifies database access and management in applications by providing a type-safe query builder and an intuitive data modeling interface.

For an introduction to Prisma ORM, see Prisma getting started.

This guide uses Prisma 7 and PostgreSQL. If you use Prisma 6 or earlier, a driver adapter is optional and your existing Prisma Client setup can remain unchanged.

Installation

To use the Prisma adapter, install @better-auth/prisma-adapter:

npm install @better-auth/prisma-adapter

The example below uses Prisma 7 and PostgreSQL. If Prisma is not already configured, follow Prisma's PostgreSQL quickstart before continuing.

Setup

The examples below use the following project structure:

.env
prisma.config.ts
schema.prisma
client.ts
prisma.ts
auth.ts

Configure Prisma

If you are starting a new Prisma project, initialize it with PostgreSQL and an explicit Prisma Client output path:

Initialize Prisma
npx prisma init --datasource-provider postgresql --output ../src/generated/prisma

This creates prisma/schema.prisma, prisma.config.ts, and .env. Set DATABASE_URL in .env to your PostgreSQL connection string.

If Prisma is already configured in your project, keep your existing datasource and output path and skip this initialization command.

Generate Prisma Client after configuring its output path:

Generate Prisma Client
npx prisma generate

Create the Prisma client

Import PrismaClient from the output path configured in your Prisma schema and pass the PostgreSQL driver adapter to it:

src/lib/prisma.ts
import { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient } from "../generated/prisma/client";

const databaseUrl = process.env.DATABASE_URL;

if (!databaseUrl) {
  throw new Error("DATABASE_URL is not set");
}

const adapter = new PrismaPg({
  connectionString: databaseUrl,
});

export const prisma = new PrismaClient({ adapter });

Create one PrismaClient instance and reuse it across your application. Frameworks with hot reloading or serverless runtimes may require a framework-specific lifecycle pattern.

Configure Better Auth

Pass the Prisma Client instance to the Better Auth Prisma adapter:

src/lib/auth.ts
import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { prisma } from "./prisma";

export const auth = betterAuth({
  database: prismaAdapter(prisma, {
    provider: "postgresql",
  }),
});

Schema generation & migration

The Better Auth CLI generates the Prisma schema required by your Better Auth configuration and plugins. Use the Prisma CLI to create and apply a migration from the generated schema.

Prisma Schema Generation

Prisma Schema Migration

✅ Supported❌ Not Supported
Schema Generation
npx auth@latest generate

The Better Auth CLI updates your Prisma schema but does not apply the migration. Use Prisma to create the database migration, then regenerate Prisma Client:

Terminal
npx prisma migrate dev --name add-better-auth
npx prisma generate

Joins

Database joins are useful when Better-Auth needs to fetch related data from multiple tables in a single query. Endpoints like /get-session, /get-full-organization and many others benefit greatly from this feature, seeing upwards of 2x to 3x performance improvements depending on database latency.

The Prisma adapter supports joins out of the box since version 1.4.0. To enable this feature, set advanced.database.joins to true in your auth configuration.

auth.ts
import { betterAuth } from "better-auth";

export const auth = betterAuth({
  advanced: {
    database: {
      joins: true,
    },
  },
});

Please make sure that your Prisma schema has the necessary relations defined. If you do not see any relations in your Prisma schema, you can manually add them using the @relation directive or run our latest CLI version npx auth@latest generate to generate a new Prisma schema with the relations.

Additional Information