# Prisma (/docs/adapters/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](https://www.prisma.io/docs/getting-started).

<Callout type="info">
  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.
</Callout>

## Installation [#installation]

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

<CodeBlockTabs defaultValue="npm" groupId="persist-install">
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="npm">
      npm
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="pnpm">
      pnpm
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="yarn">
      yarn
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="bun">
      bun
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="npm">
    ```bash
    npm install @better-auth/prisma-adapter
    ```
  </CodeBlockTab>

  <CodeBlockTab value="pnpm">
    ```bash
    pnpm add @better-auth/prisma-adapter
    ```
  </CodeBlockTab>

  <CodeBlockTab value="yarn">
    ```bash
    yarn add @better-auth/prisma-adapter
    ```
  </CodeBlockTab>

  <CodeBlockTab value="bun">
    ```bash
    bun add @better-auth/prisma-adapter
    ```
  </CodeBlockTab>
</CodeBlockTabs>

The example below uses Prisma 7 and PostgreSQL. If Prisma is not already
configured, follow
[Prisma's PostgreSQL quickstart](https://www.prisma.io/docs/prisma-orm/quickstart/postgresql)
before continuing.

## Setup [#setup]

The examples below use the following project structure:

<Files>
  <File name=".env" />

  <File name="prisma.config.ts" />

  <Folder name="prisma">
    <File name="schema.prisma" />
  </Folder>

  <Folder name="src">
    <Folder name="generated">
      <Folder name="prisma">
        <File name="client.ts" />
      </Folder>
    </Folder>

    <Folder name="lib">
      <File name="prisma.ts" />

      <File name="auth.ts" />
    </Folder>
  </Folder>
</Files>

### Configure Prisma [#configure-prisma]

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

<CodeBlockTabs defaultValue="npm" groupId="persist-install">
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="npm">
      npm
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="pnpm">
      pnpm
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="yarn">
      yarn
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="bun">
      bun
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="npm">
    ```bash title="Initialize Prisma"
    npx prisma init --datasource-provider postgresql --output ../src/generated/prisma
    ```
  </CodeBlockTab>

  <CodeBlockTab value="pnpm">
    ```bash title="Initialize Prisma"
    pnpm dlx prisma init --datasource-provider postgresql --output ../src/generated/prisma
    ```
  </CodeBlockTab>

  <CodeBlockTab value="yarn">
    ```bash title="Initialize Prisma"
    yarn dlx prisma init --datasource-provider postgresql --output ../src/generated/prisma
    ```
  </CodeBlockTab>

  <CodeBlockTab value="bun">
    ```bash title="Initialize Prisma"
    bun x prisma init --datasource-provider postgresql --output ../src/generated/prisma
    ```
  </CodeBlockTab>
</CodeBlockTabs>

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:

<CodeBlockTabs defaultValue="npm" groupId="persist-install">
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="npm">
      npm
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="pnpm">
      pnpm
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="yarn">
      yarn
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="bun">
      bun
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="npm">
    ```bash title="Generate Prisma Client"
    npx prisma generate
    ```
  </CodeBlockTab>

  <CodeBlockTab value="pnpm">
    ```bash title="Generate Prisma Client"
    pnpm dlx prisma generate
    ```
  </CodeBlockTab>

  <CodeBlockTab value="yarn">
    ```bash title="Generate Prisma Client"
    yarn dlx prisma generate
    ```
  </CodeBlockTab>

  <CodeBlockTab value="bun">
    ```bash title="Generate Prisma Client"
    bun x prisma generate
    ```
  </CodeBlockTab>
</CodeBlockTabs>

### Create the Prisma client [#create-the-prisma-client]

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

```ts title="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 [#configure-better-auth]

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

```ts title="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 [#schema-generation--migration]

The [Better Auth CLI](/docs/concepts/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.

<table>
  <thead>
    <tr className="border-b">
      <th>
        <p className="font-bold text-[16px] mb-1">
          Prisma Schema Generation
        </p>
      </th>

      <th>
        <p className="font-bold text-[16px] mb-1">
          Prisma Schema Migration
        </p>
      </th>
    </tr>
  </thead>

  <tbody>
    <tr className="h-10">
      <td>
        ✅ Supported
      </td>

      <td>
        ❌ Not Supported
      </td>
    </tr>
  </tbody>
</table>

<CodeBlockTabs defaultValue="npm" groupId="persist-install">
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="npm">
      npm
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="pnpm">
      pnpm
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="yarn">
      yarn
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="bun">
      bun
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="npm">
    ```bash title="Schema Generation"
    npx auth@latest generate
    ```
  </CodeBlockTab>

  <CodeBlockTab value="pnpm">
    ```bash title="Schema Generation"
    pnpm dlx auth@latest generate
    ```
  </CodeBlockTab>

  <CodeBlockTab value="yarn">
    ```bash title="Schema Generation"
    yarn dlx auth@latest generate
    ```
  </CodeBlockTab>

  <CodeBlockTab value="bun">
    ```bash title="Schema Generation"
    bun x auth@latest generate
    ```
  </CodeBlockTab>
</CodeBlockTabs>

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:

```bash title="Terminal"
npx prisma migrate dev --name add-better-auth
npx prisma generate
```

## Joins [#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.

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

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

<Callout type="warn">
  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.
</Callout>

## Additional Information [#additional-information]

* If you're looking for performance improvements or tips, take a look at our guide to <Link href="/docs/guides/optimizing-for-performance">performance optimizations</Link>.
* [How to use Prisma ORM with Better Auth and Next.js](https://www.prisma.io/docs/guides/authentication/better-auth/nextjs)
* [How to use Prisma ORM with Better Auth and Astro](https://www.prisma.io/docs/guides/authentication/better-auth/astro)

