Skip to main content
Prisma Documentation Docs
current

Search documentation

Type to search this documentation.

On this pageOverview

Local Postgres

Set up and use Prisma Postgres for local development

Prisma Postgres is a hosted database for your staging and production environments. For local iteration and isolated testing, you can run a local Prisma Postgres instance (powered by PGlite) with the prisma dev command. This page explains how to launch and manage a local Prisma Postgres database.

Setting up local development for Prisma Postgres

Section titled “Setting up local development for Prisma Postgres”

Follow these steps to set up local Prisma Postgres for development.

Node.js v20 or later is required for local Prisma Postgres.

Navigate into your project and start the local Prisma Postgres server using the following command:

bun
bunx prisma@7 dev
pnpm
pnpm dlx prisma@7 dev
yarn
yarn dlx prisma@7 dev
npm
npx prisma@7 dev

This starts a local Prisma Postgres server that you can connect to using Prisma ORM or another tool. The output of the command looks like this:

text
$ npx prisma@7 dev
Loaded Prisma config from prisma.config.ts.

✔  Your local Prisma Postgres server default is now running 👍
                                                                                                                                                                                               
🔌 To connect with Prisma ORM use the following connection strings:

   DATABASE_URL="postgres://postgres:postgres@localhost:51214/template1?sslmode=disable&connection_limit=10&connect_timeout=0&max_idle_connection_lifetime=0&pool_timeout=0&socket_timeout=0"
   SHADOW_DATABASE_URL="postgres://postgres:postgres@localhost:51215/template1?sslmode=disable&connection_limit=10&connect_timeout=0&max_idle_connection_lifetime=0&pool_timeout=0&socket_timeout=0"

🐘 You can also use the DATABASE_URL with the pg or postgres.js JavaScript drivers as well as your favorite DB gui.

   For the best experience, set the maximum number of connections to 10, connect timeout to 0 and
   idle timeout to the smallest positive value supported.

🌊 Prisma Streams is available at:

   PRISMA_STREAM_URL="http://127.0.0.1:51216/v1/stream/prisma-wal"

┌─────────────────┐
│ Press q to quit │
└─────────────────┘

You may:

  • q to quit

If you want to connect via Prisma ORM, hit h on your keyboard and copy both DATABASE_URL and SHADOW_DATABASE_URL into your .env file. Prisma ORM connects through the first one. Prisma Migrate uses the second one as the shadow database when it checks for drift. Without it, the first prisma migrate dev run works but later runs fail with a relation "..." already exists error.

.env
DATABASE_URL="postgres://postgres:postgres@localhost:51214/template1?sslmode=disable&connection_limit=10&connect_timeout=0&max_idle_connection_lifetime=0&pool_timeout=0&socket_timeout=0"
SHADOW_DATABASE_URL="postgres://postgres:postgres@localhost:51215/template1?sslmode=disable&connection_limit=10&connect_timeout=0&max_idle_connection_lifetime=0&pool_timeout=0&socket_timeout=0"

Then point the datasource block of your Prisma config at both variables:

prisma.config.ts
import "dotenv/config";
import { defineConfig } from "prisma/config";

export default defineConfig({
  schema: "prisma/schema.prisma",
  migrations: {
    path: "prisma/migrations",
  },
  datasource: {
    url: process.env["DATABASE_URL"],
    shadowDatabaseUrl: process.env["SHADOW_DATABASE_URL"],
  },
});

Keep the local Prisma Postgres server running in the background while you work on your application.

Alternatively, you can run the server in detached mode to free up your terminal:

bun
bunx prisma@7 dev --detach
pnpm
pnpm dlx prisma@7 dev --detach
yarn
yarn dlx prisma@7 dev --detach
npm
npx prisma@7 dev --detach

This starts the server in the background. Use prisma dev ls to see running servers and prisma dev stop to stop them.

Then in a separate terminal tab, run the prisma migrate dev command to create the database and run the migrations:

bun
bunx prisma@7 migrate dev
pnpm
pnpm dlx prisma@7 migrate dev
yarn
yarn dlx prisma@7 migrate dev
npm
npx prisma@7 migrate dev
```bash
bunx prisma@7 db execute --file create-migrations-table.sql
```
```bash
pnpm dlx prisma@7 db execute --file create-migrations-table.sql
```
```bash
yarn dlx prisma@7 db execute --file create-migrations-table.sql
```
```bash
npx prisma@7 db execute --file create-migrations-table.sql
```

prisma db push does not read that table, so it works as an alternative if you do not need migration files yet.

This will create the database and run the migrations.

If you have a seeder script to seed the database, you should also run it in this step.

Start your application's development server. You can now perform queries against the local Prisma Postgres instance using Prisma ORM.

To transition to production, you only need to update the database URL in the .env file with a Prisma Postgres connection url without additional application logic changes.

Using different local Prisma Postgres instances

Section titled “Using different local Prisma Postgres instances”

You can target a specific, local Prisma Postgres instance via the --name (-n) option of the prisma dev command, for example:

