Drizzle Kit "DATABASE_URL is undefined" (proofforge.live): 6 causes and the fixes that do not leak your database
You run npx drizzle-kit push or generate, and it stops with something like "url: undefined" or "Please provide required params for Postgres driver". Your app runs fine in next dev, and .env.local clearly has DATABASE_URL
You run npx drizzle-kit push or generate, and it stops with something like "url: undefined" or "Please provide required params for Postgres driver". Your app runs fine in next dev, and .env.local clearly has DATABASE_URL. I'm part of the ProofForge team. This is the cause I see most often in AI-built Next.js apps, plus the fixes that do not leak your database.
Why it happens
Next.js loads .env.local for its own processes. drizzle-kit is a separate CLI. It runs your drizzle.config.ts on its own, and it does not read .env.local unless you tell it to. So process.env.DATABASE_URL is undefined inside the config, even though the app can see it.
6 causes, in the order I would check them
-
The config never loads the file. Add this at the top of
drizzle.config.ts:
import { config } from "dotenv";
import { defineConfig } from "drizzle-kit";
config({ path: ".env.local" });
export default defineConfig({
schema: "./src/db/schema.ts",
out: "./drizzle",
dialect: "postgresql",
dbCredentials: { url: process.env.DATABASE_URL! },
});
-
dotenv is not installed.
npm i -D dotenv. Without it the import fails instead. -
Wrong file name.
.envand.env.localare different files. Print which one has the value:grep -c DATABASE_URL .env .env.local. -
Quotes or spaces in the value. Keep it as
DATABASE_URL=postgres://...with no spaces around=. Special characters in the password need URL encoding. - Pooled vs direct URL. Many hosts give two connection strings. Migrations usually want the direct one, the app usually wants the pooled one. If push hangs or fails with a prepared statement error, try the direct URL for the CLI only.
- Different shell, different env. In CI or a deploy step, the variable has to be set in that environment. A local file does not travel.
The fixes to skip
-
Do not rename it to
NEXT_PUBLIC_DATABASE_URLto make it appear. That prefix ships the value into the browser bundle. A database connection string is a full-access secret. -
Do not paste the URL into
drizzle.config.tsor the schema file. Config files get committed. -
Do not commit
.env.local. Check.gitignorebefore the first push, and check your git history if you already did. - If a connection string ever reached a public repo or a client bundle, rotate the database password, then redeploy. Deleting the line is not enough, because old builds and history keep it.
Check it from the outside
After you deploy, look at your app the way a stranger would. Search your built JavaScript for postgres:// and DATABASE_URL, and check that /.env and /.git/config return 404 on your live domain.
Quick check
Run the free ProofForge scan on your live URL. It looks at your public site, passively and read-only, for exposed keys, open data and missing headers, and says pass, fail or inconclusive. No account needed for the first scan. It cannot see your server-side env, so a clean result is not a full audit.
Check your own app from the outside, free. Run the free ProofForge scan on your live URL. No account needed for the first scan. It looks at your public site the way a stranger would and says pass, fail or inconclusive, so you know what is confirmed and what it could not see. One-time reports start at $9: see pricing. Building with an AI agent? There is an MCP server so it can check before it deploys.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.