> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dbhost.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Next.js and Prisma with DBHost PostgreSQL

> Connect Prisma Client to a pooled DBHost database from Next.js and run Prisma Migrate over the direct migration connection with the owner role.

DBHost serves application queries through PgBouncer in transaction mode on port
`6432`. Prisma Client can use a pooled runtime URL through its PostgreSQL driver
adapter. **Prisma Migrate and `prisma db push` need a direct database connection**
according to [Prisma's PgBouncer guide](https://www.prisma.io/docs/orm/v7/prisma-client/setup-and-configuration/databases-connections/pgbouncer).
On DBHost, use the [direct migration connection](https://docs.dbhost.app/guides/direct-migrations.md)
(available for databases with managed SQL roles) for those commands, and the
pooled URL for your app.

## 1. Create a database and choose a role

Create a database from the DBHost dashboard. **Managed SQL roles** gives you
separate owner, runtime, and read-only credentials on every plan. Use the
runtime role for your app and the owner role for schema changes. With legacy
creation, the single database login remains the owner.

Copy the exact database name, selected username, and password from the detail
page. A managed username is generated separately from the database name.
Keep the connection URL in a private environment store and never commit it.

## 2. Configure Prisma Client

This example targets Prisma ORM 7, which requires Node.js 20.19 or later.
Pin the packages to one major version:

```bash theme={null}
npm install @prisma/client@7 @prisma/adapter-pg@7 pg
npm install -D prisma@7
```

Set the pooled runtime URL in your app's private environment. Replace every
placeholder with the values for the **runtime** role (or the legacy owner):

```bash theme={null}
DATABASE_URL="postgresql://YOUR_USERNAME:PASSWORD@db.dbhost.app:6432/YOUR_DATABASE?sslmode=verify-full"
```

Generate Prisma Client from your own schema using an explicit output path as
shown in [Prisma's generator guide](https://www.prisma.io/docs/orm/v7/prisma-client/setup-and-configuration/generating-prisma-client).
Then pass a PostgreSQL adapter the pooled URL in server-only code:

```ts theme={null}
// app/lib/prisma.ts
import { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient } from "../generated/prisma/client";

const connectionString = process.env.DATABASE_URL;
if (!connectionString) throw new Error("DATABASE_URL is required");

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

Adjust the import to the output path you chose in `schema.prisma`. Keep this
module on the server; do not import it into a Client Component.

## Schema changes

Do not point `prisma migrate dev`, `prisma migrate deploy`, or `prisma db push`
at the PgBouncer runtime URL. Prisma reads a separate migration URL from
`prisma.config.ts`. Set it to the direct migration profile while a lease is
active:

```ts theme={null}
// prisma.config.ts
import "dotenv/config";
import { defineConfig, env } from "prisma/config";

export default defineConfig({
  schema: "prisma/schema.prisma",
  datasource: {
    // postgresql://MIGRATION_USER:PASSWORD@migrate.dbhost.app:5432/YOUR_DATABASE?sslmode=verify-full
    url: env("MIGRATION_DATABASE_URL"),
  },
});
```

Your app keeps using `DATABASE_URL` on port 6432. On a single-login database
without managed SQL roles, apply reviewed SQL with the owner login instead. A
runtime role cannot perform DDL.

`prisma migrate dev` can also require a separate shadow database; DBHost does
not grant `CREATEDB` on the shared PostgreSQL host. See [Prisma's shadow database
guide](https://www.prisma.io/docs/orm/prisma-migrate/understanding-prisma-migrate/shadow-database)
before using that command.

## Next steps

* [DBHost quickstart](https://docs.dbhost.app/quickstart.md) for connection details and TLS.
* [Next.js + pg](https://docs.dbhost.app/guides/nextjs-pg.md) for an end-to-end query with no ORM migration dependency.
* [Prisma's PgBouncer guidance](https://www.prisma.io/docs/orm/v7/prisma-client/setup-and-configuration/databases-connections/pgbouncer) for current adapter and direct-URL requirements.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.