apps/docs/content/docs/(index)/prisma-orm/quickstart/postgresql.mdx
PostgreSQL is a powerful, open-source relational database. In this guide, you will learn how to set up a new TypeScript project from scratch, connect it to PostgreSQL using Prisma ORM, and generate a Prisma Client for easy, type-safe access to your database.
You also need:
:::tip[Need a PostgreSQL database?]
If you don't already have a PostgreSQL database, follow the quickstart to set up a production-ready Prisma Postgres database with Prisma ORM in a new project.
:::
mkdir hello-prisma
cd hello-prisma
Initialize a TypeScript project:
npm init -y
npm install typescript tsx @types/node --save-dev
npx tsc --init
Install the packages needed for this quickstart:
npm install prisma @types/pg --save-dev
npm install @prisma/client @prisma/adapter-pg 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 that connects Prisma Client to your databasepg - The node-postgres database driver@types/pg - TypeScript type definitions for node-postgresdotenv - Loads environment variables from your .env fileUpdate tsconfig.json for ESM compatibility:
{
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "bundler",
"target": "ES2023",
"strict": true,
"esModuleInterop": true,
"ignoreDeprecations": "6.0"
}
}
Update package.json to enable ESM:
{
"type": "module" // [!code ++]
}
You can now invoke the Prisma CLI by prefixing it with npx:
npx prisma
Next, set up your Prisma ORM project by creating your Prisma Schema file with the following command:
npx prisma init --datasource-provider postgresql --output ../generated/prisma
This command does a few things:
prisma/ directory with a schema.prisma file containing your database connection and schema models.env file in the root directory for environment variablesprisma.config.ts file for Prisma configurationThe generated prisma.config.ts file looks like this:
import "dotenv/config";
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 with a custom output path:
generator client {
provider = "prisma-client"
output = "../generated/prisma"
}
datasource db {
provider = "postgresql"
}
Update your .env file with your PostgreSQL connection string:
DATABASE_URL="postgresql://username:password@localhost:5432/mydb?schema=public"
Replace the placeholders with your actual database credentials:
username: Your PostgreSQL usernamepassword: Your PostgreSQL passwordlocalhost:5432: Your PostgreSQL host and portmydb: Your database nameOpen prisma/schema.prisma and add the following models:
generator client {
provider = "prisma-client"
output = "../generated/prisma"
}
datasource db {
provider = "postgresql"
}
model User { // [!code ++]
id Int @id @default(autoincrement()) // [!code ++]
email String @unique // [!code ++]
name String? // [!code ++]
posts Post[] // [!code ++]
} // [!code ++]
model Post { // [!code ++]
id Int @id @default(autoincrement()) // [!code ++]
title String // [!code ++]
content String? // [!code ++]
published Boolean @default(false) // [!code ++]
author User @relation(fields: [authorId], references: [id]) // [!code ++]
authorId Int // [!code ++]
} // [!code ++]
Create your first migration to set up the database tables:
npx prisma migrate dev --name init
This command creates the database tables based on your schema.
Now run the following command to generate the Prisma Client:
npx prisma generate
Now that you have all the dependencies installed, you can instantiate Prisma Client. You need to pass an instance of the Prisma ORM driver adapter adapter to the PrismaClient constructor:
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 };
Create a script.ts file to test your setup:
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: "[email protected]",
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:
npx tsx script.ts
You should see the created user and all users printed to the console!
Prisma Studio is a visual editor for your database. Launch it with:
npx prisma studio
You've successfully set up Prisma ORM. Here's what you can explore next: