Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
64f84d3
feat(docs): add youtube embeded link to blog post (#7220)
ArthurGamby Oct 31, 2025
25dbc36
feat(docs): add quick section to blog after the prompt (#7221)
ArthurGamby Oct 31, 2025
d0cd80e
DC-5242 Astro Better-Auth Guide (#7215)
aidankmcalister Nov 3, 2025
ef969c8
feat: restructure getting started side nav
ankur-arch Nov 4, 2025
736f5af
DC-5841 Removed Linkspector (#7231)
aidankmcalister Nov 5, 2025
753b1b8
Update label for Prisma Postgres tab (#7236)
petradonka Nov 7, 2025
ddfc555
fix: content changes for getting started page (#7216)
ankur-arch Nov 7, 2025
2daa27a
DC-5820 AI Agents Served Markdown (#7237)
aidankmcalister Nov 10, 2025
4c19f41
feat: add prisma-orm quickstarts
ankur-arch Nov 10, 2025
8e5616e
fix: update times and add proper links
ankur-arch Nov 10, 2025
2a19cb7
fix: instropspect changes
ankur-arch Nov 10, 2025
5ac553e
feat: add get started from prisma orm page
ankur-arch Nov 10, 2025
ac2695e
feat: add other orms
ankur-arch Nov 11, 2025
6352851
fix: update other tools + ppg
ankur-arch Nov 11, 2025
eda1224
fix: add more clarity
ankur-arch Nov 11, 2025
2ca1ef2
fix: add prisma postgres
ankur-arch Nov 11, 2025
671e4cd
feat: clear migrate from early access
ankur-arch Nov 11, 2025
ab6ee45
fix: add to existing dbs sections
ankur-arch Nov 11, 2025
7f69693
Remove MCP server exploration tip (#7241)
petradonka Nov 11, 2025
04dd079
Merge branch 'prisma-7' into prisma-7-getting-started-restructure
ankur-arch Nov 11, 2025
f3365e8
fix: clean-up docs files
ankur-arch Nov 11, 2025
fc7d28a
fix: typeorm missing urls
ankur-arch Nov 11, 2025
4276072
fix: broken link
ankur-arch Nov 11, 2025
4a0e0c6
fix: update titles
ankur-arch Nov 11, 2025
43f7e1a
fix: clear redirect loop (#7250)
ankur-arch Nov 12, 2025
dc82e47
fix: add generate step + sqlite fixes
ankur-arch Nov 12, 2025
7edbd6f
Merge branch 'main' into prisma-7-getting-started-restructure
ankur-arch Nov 13, 2025
ddf0c20
fix: clean-up redirects file DC-6228
ankur-arch Nov 13, 2025
aa486e9
fix: clean-up unnecessary file
ankur-arch Nov 13, 2025
59db9d6
fix: remove unknown word
ankur-arch Nov 13, 2025
ae1d885
fix: add links
ankur-arch Nov 13, 2025
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 @@ -19,7 +19,7 @@ In this Quickstart guide, you'll learn how to get started from scratch with Pris

:::note

If you want to use Prisma Postgres with another ORM or database library (like Drizzle ORM, TypeORM or Kysely), you can follow the instructions [here](/postgres/introduction/getting-started#connect-via-any-database-library--tool).
If you want to use Prisma Postgres with another ORM or database library (like Drizzle ORM, TypeORM or Kysely), you can follow the instructions [here](/postgres/getting-started#connect-via-any-database-library--tool).

:::

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
---
title: 'Prisma Postgres'
metaTitle: 'Quickstart: Prisma ORM with Prisma Postgres (5 min)'
metaDescription: 'Create a new TypeScript project from scratch by connecting Prisma ORM to Prisma Postgres and generating a Prisma Client for database access.'
sidebar_custom_props: { badge: '5 min' }
---

import Prerequisites from '../../_components/_prerequisites.mdx'
import CreateProject from '../../_components/_create-project.mdx'
import ExploreData from '../../_components/_explore-data.mdx'
import NextSteps from '../../_components/_next-steps.mdx'

[Prisma Postgres](/postgres) is a fully managed PostgreSQL database that scales to zero and integrates smoothly with both Prisma ORM and Prisma Studio. In this guide, you will learn how to set up a new TypeScript project from scratch, connect it to Prisma Postgres using Prisma ORM, and generate a Prisma Client for easy, type-safe access to your database.

## Prerequisites

<Prerequisites />

## 1. Create a new project

<CreateProject />

## 2. Install required dependencies

Install the packages needed for this quickstart:

```terminal
npm install prisma @types/node --save-dev
npm install @prisma/client @prisma/adapter-pg dotenv
```

Here's what each package does:

- **`prisma`** - The Prisma CLI for running commands like `prisma init`, `prisma migrate`, and `prisma generate`
- **`@prisma/client`** - The Prisma Client library for querying your database
- **`@prisma/adapter-pg`** - The [`node-postgres` driver adapter](/orm/overview/databases/postgresql#using-the-node-postgres-driver) that connects Prisma Client to your database
- **`dotenv`** - Loads environment variables from your `.env` file

## 3. Configure ESM Support

Update `tsconfig.json` for ESM compatibility:

```json file=tsconfig.json
{
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "node",
"target": "ES2023",
"strict": true,
"esModuleInterop": true,
"ignoreDeprecations": "6.0"
}
}
```

Update `package.json` to enable ESM:

```json file=package.json
{
// add-start
"type": "module",
// add-end
}
```

## 4. Initialize Prisma ORM and create a Prisma Postgres database

You can now invoke the Prisma CLI by prefixing it with `npx`:

```terminal
npx prisma
```

Next, set up your Prisma ORM project by creating your [Prisma Schema](/orm/prisma-schema) file with the following command:

```terminal
npx prisma init --db --output ../generated/prisma
```

:::info

You'll need to answer a few questions while setting up your Prisma Postgres database. Select the region closest to your location and a memorable name for your database.

:::

This command does a few things:

- Creates a `prisma/` directory with a `schema.prisma` file containing your database connection and schema models
- Creates a new Prisma Postgres database (when using `--db` flag)
- Creates a `.env` file in the root directory for environment variables
- Generates the Prisma Client in the `generated/prisma/` directory
- Creates a `prisma.config.ts` file for Prisma configuration

The generated `prisma.config.ts` file looks like this:

```typescript file=prisma.config.ts
import { defineConfig, env } from 'prisma/config'

export default defineConfig({
schema: 'prisma/schema.prisma',
migrations: {
path: 'prisma/migrations',
},
datasource: {
url: env('DATABASE_URL'),
},
})
```

Add `dotenv` to `prisma.config.ts` so that Prisma can load environment variables from your `.env` file:

```typescript file=prisma.config.ts
// add-start
import 'dotenv/config'
// add-end
import { defineConfig, env } from 'prisma/config'

export default defineConfig({
schema: 'prisma/schema.prisma',
migrations: {
path: 'prisma/migrations',
},
datasource: {
url: env('DATABASE_URL'),
},
})
```

The generated schema uses [the ESM-first `prisma-client` generator](/orm/prisma-schema/overview/generators#prisma-client) with a custom output path:

```prisma file=prisma/schema.prisma
generator client {
provider = "prisma-client"
output = "../generated/prisma"
}

datasource db {
provider = "postgresql"
}
```

## 5. Define your data model

Open `prisma/schema.prisma` and add the following models:

```prisma file=prisma/schema.prisma
generator client {
provider = "prisma-client"
output = "../generated/prisma"
}

datasource db {
provider = "postgresql"
}

//add-start
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}

model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
}
//add-end
```

## 6. Create and apply your first migration

Create your first migration to set up the database tables:

```terminal
npx prisma migrate dev --name init
```

This command:
- Creates the database tables based on your schema
- Generates the Prisma Client in the `generated/prisma` directory

## 7. Instantiate Prisma Client

Now that you have all the dependencies installed, you can instantiate Prisma Client. You need to pass an instance of Prisma ORM's driver adapter to the `PrismaClient` constructor:

```typescript file=lib/prisma.ts
import "dotenv/config";
import { PrismaPg } from '@prisma/adapter-pg'
import { PrismaClient } from '../generated/prisma/client'

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

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

export { prisma }
```

## 8. Write your first query

Create a `script.ts` file to test your setup:

```typescript file=script.ts
import { prisma } from './lib/prisma'

async function main() {
// Create a new user with a post
const user = await prisma.user.create({
data: {
name: 'Alice',
email: 'alice@prisma.io',
posts: {
create: {
title: 'Hello World',
content: 'This is my first post!',
published: true,
},
},
},
include: {
posts: true,
},
})
console.log('Created user:', user)

// Fetch all users with their posts
const allUsers = await prisma.user.findMany({
include: {
posts: true,
},
})
console.log('All users:', JSON.stringify(allUsers, null, 2))
}

main()
.then(async () => {
await prisma.$disconnect()
})
.catch(async (e) => {
console.error(e)
await prisma.$disconnect()
process.exit(1)
})
```

Run the script:

```terminal
npx tsx script.ts
```

You should see the created user and all users printed to the console!

## 9. Explore your data with Prisma Studio

<ExploreData />

## Next steps

<NextSteps />

## More info

- [Prisma Postgres documentation](/postgres)
- [Prisma Config reference](/orm/reference/prisma-config-reference)
- [Database connection management](/orm/prisma-client/setup-and-configuration/databases-connections)
Loading
Loading