Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 36 ยท Getting Started

The Prisma CLI and Prisma Studio

A tour of the Prisma CLI: init, generate, validate, format, db push, migrate, db pull, db seed and studio, with a guide for which command to use when.

The prisma command-line tool is how you talk to Prisma during development. You do not need to memorize every flag, but you should know what each command is for, because choosing the wrong one (for example db push where you wanted migrate dev) is a classic way to confuse a team database. This lesson is a map.

All commands are run with npx prisma <command> (or through your package scripts).

The commands at a glance

Command What it does Touches the database?
prisma init Creates schema.prisma, prisma.config.ts and .env No
prisma validate Checks the schema for errors No
prisma format Formats the schema file No
prisma generate Writes the TypeScript client from the schema No
prisma db push Makes the database match the schema, with no migration file Yes
prisma migrate dev Creates a migration from schema changes and applies it (development) Yes
prisma migrate deploy Applies pending migrations (production, CI) Yes
prisma migrate status Shows which migrations are applied Yes
prisma migrate reset Drops the database, re-applies all migrations, runs the seed Yes (destructive)
prisma db pull Reads the existing database and writes models into the schema (introspection) Yes (read only)
prisma db seed Runs your seed script Yes
prisma studio Opens a web UI to browse and edit data Yes
prisma version Prints versions (useful in bug reports) No

Which command do I need?

Prototyping db push
Real project migrate dev
Production migrate deploy
Existing database db pull

Schema edited? Also run generate.

1 / 2

generate: the command you run most

Terminal
npx prisma generate

It reads the schema and rewrites the client folder you configured with output. It never touches the database, so it is fast and safe, and it is the answer to "my new field does not exist in TypeScript". In CI and Docker builds run it after installing dependencies and before compiling.

db push versus migrate dev

Terminal
npx prisma db push
npx prisma migrate dev --name add_posts

db push compares the schema with the database and changes the database to match, without keeping any history. It is great for quick experiments and throwaway databases, but there is no migration file to review, share or replay. migrate dev writes a SQL file into prisma/migrations/, applies it and records it, so teammates and production can run the exact same change. The rule of thumb: prototype with db push, ship with migrate. Chapter 5 covers this in depth.

db pull: start from an existing database

Terminal
npx prisma db pull

If a database already exists (legacy project, a database made by another team) this command inspects tables, columns, indexes and foreign keys and writes matching models into schema.prisma. You then tidy the names with @map and @@map (next chapter) and generate the client. This is called introspection.

db seed: sample data

Add a seed command in prisma.config.ts:

TypeScript
migrations: {
  path: "prisma/migrations",
  seed: "tsx prisma/seed.ts",
},

and write prisma/seed.ts using the client (prisma.user.createMany(...)). Then npx prisma db seed fills a fresh database with data you need in development. migrate reset runs it automatically after rebuilding the database.

prisma migrate reset deletes all data in the database it is pointed at. Check DATABASE_URL before running it, and never run it against production.

Prisma Studio

Terminal
npx prisma studio

Studio starts a local web app (by default on port 5555) that lists your models as tables. You can filter, sort, add, edit and delete records, and follow relations between tables. It reads the schema, so it knows your types and enum values. It is a development tool: do not expose it on a public server without authentication.

Typical uses: checking what your code really wrote, fixing a broken row by hand, creating a first admin user, or showing a colleague what is in the database without writing SQL.

A tiny command picker

To make the decision rules concrete, here is a stand-in that advises which command to run based on a situation. The logic is the advice in this lesson, nothing more.

Node.js Stand-in: which Prisma command?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Package scripts worth adding

A few scripts make the team's habits consistent:

JSON
{
  "scripts": {
    "db:generate": "prisma generate",
    "db:migrate": "prisma migrate dev",
    "db:deploy": "prisma migrate deploy",
    "db:seed": "prisma db seed",
    "db:studio": "prisma studio",
    "postinstall": "prisma generate"
  }
}

The postinstall hook regenerates the client after npm install, which saves "module not found" surprises on a fresh clone.

Reading the output of validate

When the schema has a mistake, the CLI prints the file, line number and a hint, for instance that a relation field is missing its opposite side. Fix the first error first; later ones are often consequences. This stand-in formats an error the way you would read it.

Node.js Stand-in: reading a validation message

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command only rewrites the TypeScript client and never touches the database?

    generate reads the schema and writes client code. The others connect to a database.
  2. 2. When is db push the right choice?

    db push syncs the database to the schema without a migration file, so it suits prototypes and throwaway databases.
  3. 3. Which command turns an existing database into Prisma models?

    db pull introspects the database and writes models into schema.prisma.
  4. 4. Why is prisma migrate reset dangerous?

    reset rebuilds the database from the migrations and then seeds it, so every row is lost. Use it only on development databases.

Summary

  • generate builds the client; validate and format check and tidy the schema.
  • Prototype with db push, use migrate dev while developing and migrate deploy in production.
  • db pull introspects an existing database; db seed loads sample data.
  • Prisma Studio is a handy local data browser, not a production admin panel.

Next: Models and Fields in the Prisma Schema