Prisma ORM 8 is here.Read the docs

A database on your machine

Get a PostgreSQL or MongoDB database for a Prisma ORM project without signing up for anything, and look at the rows your app writes.

Every Prisma ORM setup page asks you for a connection string and puts it in DATABASE_URL. If you have no database yet, this page gives you one. For PostgreSQL, pick one of three options: a temporary hosted database with nothing to install, PostgreSQL in Docker on your machine, or, for an app made with npm create prisma, the database that app can start on its own. MongoDB has its own section. The last section shows how to look at the rows once your app has written some.

A temporary database, with nothing to install

create-db creates a Prisma Postgres database, Prisma's hosted PostgreSQL, with no account and no install. It is the fastest option, and the only one on this page that runs on Prisma's servers instead of your machine.

bunx create-db@latest

The output has a Connection String line, which is the value for DATABASE_URL, and a Claim Your Database link. Put the connection string in DATABASE_URL. The setup pages that start from npm create prisma export it in the shell, and the other setup pages put it in .env:

export DATABASE_URL="<the connection string create-db printed>"

The database is deleted after 24 hours unless you claim it: open the claim link and sign in to a Prisma account, and you keep the database for free. To have it deleted sooner, set the lifetime when you create it, for example npx create-db@latest --ttl 1h. create-db lists the other options.

PostgreSQL in Docker

When you want the database on your own machine, run PostgreSQL in a container. You need Docker installed, for example Docker Desktop; docker compose version tells you whether you have it. Prisma ORM 8 needs PostgreSQL 15 or newer, and the file below runs 17.

Create this file in your project root:

docker-compose.postgres.yml
services:
  postgres:
    image: postgres:17
    restart: always
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=prisma
    ports:
      - "127.0.0.1:5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres -d postgres"]
      interval: 5s
      timeout: 2s
      retries: 20
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:

Start it:

docker compose -f docker-compose.postgres.yml up -d

The command returns as soon as the container is running, and the database takes a few more seconds to accept connections. docker compose -f docker-compose.postgres.yml ps shows healthy in the status column once it does.

The container answers on port 5432, on this machine only, with the user postgres, the password prisma, and a database named postgres, so the connection string is:

.env
DATABASE_URL="postgresql://postgres:prisma@localhost:5432/postgres"

The setup pages that start from npm create prisma read the variable from the shell, not from .env, so on those pages export it in the shell instead:

export DATABASE_URL="postgresql://postgres:prisma@localhost:5432/postgres"

Unlike the hosted examples on the setup pages, this string has no sslmode=require at the end. Leave it off: the connection never leaves your machine.

If port 5432 is already taken, for example by a PostgreSQL you installed earlier, change the middle 5432 in 127.0.0.1:5432:5432 under ports to another number such as 5433, and use that number in the connection string.

The data is kept in the postgres_data volume, so stopping and starting the container keeps your rows:

docker compose -f docker-compose.postgres.yml stop
docker compose -f docker-compose.postgres.yml up -d

To start over with an empty database, remove the container and the volume together:

docker compose -f docker-compose.postgres.yml down -v

A PostgreSQL you installed in another way, such as with Homebrew or Postgres.app, works the same. Its connection string uses the user, password, port, and database name of that install.

The database that comes with a new app

If npm create prisma made your app, its package.json has a dev:composer script, and the script gives you a database with nothing more to install. An app made another way does not have the script, so use Docker instead.

bun run dev:composer

The script starts a PostgreSQL database on your machine, creates your tables in it by applying the migration the scaffold wrote, and starts your app. Your app needs no DATABASE_URL for this, the database keeps its rows from one run to the next, and Ctrl-C stops both. The script uses Prisma Composer, which npm create prisma installed; you need not edit anything for this to work.

Commands such as npx prisma db migrate, which applies a migration to the database, still need DATABASE_URL even while the script runs. Get the connection string from a second terminal in the project directory:

npx prisma dev url my-app.database

my-app.database is the database's address: your app's name, then the name module.ts gives the database. npm create prisma names the app after the project directory, so replace my-app with yours. The command prints the connection string, and this sets DATABASE_URL to it in the same shell:

