Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

error: Environment variable not found: DATABASE_URL (Prisma P1012): how to fix it

error: Environment variable not found: DATABASE_URL is Prisma's P1012 schema validation error: schema.prisma reads the connection string with env("DATABASE_URL") and the process has no such variable. After a deploy it is a .env file that never reached the server, a variable under another name, a migration run in the build, or a prisma.config.ts that stops Prisma loading .env. Set DATABASE_URL in the server's environment and run migrations where it exists.

By Livops team, the people who build and run LivopsUpdated

You run prisma migrate deploy, prisma db push or the app itself, and Prisma stops before it connects to anything. From the CLI on Prisma 6[18][3][1]:

Error: Prisma schema validation - (get-config wasm)
Error code: P1012
error: Environment variable not found: DATABASE_URL.
  -->  prisma/schema.prisma:7
   |
 6 |   provider = "postgresql"
 7 |   url      = env("DATABASE_URL")
   |

Validation Error Count: 1
[Context: getConfig]

From the app, Prisma Client throws the same message as an initialization error[5]:

PrismaClientInitializationError: error: Environment variable not found: DATABASE_URL.

Prisma found env("DATABASE_URL") in your schema and the process has no variable by that name. This page explains when Prisma checks it, lists the causes after a deploy, most common first, with the fix for each, and shows what changed in Prisma 7, where the same mistake has another message.

What the error means

In Prisma 4 to 6 the connection string sits in the datasource block of schema.prisma, usually as url = env("DATABASE_URL"). When Prisma loads the datasource URL, it reads the variable named inside env(); if there is none, it reports Environment variable not found: DATABASE_URL. and points at the url line[4][1].

The code is P1012, Prisma’s schema validation error, whose message is simply the full list of problems found[2][6]. The CLI prints it under Prisma schema validation, with Validation Error Count at the end[3]. So other schema errors can come in the same block; this page is about the one above.

Not every command checks the variable:

What runs Needs DATABASE_URL?
prisma generate (Prisma 6) No. It ignores missing variables, unless you use typed SQL with --sql[7]
prisma migrate deploy Yes. It loads the schema with variable errors switched on[8][9]
Your app, through Prisma Client Yes. Prisma Client throws PrismaClientInitializationError[5], which happens on $connect() or the first query[6]

That is why a build can pass and the deploy fail later: the build only generated the client, and the first command that needed the URL was the migration or the app.

Causes, most common first

1. DATABASE_URL is only in your .env file

On your machine the Prisma CLI loads .env from the project root, or from the folder of the schema, or from ./prisma, and Prisma Client adds its contents to process.env too[10]. That file is in .gitignore, as it should be: it holds the database password. So the server gets the code without the file, and without the variable that was in it.

Fix: set DATABASE_URL in the server’s environment. Prisma always reads variables from the system environment[10], so a real variable needs no .env at all. Set it where the app runs (the platform’s variables, your docker run -e, the service manager’s environment) and restart. For one command you can also put it in front:

DATABASE_URL="postgresql://user:password@host:5432/db" npx prisma migrate deploy

Do not commit .env to make the error go away. To show which names the deploy needs, commit a .env.example with dummy values instead.

2. The variable has another name

The schema reads one name and the server provides another: env("DATABASE_URL") against a platform that sets POSTGRES_URL, MYSQL_URL or MONGO_URL, or a typo such as DATABSE_URL. The error names the variable Prisma looked for[1]: compare it, letter by letter, with the list of variables on the server.

Fix: either change the name inside env() to the one the server has, or add a variable under the name the schema reads. If you use directUrl or shadowDatabaseUrl with their own env(), each needs its variable as well[4].

3. A migration runs in the build, where the variable is not

A Dockerfile with RUN npx prisma migrate deploy, or a build script like prisma migrate deploy && next build, runs the migration while the image is built. Build steps often do not get the runtime variables, and the database may not be reachable from there anyway. prisma generate would pass in the same place[7]; the migration does not[9].

