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:
urlis no longer allowed inschema.prisma. A schema that still has it fails validation withThe datasource property `url` is no longer supported in schema files; connection URLs for Migrate move toprisma.config.ts[16]. SoEnvironment variable not foundcomes from Prisma 6 and older.- Prisma 7 does not load
.envby itself[13].prisma initwritesimport "dotenv/config"into the config it creates[13]. env()fromprisma/configthrows 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_URLto every service in the environment, including services added later. Keepenv("DATABASE_URL")and there is nothing to copy. - MySQL and MongoDB use other names. A MySQL database is written as
MYSQL_URLand MongoDB asMONGO_URL(alsoMONGODB_URIand others). Read that name inenv()orprisma.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, addDATABASE_URL, and mark it as a secret so its value is never shown again. An.envfile does not reach the repository. New values take effect on the next deploy; restart the service to apply one now. prisma generateruns before the build. When the project hasprismaandprisma/schema.prisma, Livops runsprisma generateitself. On Prisma 7, write the config withprocess.envas 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 deploythe pre-deploy command; if it fails, the deploy stops aspredeploy_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_failedand 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
- Prisma source — psl/diagnostics/src/error.rs (prisma-engines 6.19.0) (raw.githubusercontent.com). Checked .
- Prisma source — libs/user-facing-errors/src/common.rs (prisma-engines 6.19.0) (raw.githubusercontent.com). Checked .
- Prisma source — engine-commands/getConfig.ts (6.19.0) (raw.githubusercontent.com). Checked .
- Prisma source — psl-core/src/configuration/datasource.rs (prisma-engines 6.19.0) (raw.githubusercontent.com). Checked .
- Prisma source — client runtime resolveDatasourceUrl.ts (6.19.0) (raw.githubusercontent.com). Checked .
- Prisma ORM v6 — Error message reference (prisma.io). Checked .
- Prisma source — cli/src/Generate.ts (6.19.0) (raw.githubusercontent.com). Checked .
- Prisma source — internals/src/cli/schemaContext.ts (6.19.0) (raw.githubusercontent.com). Checked .
- Prisma source — migrate/src/commands/MigrateDeploy.ts (6.19.0) (raw.githubusercontent.com). Checked .
- Prisma ORM v6 — Environment variables (prisma.io). Checked .
- Prisma ORM v6 — Prisma Config reference (prisma.io). Checked .
- Prisma source — internals/src/utils/loadEnvFile.ts (6.19.0) (raw.githubusercontent.com). Checked .
- Prisma ORM v7 — Prisma Config reference (prisma.io). Checked .
- Prisma source — config/src/env.ts (7.10.1) (raw.githubusercontent.com). Checked .
- Prisma source — cli/src/utils/loadConfig.ts (7.10.1) (raw.githubusercontent.com). Checked .
- Prisma source — psl/diagnostics/src/error.rs (prisma-engines 7.0.0) (raw.githubusercontent.com). Checked .
- Prisma ORM v7 — How to use Prisma in Docker (prisma.io). Checked .
- 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 appRelated
- Prisma P1001: Can't reach database server at … — how to fix itPrisma tried the host and port in your connection URL and nothing answered there. After a deploy that is almost always a URL that says localhost, a database that is not attached to the project, or prisma migrate deploy running in a build that has no database. Read the host in the message first: it tells you which.
- Prisma Client could not locate the Query Engine for runtime: how to fix itPrisma Client could not locate the Query Engine for runtime debian-openssl-3.0.x, or the older Query engine library for current platform could not be found, means the client was generated for another platform, usually your laptop, or its engine file was not copied. Run prisma generate where the app is built, add the runtime to binaryTargets, and do not ship node_modules. Prisma 7 has no Rust engine, so the error ends there.
- ECONNREFUSED / Connection refused: why the app cannot reach its database after deployNothing was listening at the address your app tried. When the address is 127.0.0.1 or localhost, the app never read the database's real address: DATABASE_URL is not set, is not read, or is read during a build that has no database. Attach the database, read DATABASE_URL (or REDIS_URL, MYSQL_URL) at runtime, run migrations before the start rather than in the build, and keep the connection pool under the server's limit.
- FATAL: password authentication failed for user "…": how to fix it after deployFATAL: password authentication failed for user "…" means PostgreSQL was reached but refused the login: the password, or the user name, is not the one the server has. After a deploy it is usually a DATABASE_URL copied from your local .env, a password with special characters that are not percent-encoded in the URL, the wrong user, or a password that was changed. Test the exact value your app reads with psql.
- Node.js hosting in 2026: how to deploy an Express appA Node.js server in production needs one long-running process that listens on the port it is given, restarts when it crashes and exits cleanly on SIGTERM. On Livops you push a repository with package.json and it runs as a web app for 39 credits a month (39,000 so'm). Here is what Node needs, and how a VPS, Render, Railway, Fly.io, DigitalOcean and Heroku compare.
- Managed PostgreSQL hosting in 2026: the options comparedA production PostgreSQL needs backups you can restore, a connection limit your app stays under, the app in the same region, and a supported version. On Livops a PostgreSQL 15, 16 or 17 database runs inside your project, its DATABASE_URL is written into your services, and it is backed up every night; 500 MB is free, one per team, and 1 GB is 9 credits a month. Here is how the paid plans of Neon, Supabase, Render, Railway and a VPS compare; free tiers have their own page.
- Next.js hosting without Vercel: the options comparedNext.js runs on any host that runs Node: next build, then next start, and every feature works, Server Actions and image optimization included. On Livops a repository with next in package.json runs as a server from a push for 39 credits a month (39,000 so'm), and a static export is free. Here is how Vercel, Render, Railway, Fly.io and a VPS compare.
- Prisma P3009: migrate found failed migrations in the target databaseAn earlier prisma migrate deploy started a migration and never finished it, and Prisma recorded that in the _prisma_migrations table. Until that record is resolved, no new migration is applied. Find the migration and why it failed, fix the SQL or the database, mark it with prisma migrate resolve --rolled-back or --applied, and run migrate deploy again, before the app starts rather than in the build.