In Prisma 7, what reads the datasource.url in prisma.config.ts, and where does Prisma Client get its connection string at runtime?
answer
- two consumers of a URL
- CLI versus application
- defineConfig from prisma/config
- the adapter carries its own string
basics
~20 sIn Prisma 7, datasource.url in prisma.config.ts is read by the Prisma CLI for migrate, db and introspection commands. Prisma Client never reads it; at runtime it connects through the driver adapter you pass, such as PrismaPg with its connectionString.
solid answer
~40 sPrisma 7 splits the connection in two. `prisma.config.ts`, written with `defineConfig` from `prisma/config`, holds `datasource.url` and optionally `shadowDatabaseUrl`; CLI commands such as `prisma migrate deploy` and `prisma db push` use it, and the schema's `datasource` block keeps only the provider. The application builds `new PrismaClient({ adapter })`, and the adapter, for example `new PrismaPg({ connectionString: process.env.DATABASE_URL })`, carries the runtime connection string and its pool. Prisma 7 also stops loading `.env` for you, so the config usually begins with `import "dotenv/config"`, and the `env()` helper throws `PrismaConfigEnvError` when the variable is unset or empty. Because the two values are independent, the CLI and the app can point at different URLs.
code
typescript · 12 linesimport "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
},
datasource: {
url: env("DATABASE_URL"),
},
});go deeper
Know the two files: prisma.config.ts gives the CLI its URL, and the adapter in your code gives Prisma Client its connection string.
Explain what each consumer reads, why .env needs explicit loading in Prisma 7, and when env() throwing is a feature or a problem.
Use the split deliberately: separate URLs or roles for migrations and runtime, and builds that generate the client without a database URL.
Decide where connection settings live and who owns them, so the CLI URL and the application URL cannot drift apart unnoticed.
## Two consumers, two connection settings A Prisma 7 project talks to the database from two different places, and each has its own setting: | Consumer | What it does | Where its URL comes from | |---|---|---| | **Prisma CLI** | `migrate dev`, `migrate deploy`, `db push`, `db pull`, `db execute` | `datasource.url` in `prisma.config.ts` | | **Prisma Client** | Your application's queries | The driver adapter passed to `new PrismaClient({ adapter })` | The **Prisma CLI** is the `prisma` command. It loads a config file named `prisma.config.ts` (other extensions and a `.config/prisma.ts` location are also accepted), usually placed at the project root next to `package.json`. The config file exports `defineConfig({ ... })`, imported from `prisma/config`, with keys such as `schema`, `migrations.path`, `migrations.seed` and `datasource`. Commands that need a database read `datasource.url`; if it is missing, the CLI stops with a message that `datasource.url` is required in your Prisma config file for that command. **Prisma Client** is the generated class your application imports. In Prisma 7 its constructor needs a **driver adapter** (or, for the hosted proxy, an `accelerateUrl`); constructing it with no options throws `PrismaClientInitializationError`. For PostgreSQL the adapter is `PrismaPg` from `@prisma/adapter-pg`, and you give it a `connectionString` (or a node-postgres pool config, or an existing `pg.Pool`). The client never opens `prisma.config.ts`. ## The schema's datasource block In Prisma 7 the `datasource` block in `schema.prisma` names the `provider`, such as `postgresql`. Connection URLs (`url`, `directUrl`, `shadowDatabaseUrl`) are configured in `prisma.config.ts`, not in the schema file. `prisma generate` writes the schema into the generated client, but no URL travels with it. ## Loading environment variables Prisma 7 does not load `.env` files by default. You choose how: - add `import "dotenv/config"` as the first line of `prisma.config.ts` (what `prisma init` generates); - or start the process with Node's `--env-file` flag; - or rely on a runtime that loads `.env` itself. Your application needs the same treatment: whatever reads `process.env.DATABASE_URL` for `PrismaPg` must have the variable set before the adapter is constructed. ## env() versus process.env `prisma/config` exports a typed `env()` helper. It **throws** a `PrismaConfigEnvError` ("Cannot resolve environment variable: DATABASE_URL.") when the variable is unset or empty, and because the throw happens while the config file loads, CLI commands fail before they start, including `prisma generate`, which never connects. Use `env()` when the variable must exist. Where a command may legitimately run without it, for example generating types in a build stage, read `process.env.DATABASE_URL` directly with a fallback; a command that does need the database then fails on its own with an empty-URL error. ## What moved from Prisma 6 1. The `url` in the schema's `datasource` block moved to `datasource.url` in the config file. 2. `directUrl` is gone; the single `url` in the config is what every CLI command uses, so a direct URL for migrations goes there. 3. The client constructor's `datasources` and `datasourceUrl` options are gone; the adapter takes the connection string. 4. `.env` is no longer loaded automatically. ## Why the split helps Because the CLI and the client read different values, you can point them at different endpoints: the application at a connection pooler, migrations at the database directly. You can also give the CLI a privileged role that may alter tables while the application's role may only read and write rows. The cost is that a change of database now touches two places, the config file's `datasource.url` and the adapter's `connectionString`, and a review should check both. ## A worked setup across environments A typical project ends up with the same two settings filled differently per environment: - **Local development**: one `.env` with `DATABASE_URL`; `prisma.config.ts` starts with `import "dotenv/config"` and uses `env("DATABASE_URL")`, and the application's adapter reads the same variable. - **Build stage**: only `prisma generate` runs. No database exists, so the config reads `process.env.DATABASE_URL` with a fallback instead of `env()`, or the build supplies a placeholder value. - **Release step**: `prisma migrate deploy` runs with the config's URL pointing at the target database, often with a role allowed to change tables. - **Running application**: the adapter gets the runtime URL, which may point at a pooler and use a role limited to reading and writing rows. Seeing the four side by side makes the rule plain: the config file serves whichever CLI command is running, and the adapter serves the application.
- How does Prisma 7 load a .env file when you run the CLI?It does not, by default. Import `dotenv/config` at the top of `prisma.config.ts`, which is what `prisma init` generates, or start the command with Node's `--env-file` flag, or use a runtime that loads `.env` on its own. Without one of these, `env("DATABASE_URL")` throws while the config loads.
- What happens if prisma migrate deploy runs with no usable datasource.url?With no `datasource.url` in the config, the CLI stops and says the property is required in your Prisma config file for that command. With `env()` and the variable unset, the config itself fails to load with `PrismaConfigEnvError`. With an empty string, `migrate deploy` fails with "Connection url is empty".
- PrismaClient reads datasource.url from prisma.config.ts when the application starts.
- In Prisma 7 the connection URL still belongs in the schema's datasource block.
- Prisma 7 loads the .env file automatically before every CLI command.
- The env() helper returns undefined when the variable is missing.
- Migrations get their own URL through datasource.directUrl in prisma.config.ts.
Assumes Prisma 7, which moved the URL out of the schema's datasource block into prisma.config.ts and requires a driver adapter (or accelerateUrl) in the PrismaClient constructor.