Fix: migrate when the app starts, not when it is built. Prisma’s own Docker guide runs prisma migrate deploy from the container’s start command, with DATABASE_URL given to the running container through an env file[17]:

CMD ["sh", "-c", "npx prisma migrate deploy && node dist/server.js"]

On a platform with a release or pre-deploy step, put the migration there instead, so a failed migration stops the release before the new version starts.

4. prisma.config.ts turned off .env loading

Since Prisma 6.4 a project can have a prisma.config.ts[10]. When it does, Prisma no longer loads .env for you: the variables in it must be loaded explicitly[11], and the CLI says so when it starts[12]:

Prisma config detected, skipping environment variable loading.

So a project that worked with .env breaks locally the day someone adds the config file. Fix: load the file at the top of prisma.config.ts[11]:

import "dotenv/config";

and install dotenv. That is for your machine. On the server, set real variables as in cause 1: a .env that is not there cannot be loaded by any import.

Prisma 7: the same mistake, another message

Prisma 7 changes where the URL lives and the error you get:

  • url is no longer allowed in schema.prisma. A schema that still has it fails validation with The datasource property `url` is no longer supported in schema files; connection URLs for Migrate move to prisma.config.ts[16]. So Environment variable not found comes from Prisma 6 and older.
  • Prisma 7 does not load .env by itself[13]. prisma init writes import "dotenv/config" into the config it creates[13].
  • env() from prisma/config throws when the variable is missing[13][14], and the CLI fails to load the config file[15]:
Failed to load config file "/app/prisma.config.ts" as a TypeScript/JavaScript module. Error: PrismaConfigEnvError: Cannot resolve environment variable: DATABASE_URL.

Prisma 7.0 and 7.1 printed Missing required environment variable: DATABASE_URL instead[13].

This one also stops prisma generate. It does not need a database URL, but it still loads the config, and env() throws while loading it[13]. Prisma’s advice: use env() when the variable must exist, and read process.env directly when it may be absent for some commands[13]:

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 ?? "",
  },
});

Now prisma generate works in a build without the variable, and a command that connects to the database still needs it to be set[13].

How to confirm the fix locally

Check the variable in the same shell, container or step that runs Prisma, not in another one:

node -e 'console.log(process.env.DATABASE_URL ? "set" : "missing")'

Then reproduce the server’s conditions: move .env away for a moment and run the command that failed, with the variable given only by the environment.

mv .env .env.off
DATABASE_URL="postgresql://user:password@localhost:5432/db" npx prisma migrate deploy
mv .env.off .env

If that works and the server still fails, the variable is missing or misnamed on the server.

On Livops

  • A PostgreSQL database brings DATABASE_URL. Add PostgreSQL under Databases, and its address is written as DATABASE_URL to every service in the environment, including services added later. Keep env("DATABASE_URL") and there is nothing to copy.
  • MySQL and MongoDB use other names. A MySQL database is written as MYSQL_URL and MongoDB as MONGO_URL (also MONGODB_URI and others). Read that name in env() or prisma.config.ts, or cause 2 is yours.
  • Everything else goes in Variables. For an external database, or SQLite at DATABASE_URL=file:/app/data/app.db, open Settings → Variables, add DATABASE_URL, and mark it as a secret so its value is never shown again. An .env file does not reach the repository. New values take effect on the next deploy; restart the service to apply one now.
  • prisma generate runs before the build. When the project has prisma and prisma/schema.prisma, Livops runs prisma generate itself. On Prisma 7, write the config with process.env as above, so generating never depends on the URL.
  • Migrations go in the pre-deploy command, not the build. Livops’s docs put migrations there too. Make npx prisma migrate deploy the pre-deploy command; if it fails, the deploy stops as predeploy_failed, the new version is not started and the old one keeps serving.
  • Where you see it. If the app throws at start-up, the deploy fails as start_failed and the message is on the Logs page; a web app’s previous version keeps running.
  • Staging has its own database. A new environment gets its own empty database for each type in production, and a promoted release keeps production’s own DATABASE_URL.

