CLI command reference

Overview

This document describes the Prisma CLI commands, arguments, and options.

Synopsis

The prisma command can be called from command line once installed. When called without arguments, it will display its command usage and help document:

$prisma
Hide CLI output
Prisma is a modern DB toolkit to query, migrate and model your database (https://prisma.io)
Usage
$ prisma [command]
Commands
init Setup Prisma for your app
introspect Get the datamodel of your database
generate Generate artifacts (e.g. Prisma Client)
studio Open Prisma Studio
format Format your schema
db Manage your database schema and lifecycle (preview)
Flags
--experimental Show and run Experimental Prisma commands
--preview Run Preview Prisma commands
Examples
Setup a new Prisma project
$ prisma init
Introspect an existing database
$ prisma introspect
Generate artifacts (e.g. Prisma Client)
$ prisma generate
Browse your data
$ prisma studio
Push the Prisma schema state to the database
$ prisma db push --preview

You can get additional help on any of the prisma commands by adding the --help flag after the command.

Commands

version (-v)

The version command outputs information about your current @prisma/cli version, platform, and engine binaries.

Options

The version command recognizes the following options to modify its behavior:

OptionRequiredDescription
--jsonNoOutputs version information in JSON format.

Examples

Output version information
$prisma version
Hide CLI output
Environment variables loaded from ./prisma/.env @prisma/cli : 2.0.0-beta.3 Current platform
: windows Query Engine : query-engine 2fb8f444d9cdf7c0beee7b041194b42d7a9ce1e6 (at
C:\Users\veroh\AppData\Roaming\npm\node_modules\@prisma\cli\query-engine-windows.exe) Migration
Engine : migration-engine-cli 2fb8f444d9cdf7c0beee7b041194b42d7a9ce1e6 (at
C:\Users\veroh\AppData\Roaming\npm\node_modules\@prisma\cli\migration-engine-windows.exe)
Introspection Engine : introspection-core 2fb8f444d9cdf7c0beee7b041194b42d7a9ce1e6 (at
C:\Users\veroh\AppData\Roaming\npm\node_modules\@prisma\cli\introspection-engine-windows.exe)
Output version information (-v)
$prisma -v
Hide CLI output
Environment variables loaded from ./prisma/.env @prisma/cli : 2.0.0-beta.3 Current platform
: windows Query Engine : query-engine 2fb8f444d9cdf7c0beee7b041194b42d7a9ce1e6 (at
C:\Users\veroh\AppData\Roaming\npm\node_modules\@prisma\cli\query-engine-windows.exe) Migration
Engine : migration-engine-cli 2fb8f444d9cdf7c0beee7b041194b42d7a9ce1e6 (at
C:\Users\veroh\AppData\Roaming\npm\node_modules\@prisma\cli\migration-engine-windows.exe)
Introspection Engine : introspection-core 2fb8f444d9cdf7c0beee7b041194b42d7a9ce1e6 (at
C:\Users\veroh\AppData\Roaming\npm\node_modules\@prisma\cli\introspection-engine-windows.exe)
Output version information as JSON
$prisma version --json
Hide CLI output
Environment variables loaded from ./prisma/.env
{
"@prisma/cli": "2.0.0-beta.3",
"current-platform": "windows",
"query-engine": "query-engine 2fb8f444d9cdf7c0beee7b041194b42d7a9ce1e6 (at C:\\Users\\veroh\\AppData\\Roaming\\npm\\node_modules\\@prisma\\cli\\query-engine-windows.exe)",
"migration-engine": "migration-engine-cli 2fb8f444d9cdf7c0beee7b041194b42d7a9ce1e6 (at C:\\Users\\veroh\\AppData\\Roaming\\npm\\node_modules\\@prisma\\cli\\migration-engine-windows.exe)", "introspection-engine": "introspection-core 2fb8f444d9cdf7c0beee7b041194b42d7a9ce1e6 (at C:\\Users\\veroh\\AppData\\Roaming\\npm\\node_modules\\@prisma\\cli\\introspection-engine-windows.exe)"
}

init

Bootstraps a fresh Prisma project within the current directory.

The init command does not interpret any existing files. Instead, it creates a prisma directory containing a bare-bones schema.prisma file within your current directory.

Examples

Run prisma init
$prisma init
Hide CLI output
✔ Your Prisma schema was created at prisma/schema.prisma.
You can now open it in your favorite editor.
Next steps:
1. Set the provider of the datasource block in schema.prisma to match your database: postgresql, mysql or sqlite.
2. Set the DATABASE_URL in the .env file to point to your existing database. If your database has no tables yet, read https://pris.ly/d/getting-started.
3. Run prisma introspect to turn your database schema into a Prisma data model.
4. Run prisma generate to install Prisma Client. You can then start querying your database.
More information in our documentation:
https://pris.ly/d/getting-started

The command output contains helpful information on how to use the generated files and begin using Prisma with your project.

Generated Assets

prisma/schema.prisma

A minimal schema.prisma file to define your schema in:

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}

prisma/.env

A file to define environment variables for your project:

# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables
# Prisma supports the native connection string format for PostgreSQL, MySQL and SQLite.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
DATABASE_URL="postgresql://johndoe:johndoe@localhost:5432/mydb?schema=public"

generate

The generate command generates assets like Prisma Client based on the generator and data model blocks defined in your prisma/schema.prisma file.

The generate command is most often used to generate Prisma Client with the prisma-client-js generator. This does three things:

  1. Searches the current directory and parent directories to find the applicable npm project. It will create a package.json file in the current directory if it cannot find one.
  2. Installs the @prisma/client into the npm project if it is not already present.
  3. Inspects the current directory to find a Prisma schema file to process. It will then generate a customized Prisma Client for your project.

Prerequisites

To use the generate command, you must add a generator definition in your schema.prisma file. The prisma-client-js generator, used to generate Prisma Client, can be added by including the following in your schema.prisma file:

generator client {
provider = "prisma-client-js"
}

Options

The generate command recognizes the following options to modify its behavior:

OptionRequiredDescriptionDefault
--schemaNoSpecifies the path to the desired schema.prisma file to be processed instead of the default path. Both absolute and relative paths are supported../schema.prisma, ./prisma/schema.prisma
--watchNoThe generate command will continue to watch the schema.prisma file and re-generate Prisma Client on file changes.

Examples

Generate Prisma Client using the default schema.prisma path
$prisma generate
Hide CLI output
✔ Generated Prisma Client to ./node_modules/@prisma/client in 61ms
You can now start using Prisma Client in your code:
import { PrismaClient } from '@prisma/client'
// or const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
Explore the full API: http://pris.ly/d/client
Generate Prisma Client using a non-default schema.prisma path
$prisma generate --schema=./alternative/schema.prisma
Continue watching the schema.prisma file for changes to automatically re-generate Prisma Client
$prisma generate --watch
Hide CLI output
Watching... /home/prismauser/prisma/prisma-play/prisma/schema.prisma
✔ Generated Prisma Client to ./node_modules/@prisma/client in 45ms

Generated Assets

The prisma-client-js generator creates a customized client for working with your database within the ./node_modules/@prisma/client directory.

introspect

The introspect command connects to your database and adds Prisma models to your Prisma schema that reflect the current database schema.

To run the introspect command, a schema.prisma file with a valid datasource must be available.

Warning: The command will overwrite the current schema.prisma file with the new schema. Any manual changes or customization will be lost. Be sure to back up your current schema.prisma file before running introspect if it contains important modifications.

Prerequisites

Before using the introspect command, you must define a valid datasource within your schema.prisma file.

For example, the following datasource defines a SQLite database file within the current directory:

datasource db {
provider = "sqlite"
url = "file:my-database.db"
}

Options

