Config API
Prisma ORM v7Complete reference for prisma.config.ts configuration options
The Prisma Config file (prisma.config.ts) configures the Prisma CLI using TypeScript. It's automatically created when you run prisma init.
You can define your config using either the defineConfig helper or TypeScript's satisfies operator:
-
Using the
defineConfighelper:import "dotenv/config"; import { defineConfig, env } from "prisma/config"; export default defineConfig({ schema: "prisma/schema.prisma", migrations: { path: "prisma/migrations", seed: "tsx prisma/seed.ts", }, datasource: { url: env("DATABASE_URL"), }, }); -
Using TypeScript's
satisfiesoperator with thePrismaConfigtype:import "dotenv/config"; import type { PrismaConfig } from "prisma"; import { env } from "prisma/config"; export default { schema: "prisma/schema.prisma", migrations: { path: "prisma/migrations", seed: "tsx prisma/seed.ts", }, datasource: { url: env("DATABASE_URL"), }, } satisfies PrismaConfig;
Configuration interface
Here is a simplified version of the PrismaConfig type:
export declare type PrismaConfig = {
// Whether features with an unstable API are enabled.
experimental: {
externalTables: boolean;
};
// The path to the schema file, or path to a folder that shall be recursively searched for *.prisma files.
schema?: string;
// Configuration for Prisma migrations.
migrations?: {
path: string;
seed: string;
initShadowDb: string;
};
// Configuration for the database view entities.
views?: {
path: string;
};
// Configuration for the `typedSql` preview feature.
typedSql?: {
path: string;
};
// Database connection configuration
datasource?: {
url: string;
shadowDatabaseUrl?: string;
};
};Supported file extensions
Prisma Config files can be named as prisma.config.* or .config/prisma.* with the extensions js, ts, mjs, cjs, mts, or cts. Other extensions are supported to ensure compatibility with different TypeScript compiler settings.
- Use
prisma.config.tsfor small TypeScript projects. - Use
.config/prisma.tsfor larger TypeScript projects with multiple configuration files (following the.configdirectory proposal).
Options reference
schema
Configures how Prisma ORM locates and loads your schema file(s). Can be a file or folder path. Relative paths are resolved relative to the prisma.config.ts file location. See Multi-file Prisma schema for more info about schema location options.
| Property | Type | Required | Default |
|---|---|---|---|
schema | string | No | ./prisma/schema.prisma and ./schema.prisma |
tables.external and enums.external
These options declare tables and enums in your database that are managed externally (not by Prisma Migrate). You can still query them with Prisma Client, but they will be ignored by migrations.
| Property | Type | Required | Default |
|---|---|---|---|
tables.external | string[] | No | [] |
enums.external | string[] | No | [] |
Example:
import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
},
datasource: {
url: env("DATABASE_URL"),
},
experimental: {
externalTables: true,
},
tables: {
external: ["public.users"],
},
enums: {
external: ["public.role"],
},
});Learn more about the externalTables feature here.
migrations.path
The path to the directory where Prisma should store migration files, and look for them.
| Property | Type | Required | Default |
|---|---|---|---|
migrations.path | string | No | none |
migrations.seed
Defines the command to run when executing npx prisma db seed. Seeding is only triggered explicitly via this command.
| Property | Type | Required | Default |
|---|---|---|---|
migrations.seed | string | No | none |
Example:
import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
seed: "tsx db/seed.ts",
},
datasource: {
url: env("DATABASE_URL"),
},
});migrations.initShadowDb
This option allows you to define SQL statements that Prisma runs on the shadow database before creating migrations. It is useful when working with external managed tables, as Prisma needs to know about the structure of these tables to correctly generate migrations.
| Property | Type | Required | Default |
|---|---|---|---|
migrations.initShadowDb | string | No | none |
Example:
import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
initShadowDb: `
CREATE TABLE public.users (id SERIAL PRIMARY KEY);
`,
},
datasource: {
url: env("DATABASE_URL"),
},
experimental: {
externalTables: true,
},
tables: {
external: ["public.users"],
},
});Learn more about the externalTables feature here.
views.path
The path to the directory where Prisma should look for the SQL view definitions.
| Property | Type | Required | Default |
|---|---|---|---|
views.path | string | No | none |
typedSql.path
The path to the directory where Prisma should look for the SQL files used for generating typings via typedSql.
| Property | Type | Required | Default |
|---|---|---|---|
typedSql.path | string | No | none |
experimental
Enables specific experimental features in the Prisma CLI.
| Property | Type | Required | Default |
|---|---|---|---|
externalTables | boolean | No | false |
Example:
import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
},
datasource: {
url: env("DATABASE_URL"),
},
experimental: {
externalTables: true,
},
});If you use the externalTables feature without enabling the experimental flag, Prisma will throw an error:
Failed to load config file "~" as a TypeScript/JavaScript module. Error: Error: The `externalTables` configuration requires `experimental.externalTables` to be set to `true`.datasource.url
Connection URL including authentication info. Uses the syntax provided by the database.
| Property | Type | Required | Default |
|---|---|---|---|
datasource.url | string | Yes | '' |
Example:
import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
},
datasource: {
url: env("DATABASE_URL"),
},
});Prisma CLI commands that connect to the database stop with one of these errors when datasource.url is empty, not set or not valid:
Connection url is empty: the value is an empty string, for example when the config readsprocess.env.DATABASE_URL ?? ''and the variable is not set.The datasource.url property is required in your Prisma config file when using prisma migrate deploy.: the value isundefined, for example when the config readsprocess.env["DATABASE_URL"], the form thatprisma initwrites on Node.js, and the variable is not set. The message ends with the command you ran,prisma migrate deployin this example.P1013: The provided database string is invalid: the value is not a valid connection URL, for example a placeholder, a missing scheme such aspostgresql://, or a password with a#that is not percent-encoded. Check it against the connection URL format.
Prisma ORM 7 does not load .env files by itself. A variable that is only in .env is not set until the file is loaded, for example with import "dotenv/config" on the first line of the Prisma config file (prisma.config.ts, or prisma7.config.ts in projects created with Prisma ORM 7.10 or later). See Using environment variables.
If Connection url is empty, the datasource.url property is required error or P1013 appears and your project already has a database, keep its DATABASE_URL and fix how it is loaded or encoded. If your project uses PostgreSQL and has no database yet, npx create-db creates a temporary Prisma Postgres database without an account and prints its connection string and a claim URL. The temporary database is deleted after 24 hours unless you claim it with a free Prisma account.
npx create-db@latestRerun the Prisma CLI command that failed, with the printed connection string set on the command in the variable your Prisma config reads (DATABASE_URL in the example config above) and without editing .env:
DATABASE_URL="<connection string printed by create-db>" npx prisma migrate devIn PowerShell, set the variable for the current session first, with $env:DATABASE_URL = "<connection string printed by create-db>", then run npx prisma migrate dev. The value lasts until you close that window, and .env stays as it is.
To keep using the temporary database, put the string in .env under the variable your Prisma config reads (DATABASE_URL in the example config above) if the file has no line for it yet, or if the line is empty or still a placeholder. A line that names a database your project uses stays as it is.
datasource.shadowDatabaseUrl
Connection URL to the shadow database used by Prisma Migrate. Allows you to use a cloud-hosted database as the shadow database.
| Property | Type | Required | Default |
|---|---|---|---|
datasource.shadowDatabaseUrl | string | No | '' |
datasource.directUrl (Removed)
The datasource.directUrl property has been removed in Prisma ORM v7 in favor of the url property.
For Prisma ORM v6.19 and earlier
Connection URL for direct connection to the database.
If you use a connection pooler URL in the url argument (for example, pgBouncer), Prisma CLI commands that require a direct connection to the database use the URL in the directUrl argument.
The directUrl property is supported by Prisma Studio from version 5.1.0 upwards. The directUrl property is not needed when using Prisma Postgres database.
| Property | Type | Required | Default |
|---|---|---|---|
datasource.directUrl | string | No | '' |
adapter (Removed)
The adapter property has been removed in Prisma ORM v7. Migrations for driver adapters work automatically without additional configuration in prisma.config.ts as of Prisma ORM v7.
For Prisma ORM v6.19 and earlier
A function that returns a Prisma driver adapter instance which is used by the Prisma CLI to run migrations. The function should return a Promise that resolves to a valid Prisma driver adapter.
| Property | Type | Required | Default |
|---|---|---|---|
adapter | () => Promise<SqlMigrationAwareDriverAdapterFactory> | No | none |
Example using the Prisma ORM D1 driver adapter:
import path from "node:path";
import type { PrismaConfig } from "prisma";
import { PrismaD1 } from "@prisma/adapter-d1";
export default {
experimental: {
adapter: true,
},
engine: "js",
schema: path.join("prisma", "schema.prisma"),
async adapter() {
return new PrismaD1({
CLOUDFLARE_D1_TOKEN: process.env.CLOUDFLARE_D1_TOKEN,
CLOUDFLARE_ACCOUNT_ID: process.env.CLOUDFLARE_ACCOUNT_ID,
CLOUDFLARE_DATABASE_ID: process.env.CLOUDFLARE_DATABASE_ID,
});
},
} satisfies PrismaConfig;As of Prisma ORM v6.11.0, the D1 adapter has been renamed from PrismaD1HTTP to PrismaD1.
engine (Removed)
The engine property has been removed in Prisma ORM v7.
For Prisma ORM v6.19 and earlier
Configure the schema engine your project should use.
| Property | Type | Required | Default |
|---|---|---|---|
engine | classic or js | No | classic |
By default it is set to use the classic engine, which requires that datasource be set in your prisma.config.ts.
import "dotenv/config";
import path from "node:path";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
engine: "classic",
datasource: {
url: env("DATABASE_URL"),
},
schema: path.join("prisma", "schema.prisma"),
});studio (Removed)
The studio property has been removed in Prisma ORM v7. To run Prisma Studio, use:
bunx prisma studio --config ./prisma.config.tsPrisma Studio now uses the connection configuration from the datasource property automatically. See the Prisma Studio documentation for more details.
For Prisma ORM v6.19 and earlier
Configures how Prisma Studio connects to your database. See sub-options below for details.
| Property | Type | Required | Default |
|---|---|---|---|
studio | object | No | none |
studio.adapter (Removed)
A function that returns a Prisma driver adapter instance. The function receives an env parameter containing environment variables and should return a Promise that resolves to a valid Prisma driver adapter.
| Property | Type | Required | Default |
|---|---|---|---|
studio.adapter | (env: Env) => Promise<SqlMigrationAwareDriverAdapterFactory> | No | none |
Example using the Prisma ORM LibSQL driver adapter:
import type { PrismaConfig } from "prisma";
export default {
experimental: {
studio: true,
},
engine: "js",
studio: {
adapter: async (env: Env) => {
const { PrismaLibSQL } = await import("@prisma/adapter-libsql");
const { createClient } = await import("@libsql/client");
const libsql = createClient({
url: env.DOTENV_PRISMA_STUDIO_LIBSQL_DATABASE_URL,
});
return new PrismaLibSQL(libsql);
},
},
} satisfies PrismaConfig;Common patterns
Setting up your project
To get started with Prisma Config, create a prisma.config.ts file in your project root. You can use either of these approaches:
Using defineConfig:
import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
},
datasource: {
url: env("DATABASE_URL"),
},
});Using TypeScript types:
import "dotenv/config";
import type { PrismaConfig } from "prisma";
import { env } from "prisma/config";
export default {
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
},
datasource: {
url: env("DATABASE_URL"),
},
} satisfies PrismaConfig;Using environment variables
Environment variables from .env files need to be loaded explicitly. The prisma init command generates a config that includes import 'dotenv/config' by default.
Using dotenv (Recommended for Prisma ORM v7)
- Install the
dotenvpackage:
bun add dotenv- Import
dotenv/configat the top of yourprisma.config.tsfile:
import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
seed: "tsx prisma/seed.ts",
},
datasource: {
url: env("DATABASE_URL"),
},
});Using Node.js v20+ or tsx with --env-file flag
With Node.js 20.6.0 or later, you can pass an --env-file flag to node or tsx to load environment variables. tsx passes the flag on to Node.js, so it needs the same Node.js version:
tsx --env-file=".env" src/index.ts
tsx watch --env-file=".env" --env-file=".local.env" src/index.ts
tsx --env-file=".env" ./prisma/seed.tsThese commands load .env for the script they start, such as your application or a seed script. For Prisma CLI commands, load the file in the Prisma config file, as shown above.
Using Bun
For Bun, .env files are automatically loaded without additional configuration. The import 'dotenv/config' line that prisma init generates is not needed when using Bun and can be safely removed from your prisma.config.ts file.
When running prisma init with Bun, use the --bun flag (for example, bunx --bun prisma init) to ensure Prisma uses the Bun runtime instead of falling back to Node.js.
Type-safe environment variables
Use the env() helper function to provide type-safe access to environment variables:
import "dotenv/config";
import { defineConfig, env } from "prisma/config";
type Env = {
DATABASE_URL: string;
};
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
},
datasource: {
url: env<Env>("DATABASE_URL"),
},
});Handling optional environment variables
The env() helper function from prisma/config throws an error if the specified environment variable is not defined. This is important to understand because:
- Every Prisma CLI command loads the
prisma.config.tsfile - Only some commands actually need the
datasource.urlvalue (e.g.,prisma db *,prisma migrate *,prisma generate --sql) - Commands like
prisma generatedon't need a database URL, but will still fail ifenv()throws an error when loading the config file
For example, if you run prisma generate without DATABASE_URL set, and your config uses env('DATABASE_URL'), you'll see:
Failed to load config file "/path/to/project" as a TypeScript/JavaScript module. Error: PrismaConfigEnvError: Cannot resolve environment variable: DATABASE_URL.Prisma ORM 7.0 and 7.1 print Missing required environment variable: DATABASE_URL instead.
Solution: If your environment variable isn't guaranteed to exist (e.g., in CI/CD pipelines where you only run prisma generate for type-checking), don't use the env() helper. Instead, access the environment variable directly:
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!, // Or use: process.env.DATABASE_URL ?? '' to provide a fallback value
},
});Use the env() helper when you want to enforce that an environment variable exists. Use process.env directly when the variable may be optional depending on the command being run.
A command that connects to the database needs the variable to be set, with either form of the config. Set DATABASE_URL in .env or on the command itself:
DATABASE_URL="your_database_connection_string_here" npx prisma migrate deployThe variable applies to that command only, and no file is changed. If your project has no database yet, see datasource.url.
Using multi-file schemas
If you want to split your Prisma schema into multiple files, you need to specify the path to your Prisma schema folder via the schema property:
import path from "node:path";
import type { PrismaConfig } from "prisma";
export default {
schema: path.join("prisma", "schema"),
} satisfies PrismaConfig;In that case, your migrations directory must be located next to the .prisma file that defines the datasource block.
For example, assuming schema.prisma defines the datasource, here's how how need to place the migrations folder:
# `migrations` and `schema.prisma` are on the same level
.
├── migrations
├── models
│ ├── posts.prisma
│ └── users.prisma
└── schema.prismaPath resolution
Prisma CLI commands such as prisma validate or prisma migrate use prisma.config.ts (or .config/prisma.ts) to locate your Prisma schema and other resources.
Key rules:
- Paths defined in the config file (e.g.,
schema,migrations) are always resolved relative to the location of the config file, not where you run the CLI command from. - The CLI must first find the config file itself, which depends on how Prisma is installed and the package manager used.
Behavior with pnpm prisma
When Prisma is installed locally and run via pnpm prisma, the config file is detected automatically whether you run the command from the project root or a subdirectory.
Example project tree:
.
├── node_modules
├── package.json
├── prisma-custom
│ └── schema.prisma
├── prisma.config.ts
└── srcExample run from the project root:
pnpm prisma validate
# → Loaded Prisma config from ./prisma.config.ts
# → Prisma schema loaded from prisma-custom/schema.prismaExample run from a subdirectory:
cd src
pnpm prisma validate
# → Still finds prisma.config.ts and resolves schema correctlyBehavior with npx prisma or bunx prisma
When running via npx prisma or bunx prisma, the CLI only detects the config file if the command is run from the project root (where package.json declares Prisma).
When running prisma init with Bun, use bunx --bun prisma init to ensure Prisma runs with the Bun runtime.
Example run from the project root:
bunx prisma validate
# → Works as expectedRun from a subdirectory (fails):
cd src
bunx prisma validate
# → Error: Could not find Prisma Schema...To fix this, you can use the --config flag:
bunx prisma validate --config ../prisma.config.tsGlobal Prisma installations
If Prisma is installed globally (npm i -g prisma), it may not find your prisma.config.ts or prisma/config module by default.
To avoid issues:
- Prefer local Prisma installations in your project.
- Or use
prisma/configlocally and pass--configto point to your config file.
Monorepos
- If Prisma is installed in the workspace root,
pnpm prismawill detect the config file from subdirectories. - If Prisma is installed in a subpackage (e.g.,
./packages/db), run commands from that package directory or deeper.
Custom config location
You can specify a custom location for your config file when running Prisma CLI commands:
prisma validate --config ./path/to/myconfig.tsLoading environment variables
To load environment variables, install the dotenv package and add import 'dotenv/config' at the top of your prisma.config.ts file.
To load environment variables in your Prisma application, use the prisma.config.ts file along with the env helper from prisma/config.
-
Install the
dotenvpackage:bun add dotenv -
Create a
.envfile in your project root (if it doesn't exist) and add your database connection string:DATABASE_URL="your_database_connection_string_here" -
Ensure your
prisma.config.tsfile importsdotenv/configat the top:prisma.config.ts import "dotenv/config"; import { defineConfig, env } from "prisma/config"; export default defineConfig({ schema: "prisma/schema.prisma", migrations: { path: "prisma/migrations", seed: "tsx prisma/seed.ts", }, datasource: { url: env("DATABASE_URL"), }, });
Updated