A web app costs 39 credits a month. Once the variable is found, the next error on the way is usually the connection itself: Prisma P1001: Can’t reach database server. If the client cannot start at all, see Prisma Client could not locate the Query Engine.

Questions

What is Prisma error P1012?

P1012 is the code for a Prisma schema validation error. Its message is the full list of problems found in schema.prisma, so Environment variable not found is only one of the errors that come with this code. Read the lines after Error code: P1012 to see which.

Why does it work on my machine and fail on the server?

On your machine DATABASE_URL comes from a .env file that Prisma loads for you. That file is in .gitignore, so the server never gets it. Prisma always reads the real environment, so set DATABASE_URL there and the same schema works in both places.

Does prisma generate need DATABASE_URL?

Not in Prisma 6, unless you use typed SQL with --sql: plain prisma generate ignores missing variables. In Prisma 7 it does not need a URL either, but it still fails if prisma.config.ts calls env("DATABASE_URL") and the variable is missing. Read process.env.DATABASE_URL in the config instead.

Should I commit my .env file so the server has it?

No. It holds the database password. Keep it out of the repository, commit a .env.example with the names and dummy values if you want to document them, and set the real values as environment variables on the server.

I added prisma.config.ts and now the variable is missing. Why?

Since Prisma 6.4 a project can have prisma.config.ts, and with it Prisma stops loading .env files for you: the CLI prints Prisma config detected, skipping environment variable loading. Prisma 7 never loads .env itself. Add import "dotenv/config" at the top of the config file for local work.

What error does Prisma 7 show instead?

Prisma 7 no longer accepts url in schema.prisma; the URL moves to prisma.config.ts. If env("DATABASE_URL") there finds nothing, loading the config fails with PrismaConfigEnvError: Cannot resolve environment variable: DATABASE_URL. Prisma 7.0 and 7.1 printed Missing required environment variable: DATABASE_URL.

Sources

  1. Prisma source — psl/diagnostics/src/error.rs (prisma-engines 6.19.0) (raw.githubusercontent.com). Checked .
  2. Prisma source — libs/user-facing-errors/src/common.rs (prisma-engines 6.19.0) (raw.githubusercontent.com). Checked .
  3. Prisma source — engine-commands/getConfig.ts (6.19.0) (raw.githubusercontent.com). Checked .
  4. Prisma source — psl-core/src/configuration/datasource.rs (prisma-engines 6.19.0) (raw.githubusercontent.com). Checked .
  5. Prisma source — client runtime resolveDatasourceUrl.ts (6.19.0) (raw.githubusercontent.com). Checked .
  6. Prisma ORM v6 — Error message reference (prisma.io). Checked .
  7. Prisma source — cli/src/Generate.ts (6.19.0) (raw.githubusercontent.com). Checked .
  8. Prisma source — internals/src/cli/schemaContext.ts (6.19.0) (raw.githubusercontent.com). Checked .
  9. Prisma source — migrate/src/commands/MigrateDeploy.ts (6.19.0) (raw.githubusercontent.com). Checked .
  10. Prisma ORM v6 — Environment variables (prisma.io). Checked .
  11. Prisma ORM v6 — Prisma Config reference (prisma.io). Checked .
  12. Prisma source — internals/src/utils/loadEnvFile.ts (6.19.0) (raw.githubusercontent.com). Checked .
  13. Prisma ORM v7 — Prisma Config reference (prisma.io). Checked .
  14. Prisma source — config/src/env.ts (7.10.1) (raw.githubusercontent.com). Checked .
  15. Prisma source — cli/src/utils/loadConfig.ts (7.10.1) (raw.githubusercontent.com). Checked .
  16. Prisma source — psl/diagnostics/src/error.rs (prisma-engines 7.0.0) (raw.githubusercontent.com). Checked .
  17. Prisma ORM v7 — How to use Prisma in Docker (prisma.io). Checked .
  18. Prisma source — cli/src/bin.ts (6.19.0) (raw.githubusercontent.com). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your Node.js app

Got a question? — @livops_uz