bun
bunx prisma@7 dev --name="mydb1"
pnpm
pnpm dlx prisma@7 dev --name="mydb1"
yarn
yarn dlx prisma@7 dev --name="mydb1"
npm
npx prisma@7 dev --name="mydb1"

Whenever you pass the --name="mydb1" to prisma dev, the command will return the same connection string pointing to a local instance called mydb1. This creates a named instance that you can later manage using the instance management commands.

Starting existing Prisma Postgres instances in the background

Section titled “Starting existing Prisma Postgres instances in the background”

You can start existing Prisma Postgres instances in the background using:

bun
bunx prisma@7 dev start <glob>
pnpm
pnpm dlx prisma@7 dev start <glob>
yarn
yarn dlx prisma@7 dev start <glob>
npm
npx prisma@7 dev start <glob>

<glob> is a placeholder for a glob pattern to specify which local Prisma Postgres instances should be started, for example:

bun
bunx prisma@7 dev start mydb # starts a DB called `mydb` in the background (only if it already exists)
pnpm
pnpm dlx prisma@7 dev start mydb # starts a DB called `mydb` in the background (only if it already exists)
yarn
yarn dlx prisma@7 dev start mydb # starts a DB called `mydb` in the background (only if it already exists)
npm
npx prisma@7 dev start mydb # starts a DB called `mydb` in the background (only if it already exists)

To start all databases that begin with mydb (e.g. mydb-dev and mydb-prod), you can use a glob:

bun
bunx prisma@7 dev start mydb* # starts all existing DBs starting with `mydb`
pnpm
pnpm dlx prisma@7 dev start mydb* # starts all existing DBs starting with `mydb`
yarn
yarn dlx prisma@7 dev start mydb* # starts all existing DBs starting with `mydb`
npm
npx prisma@7 dev start mydb* # starts all existing DBs starting with `mydb`

Use this command to start instances in the background without the VS Code extension.

You can view all your local Prisma Postgres instances using:

bun
bunx prisma@7 dev ls
pnpm
pnpm dlx prisma@7 dev ls
yarn
yarn dlx prisma@7 dev ls
npm
npx prisma@7 dev ls

This command lists all available instances on your system, showing their current status and configuration.

You can stop a running Prisma Postgres instance with this command:

bun
bunx prisma@7 dev stop <glob>
pnpm
pnpm dlx prisma@7 dev stop <glob>
yarn
yarn dlx prisma@7 dev stop <glob>
npm
npx prisma@7 dev stop <glob>

<glob> is a placeholder for a glob pattern to specify which local Prisma Postgres instances should be stopped, for example:

bun
bunx prisma@7 dev stop mydb # stops a DB called `mydb`
pnpm
pnpm dlx prisma@7 dev stop mydb # stops a DB called `mydb`
yarn
yarn dlx prisma@7 dev stop mydb # stops a DB called `mydb`
npm
npx prisma@7 dev stop mydb # stops a DB called `mydb`

To stop all databases that begin with mydb (e.g. mydb-dev and mydb-prod), you can use a glob:

bun
bunx prisma@7 dev stop mydb* # stops all DBs starting with `mydb`
pnpm
pnpm dlx prisma@7 dev stop mydb* # stops all DBs starting with `mydb`
yarn
yarn dlx prisma@7 dev stop mydb* # stops all DBs starting with `mydb`
npm
npx prisma@7 dev stop mydb* # stops all DBs starting with `mydb`

Prisma Postgres saves the information and data from your local Prisma Postgres instances on your file system. To remove any trace from a database that's not in use any more, you can run the following command:

bun
bunx prisma@7 dev rm <glob>
pnpm
pnpm dlx prisma@7 dev rm <glob>
yarn
yarn dlx prisma@7 dev rm <glob>
npm
npx prisma@7 dev rm <glob>

<glob> is a placeholder for a glob pattern to specify which local Prisma Postgres instances should be removed, for example:

bun
bunx prisma@7 dev rm mydb # removes a DB called `mydb`
pnpm
pnpm dlx prisma@7 dev rm mydb # removes a DB called `mydb`
yarn
yarn dlx prisma@7 dev rm mydb # removes a DB called `mydb`
npm
npx prisma@7 dev rm mydb # removes a DB called `mydb`

To remove all databases that begin with mydb (e.g. mydb-dev and mydb-prod), you can use a glob:

bun
bunx prisma@7 dev rm mydb* # removes all DBs starting with `mydb`
pnpm
pnpm dlx prisma@7 dev rm mydb* # removes all DBs starting with `mydb`
yarn
yarn dlx prisma@7 dev rm mydb* # removes all DBs starting with `mydb`
npm
npx prisma@7 dev rm mydb* # removes all DBs starting with `mydb`

You can use the --force flag to stop any running servers before removing them:

bun
bunx prisma@7 dev rm mydb --force
pnpm
pnpm dlx prisma@7 dev rm mydb --force
yarn
yarn dlx prisma@7 dev rm mydb --force
npm
npx prisma@7 dev rm mydb --force

Without --force, the command will fail if any server is still running.

