Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ import "dotenv/config";
import { PrismaClient } from "../generated/prisma/client";
import { PrismaPg } from "@prisma/adapter-pg";

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,40 @@ Use JavaScript database drivers via [driver adapters](/orm/core-concepts/support
**Standard PostgreSQL with `pg`:**

```npm
npm install @prisma/adapter-pg
npm install @prisma/adapter-pg pg dotenv
```

```ts
import "dotenv/config";
import { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient } from "./generated/prisma";

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

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

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

`PrismaPg` accepts the same connection options as `pg.PoolConfig`. If you need to manage a `pg.Pool` yourself, you can pass the pool instance instead:

```ts
import "dotenv/config";
import { Pool } from "pg";
import { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient } from "./generated/prisma";

const connectionString = process.env.DATABASE_URL;

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,13 @@ import "dotenv/config";
import { PrismaClient } from "../generated/prisma/client";
import { PrismaPg } from "@prisma/adapter-pg";

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

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

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

Expand Down
29 changes: 26 additions & 3 deletions apps/docs/content/docs/orm/v6/overview/databases/postgresql.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ This section explains how you can use it with Prisma ORM and the `@prisma/adapte

### 1. Install the dependencies

First, install Prisma ORM's driver adapter for `pg`:
First, install Prisma ORM's driver adapter for `pg`, the `pg` driver, and `dotenv` if you load your connection string from `.env`:

```npm
npm install @prisma/adapter-pg
npm install @prisma/adapter-pg pg dotenv
```

### 2. Instantiate Prisma Client using the driver adapter
Expand All @@ -73,14 +73,37 @@ import "dotenv/config";
import { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient } from "../generated/prisma/client";

const connectionString = `${process.env.DATABASE_URL}`;
const connectionString = process.env.DATABASE_URL;

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

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

Notice that this code requires the `DATABASE_URL` environment variable to be set to your PostgreSQL connection string. You can learn more about the connection string below.

`PrismaPg` accepts the same connection options as `pg.PoolConfig`. If you need to manage a `pg.Pool` yourself, you can pass the pool instance instead:

```ts
import "dotenv/config";
import { Pool } from "pg";
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 not set");
}

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

### Notes

#### Specifying a PostgreSQL schema
Expand Down
Loading