Skip to main content

Relational databases

Learn how to create a new Node.js or TypeScript project from scratch by connecting Prisma ORM to your database and generating a Prisma Client for database access. The following tutorial introduces you to the Prisma CLI, Prisma Client, and Prisma Migrate.

Prerequisites

In order to successfully complete this guide, you need:

See System requirements for exact version requirements.

Make sure you have your database connection URL at hand. If you don't have a database server running and just want to explore Prisma ORM, check out the Quickstart.

Create project setup

As a first step, create a project directory and navigate into it:

mkdir hello-prisma
cd hello-prisma

Next, initialize a TypeScript project and add the Prisma CLI as a development dependency to it:

npm init -y
npm install prisma typescript tsx @types/node --save-dev

This creates a package.json with an initial setup for your TypeScript app.

Next, initialize TypeScript:

npx tsc --init
info

See installation instructions to learn how to install Prisma using a different package manager.

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:

  • Creates a new directory called prisma that contains a file called schema.prisma, which contains the Prisma Schema with your database connection variable and schema models.
  • Sets the datasource to PostgreSQL and the output to a custom location, respectively.
  • Creates the .env file in the root directory of the project, which is used for defining environment variables (such as your database connection)
Using version control?

If you're using version control, like git, we recommend you add a line to your .gitignore in order to exclude the generated client from your application. In this example, we want to exclude the generated/prisma directory.

.gitignore
generated/prisma/

Note that the default schema created by prisma init uses PostgreSQL as the provider. If you didn't specify a provider with the datasource-provider option, you need to edit the datasource block to use the postgresql provider instead:

prisma/schema.prisma
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}