Local Prisma Postgres supports direct PostgreSQL connections, allowing you to connect to it via any tool.

In order to connect to your local Prisma Postgres instance, use the postgres:// connection string that's returned by prisma dev.

Managing local Prisma Postgres instances via the Prisma VS Code extension

Section titled “Managing local Prisma Postgres instances via the Prisma VS Code extension”

The Prisma VS Code extension has a dedicated UI managing Prisma Postgres instances.

To use it, install the VS Code extension and find the Prisma logo in the activity bar of your VS Code editor. It enables the following workflows:

  • creating and deleting databases
  • starting and stopping the server for a particular database
  • "push to cloud": move a database from local to remote

Manage local Prisma Postgres programmatically

Section titled “Manage local Prisma Postgres programmatically”

You can start and stop a local Prisma Postgres server from Node.js without invoking the CLI. This uses undocumented, unstable APIs from @prisma/dev and may change without notice. Use it at your own risk. It's especially useful for integration tests that need an ephemeral local database per test or suite.

This is a complete runnable example that will print [{abba: 1}] when run:

TypeScript
import { Client } from "pg";
import { startPrismaDevServer } from "@prisma/dev";

async function startLocalPrisma(name: string) {
  return await startPrismaDevServer({
    name, // required, use a unique name if running tests in parallel
    port: 51213, // optional, defaults to 51213
    databasePort: 51214, // optional, defaults to 51214
    shadowDatabasePort: 51215, // optional, defaults to 51215
    persistenceMode: "stateless", // optional, defaults to 'stateless'. Use 'stateful' to persist data between runs
  });
}

// Usage in tests
const server = await startLocalPrisma(`my-tests-${Date.now()}`);
try {
  const client = new Client({
    connectionString: server.database.connectionString,
  });
  await client.connect();

  const res = await client.query(`SELECT 1 as "abba"`);
  console.log(res.rows);

  client.end();
} finally {
  await server.close!();
}

The startPrismaDevServer() function accepts the following options:

Argument Required Description Default
name ❌ Unique identifier for the local Prisma Postgres instance. Use distinct names if running multiple servers in parallel. 'default'
port ❌ Port for the Prisma engine server. Throws an error if the port is already in use. 51213
databasePort ❌ Port for the embedded PostgreSQL database. Used for all Prisma ORM connections. 51214
shadowDatabasePort ❌ Port for the shadow database used during migrations. 51215
persistenceMode ❌ Defines how data is persisted:
• 'stateless': no data is retained between runs
• 'stateful': data persists locally
'stateless'
debug ❌ Whether to enable debug logging. false
dryRun ❌ Whether to run the server in dry run mode. false
databaseConnectTimeoutMillis ❌ Connection timeout in milliseconds for pending database connections. Starts ticking for every new client that attempts to connect. When exceeded, the pending client connection is evicted and closed. Use with caution, as it may lead to unexpected behavior. Best used with a pool client that retries connections. 60000 (1 minute)
databaseIdleTimeoutMillis ❌ Idle timeout in milliseconds for active database connections. Re-starts ticking after each message received on the active connection. When exceeded, the active client connection is closed, and a pending connection is promoted to active. Use with caution, as it may lead to unexpected disconnections. Best used with a pool client that can handle disconnections gracefully. Set it if you suffer from client hanging indefinitely. Not applied by default
shadowDatabaseConnectTimeoutMillis ❌ Connection timeout in milliseconds for pending shadow database connections. Defaults to databaseConnectTimeoutMillis
shadowDatabaseIdleTimeoutMillis ❌ Idle timeout in milliseconds for active shadow database connections. Defaults to databaseIdleTimeoutMillis

Notes:

  • Allocate unique ports and name values when running tests concurrently.
  • Use server.database.connectionString to connect with Postgres clients or ORMs.
  • Use this pattern for tests that require a local database.

Start says "already running", but ls says not_running

Section titled “Start says "already running", but ls says not_running”

Starting an instance may fail with A Prisma Dev server with the name <name> is already running, while npx prisma@7 dev ls reports that same instance as not_running. This happens when a previous run of the server crashed or was force-killed and left its lock file behind: the start command saw only the held lock, while ls checked whether the server was actually alive.

Recent versions of prisma detect that the previous owner is dead, take over the leftover lock, and start normally. Projects whose installed prisma predates the fix can still hit this. Tools that embed the local server (such as Prisma Composer) run the @prisma/dev version installed in your project, so the fix reaches you by updating the project's prisma dependency.

To recover on an affected version, remove the stuck instance and start it again:

bun
bunx prisma@7 dev rm <name>
pnpm
pnpm dlx prisma@7 dev rm <name>
yarn
yarn dlx prisma@7 dev rm <name>
npm
npx prisma@7 dev rm <name>

Note that rm deletes the instance's stored data along with the leftover lock.

The local Prisma Postgres database server accepts one connection at a time. Additional connection attempts queue until the active connection closes. This constraint is sufficient for most local development and testing scenarios.

The local Prisma Postgres server doesn't use HTTPS. We advise against self-hosting it.

Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu