Error: Environment variable not found: DATABASE_URL
What causes this
Prisma reads DATABASE_URL from your .env file (or system environment variables) to connect to your database. This error means it can’t find it. Common causes:
- No
.envfile exists in your project root - The
.envfile exists but doesn’t containDATABASE_URL - The
.envfile is in the wrong directory (Prisma looks in the directory where you run the command) - You’re running in a CI/CD environment where
.envfiles aren’t deployed - The variable name has a typo or extra whitespace
Fix 1: Create or fix your .env file
# .env (in project root, same level as package.json)
DATABASE_URL="postgresql://user:password@localhost:5432/mydb"
For different databases:
# PostgreSQL
DATABASE_URL="postgresql://user:password@localhost:5432/mydb"
# MySQL
DATABASE_URL="mysql://user:password@localhost:3306/mydb"
# SQLite
DATABASE_URL="file:./dev.db"
# MongoDB
DATABASE_URL="mongodb://user:password@localhost:27017/mydb"
Fix 2: Check the file location
Prisma looks for .env relative to where you run the command:
# Make sure you're in the project root
ls -la .env
# Check what's in it
cat .env | grep DATABASE_URL
If your .env is in a subdirectory, tell Prisma where to find it in schema.prisma:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
Or specify a custom path:
# Use dotenv-cli to load from a specific file
npx dotenv -e .env.local -- npx prisma migrate dev
Fix 3: Set the variable in your shell
For one-off commands:
DATABASE_URL="postgresql://user:password@localhost:5432/mydb" npx prisma migrate dev
Or export it for the session:
export DATABASE_URL="postgresql://user:password@localhost:5432/mydb"
npx prisma migrate dev
Fix 4: Fix CI/CD environments
In CI, set environment variables through your platform’s settings, not .env files:
# GitHub Actions
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
# Vercel — set in project settings dashboard
# Docker
docker run -e DATABASE_URL="postgresql://..." myapp
Fix 5: Check for invisible characters
Sometimes copy-pasting introduces invisible characters:
# Check for hidden characters
cat -A .env | grep DATABASE_URL
# Should show: DATABASE_URL="..."$
# If you see ^M or other characters, recreate the file
How to prevent it
- Add
.env.exampleto your repo with placeholder values so new developers know what variables are needed - Use our .env file validator to check for common issues
- Never commit
.envto git — add it to.gitignore - In production, use your platform’s secrets management instead of
.envfiles - Check out our guide on environment variables for Next.js, Prisma, and Stripe
Related: Prisma: Migration Failed — Database Schema Drift
Related: Prisma P2002: Unique Constraint Failed — How to Fix It