The introspect command recognizes the following options to modify its behavior:

OptionRequiredDescriptionDefault
--forceNoForce overwrite of manual changes made to schema. The generated schema will be based on the introspected schema only.
--schemaNoSpecifies the path to the desired schema.prisma file to be processed instead of the default path. Both absolute and relative paths are supported../schema.prisma, ./prisma/schema.prisma
--printNoPrints the created schema.prisma to the screen instead of writing it to the filesystem.

Examples

Analyze the database and write its schema to the schema.prisma file
$prisma introspect
Hide CLI output
Introspecting based on datasource defined in schema.prisma …
✔ Wrote Prisma data model into schema.prisma in 38ms
Run prisma generate to generate Prisma Client.
Specify an alternative schema.prisma file to read and write to
$prisma introspect --schema=./alternative/schema.prisma
Hide CLI output
Introspecting based on datasource defined in alternative/schema.prisma …
✔ Wrote Prisma data model into alternative/schema.prisma in 60ms
Run prisma generate to generate Prisma Client.
Display the generated schema.prisma file instead of writing it to the filesystem
$prisma introspect --print
Hide CLI output
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlite"
url = "file:./hello-prisma.db"
}
model User {
email String @unique
name String?
user_id Int @default(autoincrement()) @id
post Post[]
profile Profile[]
}
model Post {
content String?
post_id Int @default(autoincrement()) @id
title String
author_id User?
}
model Profile {
bio String?
profile_id Int @default(autoincrement()) @id
user_id User
}

db

db push

Note: This feature is currently in Preview. Features in Preview may still have some issues, and are not recommended for use in production environments. To access commands in Preview, you must opt-in via the --preview-feature flag.

The db push command pushes the state of your Prisma schema file to the database without using migrations. This command is a good choice when you do not need to version schema changes, such as during prototyping and local development. db push works as follows:

  • db push introspects the database, and infers and executes the changes required to make your database schema reflect the state of your Prisma schema.

  • By default, after changes have been applied to the database schema, generators are triggered (for example, Prisma Client). You do not need to manually invoke prisma generate.

  • If db push anticipates that the changes could result in data loss, it will:

    • Throw an error
    • Require the --force option if you still want to make the changes
  • db push does not interact with or rely on migrations. The migrations table will no be updated, and no migration files will be generated. If migrations are detected, db push will prompt for confirmation or require an explicit option (--ignore-migrations).

db push works well if:

  • You want to quickly prototype and iterate on schema design locally without the need to deploy these changes to other environments such as other developers, or staging and production environments.
  • You are prioritizing reaching a desired end-state and not the changes or steps executed to reach that end-state. There is no way to Preview the changes that will be made to the database.
  • You do not need control over how schema changes impact data. There is no way to orchestrate schema and data migrations - if db push anticipates that changes will result in data loss, it will require specific confirmation via a --force option. You can either accept data loss by specifying the option or stop the process, but there is no way to customize the changes.

db push is not recommended if:

  • You want to replicate your schema changes in other environments where there could be important data you cannot afford to lose. You can use db push for prototyping, but you should use migrations to commit the schema changes and apply these in your other environments.
  • You want fine-grained control over how the schema changes are executed - for example, renaming a column instead of dropping it and creating a new one.
  • You want to keep track of changes made to the database schema over time. db push does not create any artifacts that allow you to keep track of these changes.
  • You want the schema changes to be reversible. You can use db push again to revert to the original state, but this might result in data loss and there will be no mechanism for you to orchestrate these changes.

db push and Migrate (Experimental)

If you are already using migrations with Prisma Migrate, db push will still work. However, be aware of the following side effects of using both Prisma Migrate and db push:

Prisma Migrate introspects the database to anticipate the changes needed to reach the end-state of the migration and Preview these changes as DDL statements in README.md files.

