Installing Prisma
Objectives
By the end of this lesson, you should be able to:
- Install Prisma in a Node.js project
- Run
prisma initand explain what it generates - Point Prisma at a real PostgreSQL database
๐ก Why this matters: Every Prisma feature in this course, models, migrations, queries, relationships, is built on top of the project structure this lesson sets up.
โ ๏ธ A note on verification: the Prisma CLI (
prisma init,prisma generate,prisma migrate) needs to download a query engine binary the first time it runs, and this course’s own tooling runs in a sandboxed environment with no route to that download. So unlike every other lesson in this course, the commands and output below are not this course’s own live output, they’re Prisma’s standard, current, documented behavior, stable across recent Prisma versions. Run them yourself following this lesson, they’ll behave exactly as shown, on your own machine, with normal internet access.
Project File Structure
prisma-intro/
โโโ package.json
โโโ .env
โโโ prisma/
โ โโโ schema.prisma
โโโ node_modules/
Installing Prisma
mkdir prisma-intro && cd prisma-intro
npm init -y
npm install prisma --save-dev
npm install @prisma/client
prisma is the CLI, used at development time (migrate, generate, and so on). @prisma/client is the library the application code actually imports and calls at runtime. Both are needed, they do different jobs.
Running prisma init
npx prisma init --datasource-provider postgresql
โ Your Prisma schema was created at prisma/schema.prisma
You can now open it in your favorite editor.
Next steps:
1. Set the DATABASE_URL in the .env file to point to your existing database.
2. Run prisma db pull to turn your database schema into a Prisma schema.
3. Run prisma generate to generate the Prisma Client.
More information in our documentation:
https://pris.ly/d/getting-started
This creates two files:
// prisma/schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
# .env
DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public"
schema.prisma is where every model in this course is defined, starting next module. .env holds the connection string, exactly the same pattern used with the raw pg driver in Module 1.
Pointing It at a Real Database
Replace the placeholder DATABASE_URL with the real connection string for the school database created back in Module 1:
# .env
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/school"
Prisma reads this same DATABASE_URL for every command from here on, migrations, queries, everything.
Try It
- Install Prisma in a new project, and run
prisma init --datasource-provider postgresql. - Confirm
prisma/schema.prismaand.envwere created, matching what’s shown above. - Update
DATABASE_URLin.envto point at a real PostgreSQL database on your machine. - Open
schema.prismaand identify the two blocks it starts with,generatoranddatasource, and explain what each one is for.
Recap
prisma(dev dependency) is the CLI,@prisma/client(regular dependency) is the runtime library, both are installed separately.prisma initcreatesprisma/schema.prisma(the schema file) and.env(holdingDATABASE_URL).- The schema file starts with a
generatorblock (how the client gets generated) and adatasourceblock (which database, and how to connect to it).
Next lesson: the Prisma schema file itself, and what the generator and datasource blocks actually configure.