export DATABASE_URL="$(npx prisma dev url my-app.database)"

dev url is in prisma since 8.0.0-rc.24, and npx prisma --version shows which version your project has.

To empty the database, stop the script, run npm run build, and then run npx prisma dev --fresh. That deletes the database and its rows and starts the app as the script does. Stop it with Ctrl-C when you are done, and use npm run dev:composer next time. Local development covers running the app in the background.

MongoDB

Prisma ORM 8 needs MongoDB 8.0 or newer. To run it in Docker, create this file in your project root:

docker-compose.mongodb.yml
services:
  mongodb:
    image: mongo:8
    restart: always
    ports:
      - "127.0.0.1:27017:27017"
    volumes:
      - mongodb_data:/data/db

volumes:
  mongodb_data:

Start it:

docker compose -f docker-compose.mongodb.yml up -d

The server answers on port 27017, on this machine only, with no user or password. The database name at the end of the connection string, mydb here, can be any name you like, and you need not create it first.

.env
DATABASE_URL="mongodb://localhost:27017/mydb?directConnection=true"

The setup pages that start from npm create prisma read the variables from the shell, not from .env, and the MongoDB scaffold uses two names: the prisma commands read MONGODB_URL and the app reads DATABASE_URL. On those pages export both instead of writing .env:

export MONGODB_URL="mongodb://localhost:27017/mydb?directConnection=true"
export DATABASE_URL="$MONGODB_URL"

This is a standalone server, not a replica set. A replica set is needed only for transactions and change streams, and the setup pages use neither; if your own app uses transactions, use the Atlas option below instead. The string the MongoDB setup pages show has replicaSet=rs0 in it, which only connects to a replica set. For this server use the string above, without it.

A free MongoDB Atlas cluster is the hosted alternative. It is a replica set already, so use the connection string Atlas shows you as it is.

The same stop, start, and down -v commands as for PostgreSQL apply, with docker-compose.mongodb.yml as the file.

Look at your rows

PostgreSQL

Every command here takes the connection string of whichever database you set up above, so export it in the shell first. For the Docker setup that is the string from .env, for the create-db database the string it printed, and for the database the dev:composer script started, the one dev url prints:

export DATABASE_URL="postgresql://postgres:prisma@localhost:5432/postgres"

psql is the PostgreSQL command line. With it installed on your machine:

psql "$DATABASE_URL"

With the Docker setup you need not install it, because the container has it:

docker compose -f docker-compose.postgres.yml exec postgres psql -U postgres -d postgres

Inside psql, \dt lists your tables. A table is named after its model, or after the model's @@map if it has one, so the starter contract's User model is the "User" table: \d "User" shows its columns, and SELECT * FROM "User"; prints its rows. The quotes matter, because PostgreSQL lowercases a name written without them.

Prisma Studio shows your tables in the browser, and the migrations Prisma ORM has applied. Run it from the directory above your project and pass the connection string:

cd .. && npx prisma@prev studio --url "$DATABASE_URL"

Studio works with Prisma ORM 8 databases, but it still ships with the Prisma ORM 7 command-line tool, which prisma@prev installs. That tool stops with an error inside a Prisma ORM 8 project, so run it from the directory above and cd back afterwards. Studio prints the address it serves on, usually http://localhost:5555. Studio with Prisma ORM explains the Migrations view, which shows what each applied migration changed.

A database editor such as pgAdmin, TablePlus, DBeaver, or DataGrip asks for the parts of the connection string. For the Docker setup they are the host localhost, the port 5432, the user postgres, the password prisma, the database postgres, and SSL off. Viewing data shows the connection dialogs of several editors.

MongoDB

Prisma Studio does not support MongoDB. mongosh is the MongoDB shell, and the container has it:

docker compose -f docker-compose.mongodb.yml exec mongodb mongosh mydb

Inside mongosh, show collections lists your collections and db.users.find() prints the documents in the users collection. A collection is named after its model, so a Comment model is the Comment collection, unless the model has @@map: the starter contract maps User to users and Post to posts. MongoDB Compass is the graphical alternative; connect it to mongodb://localhost:27017.

Next steps

Edit on GitHub

Last updated on

On this page