If you use db push to change the database schema using the Prisma Schema file, and subsequently use prisma migrate save --experimental to generate the corresponding migration:

  1. Migrate will still work regardless if it is executed on top of the changes from db push or not, but
  2. the DDL statements in README.md will not reflect the steps carried out in other environments where db push was not used - the statements can be different or entirely missing.

Because this behavior could be confusing to some, if db push detect migration files, it will require confirmation to proceed or supplying an —-ignore-migrations option.

Prerequisites

Before using the db push command, you must define a valid datasource within your schema.prisma file.

For example, the following datasource defines a SQLite database file within the current directory:

datasource db {
provider = "sqlite"
url = "file:my-database.db"
}

Arguments

ArgumentRequiredDescriptionDefault
--schemaNoSpecifies the path to the desired schema.prisma file to be processed instead of the default path. Both absolute and relative paths are supported../schema.prisma
./prisma/schema.prisma

Options

OptionsRequiredDescription
--preview-featureYesEnables use of Preview feature commands
--skip-generateNoSkip generation of artifacts such as Prisma Client
--ignore-migrationsNoIf there migration files, db push will have some side effects. Use this option to ignore these side effects and proceed.
--forceNoIgnore data loss warnings. This option is required if as a result of making the schema changes, data may be lost.
--help / --hNoDisplays the help message

Examples

Push the schema:

$npx prisma db push --preview-feature

Push the schema, accepting data loss:

$npx prisma db push --preview-feature --force

Push the schema with a custom schema location:

$npx prisma db push --preview-feature --schema=/tmp/schema.prisma

Migrations (Experimental)

Warning: The migrate command is still considered Experimental. As such, there are no guarantees about API stability or production-readiness. Access to this command is provided for evaluation and experimentation. To access Experimental commands, you must add the --experimental flag.

The migrate command creates and manages database migrations. It can be used to create, apply, and rollback database schema updates in a controlled manner.

The migrate command includes a number of subcommands to specify the desired action.

migrate save

Saves a migration that defines the steps necessary to update your current schema.

Prerequisites

Before using the migrate save command, you must define a valid datasource within your schema.prisma file.

For example, the following datasource defines a SQLite database file within the current directory:

datasource db {
provider = "sqlite"
url = "file:my-database.db"
}

Options

The migrate save command recognizes the following options to modify its behavior:

OptionRequiredDescriptionDefault
--experimentalYesEnables use of Experimental commands.
-n, --nameNoThe name of the migration. If not provided, migrate save will prompt you for a name.Timestamp 20200618145356
-c, --create-dbNoCreate the database if it does not exist.
-p, --previewNoPreview the migration that would be created without writing any changes to the filesystem.
--schemaNoSpecifies the path to the desired schema.prisma file to be processed instead of the default path. Both absolute and relative paths are supported../schema.prisma, ./prisma/schema.prisma

Generated Assets

The migrate save command generates the following directories and files as necessary:

  • migrations: A directory within the current project to store migrations. This directory will be created if it does not exist.
  • migrations/migrate.lock: A lock file created specifying the current migration applied to the database. This file will be created if it does not exist.
  • migrations/<migration>: A directory for a specific migration. The migration name is derived from the timestamp when it was created followed by a hyphen and the migration name provided by the user.
  • migrations/<migration>/README.md: A human-readable description of the migration including metadata like when the migration was created and by who, a list of the actual migration changes and a diff of the changes that are made to the schema.prisma file.
  • migrations/<migration>/schema.prisma: The schema that will be created if the migration is applied to the project.
  • migrations/<migration>/steps.json: An alternative representation of the migration steps that will be applied.

Examples

Create a new migration
$prisma migrate save --experimental

The command will prompt you for a name for the migration since one was not provided on the command line. After creating the migration, the contents of the generated schema.prisma file are displayed.

Create a migration with a specific name
$prisma migrate save --name "First migration" --experimental
Create the database if it does not already exist
$prisma migrate save --create-db --experimental
Preview the migration that would be created by running the migrate save command
$prisma migrate save --preview --experimental

migrate up

Migrate the database up to a specific state.

Prerequisites

Before using the migrate up command, you must define a valid datasource within your schema.prisma file.

For example, the following datasource defines a SQLite database file within the current directory:

datasource db {
provider = "sqlite"
url = "file:my-database.db"
}

Arguments

The point to migrate the database up to can be defined in any of the following three ways:

ArgumentRequiredDescriptionDefault
incrementNoSpecifies the number of forward migrations to apply.latest
nameNoSpecifies where to migrate to using the name of the final migration to apply.latest
timestampNoSpecifies where to migrate to using the timestamp of the final migration to apply.latest

Options

Additionally, the following options modify the behavior of the migrate up command:

OptionRequiredDescriptionDefault
--experimentalYesEnables use of Experimental commands
-c, --create-dbNoCreate the database if it does not exist.
-p, --previewNoPreview the migration that would be created without writing any changes to the filesystem.
--schemaNoSpecifies the path to the desired schema.prisma file to be processed instead of the default path. Both absolute and relative paths are supported../schema.prisma, ./prisma/schema.prisma
--auto-approveNoSkip interactive approval before migrating.

Examples

Migrate the database up to the latest available migration
$prisma migrate up --experimental
Apply the next two migrations to the database
$prisma migrate up 2 --experimental
Apply all migrations necessary up to and including a migration by name
$prisma migrate up "First migration" --experimental
Apply all migrations necessary up to and including a migration by timestamp
$prisma migrate up 20200223181448 --experimental
Create the database if it does not already exist before applying the migrations
$prisma migrate up --create-db --experimental
Preview the migration that would be applied by running the migrate up command
$prisma migrate up --preview --experimental

migrate down

Migrate the database down to a specific state.

Prerequisites

Before using the migrate down command, you must define a valid datasource within your schema.prisma file.

For example, the following datasource defines a SQLite database file within the current directory:

datasource db {
provider = "sqlite"
url = "file:my-database.db"
}

Arguments

The point to migrate back to can be defined in any of the following three ways:

ArgumentRequiredDescriptionDefault
decrementNoSpecifies the number of backwards migrations to apply.1
nameNoSpecifies where to migrate back to using the name of the final migration to apply.
timestampNoSpecifies where to migrate back to using the timestamp of the final migration to apply.

Options

Additionally, the following options modify the behavior of the migrate down command:

OptionRequiredDescriptionDefault
--experimentalYesEnables use of Experimental commands
-p, --previewNoPreview the migration that would be created without writing any changes to the filesystem.
--schemaNoSpecifies the path to the desired schema.prisma file to be processed instead of the default path. Both absolute and relative paths are supported../schema.prisma, ./prisma/schema.prisma

Examples

Migrate the database backwards by a single migration
$prisma migrate down --experimental
Migrate the database backwards by two migrations
$prisma migrate down 2 --experimental
Migrate backwards through all migrations up to and including a migration by name
$prisma migrate down "First migration" --experimental
Migrate backwards through all migrations up to and including a migration by timestamp
$prisma migrate down 20200223181448 --experimental
Preview the migration that would be applied by running the migrate down command
$prisma migrate down --preview --experimental

Studio

studio

The studio command allows you to interact with and manage your data interactively. It does this by starting a local web server with a web app configured with your project's data schema and records.

Prerequisites

Before using the studio command, you must define a valid datasource within your schema.prisma file.

For example, the following datasource defines a SQLite database file within the current directory:

datasource db {
provider = "sqlite"
url = "file:my-database.db"
}

Options

The studio command recognizes the following options:

OptionRequiredDescriptionDefault
-p, --portNoThe port number to start Studio on.5555

Examples

Start Studio on the default port and open a new browser tab to it

$prisma studio

Start Studio on a different port and open a new browser tab to it

$prisma studio --port 7777
Edit this page on GitHub