Fix Prisma Datasource URL – Connection String Issues
DodaTech
Updated 2026-06-24
3 min read
In this tutorial, you'll learn about Fix Prisma Datasource URL. We cover key concepts, practical examples, and best practices.
You run any Prisma command and get: "Error: Environment variable not found: DATABASE_URL" — or "Error: Invalid datasource URL: invalid port number". The URL is set in .env but Prisma can't read it.
Wrong ❌
# .env file
DATABASE_URL=postgresql://postgres:password@localhost:5432
```prisma
datasource db {
provider = "<a href="/databases/postgresql/">postgresql</a>"
url = env("DATABASE_URL")
}
```bash
npx prisma db pull
Error:
Error: Environment variable not found: DATABASE_URL
The `.env` file exists but Prisma didn't load it. The `DATABASE_URL` is set but the path to `.env` is wrong.
## Right ✅
```env
# .env (in the project root, same directory as schema.prisma)
DATABASE_URL="<a href="/databases/postgresql/">postgresql</a>://postgres:password@localhost:5432/mydb?schema=public"
```prisma
// schema.prisma
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
**Common URL formats:**
```env
# <a href="/databases/postgresql/">PostgreSQL</a>
DATABASE_URL="<a href="/databases/postgresql/">postgresql</a>://user:password@host:5432/dbname?schema=public"
# MySQL
DATABASE_URL="mysql://user:password@host:3306/dbname"
# SQLite
DATABASE_URL="file:./dev.db"
# <a href="/databases/mongodb/">MongoDB</a>
DATABASE_URL="<a href="/databases/mongodb/">mongodb</a>://user:password@host:27017/dbname"
# SQL Server
DATABASE_URL="sqlserver://host:1433;database=dbname;user=sa;password=password;trustServerCertificate=true"
**If Prisma still can't find the env var:**
Specify the path to `.env`:
```bash
dotenv -e .env -- npx prisma db pull
# Or use the Prisma CLI option:
npx prisma db pull --schema=prisma/schema.prisma
**Or load `.env` manually in `prisma/seed.js`:**
```<a href="/programming-languages/javascript/">javascript</a>
const { PrismaClient } = require('@prisma/client');
require('dotenv').config({ path: '../.env' }); // if seed is in prisma/
**Validate the URL format:**
```bash
npx prisma validate --schema=prisma/schema.prisma
# If the URL is invalid, it shows the exact parsing error
**If using a connection pooler (PgBouncer):**
```env
# Direct connection (for migrations)
DATABASE_URL="<a href="/databases/postgresql/">postgresql</a>://user:password@host:5432/dbname"
# Pooled connection (for runtime)
DATABASE_URL_POOLED="<a href="/databases/postgresql/">postgresql</a>://user:password@host:6543/dbname?pgbouncer=true"
```prisma
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DATABASE_URL_POOLED") // Prisma 5.10+
}
**Check if the database is accessible:**
```bash
psql "${DATABASE_URL}"
# Should connect successfully
## Root Cause
Prisma reads `.env` automatically only if the file is in the **project root** (where `package.json` and `prisma/schema.prisma` reside). If `.env` is in a subdirectory or missing, `env("DATABASE_URL")` returns undefined.
## Prevention
- Keep `.env` in the project root.
- Run `npx prisma validate` to verify the datasource URL is valid.
- Use `dotenv-cli` if `.env` is in a non‑standard location.
- Never commit `.env` to Git — use `.env.example` as a template.
## Common Mistakes with datasource url
1. **Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable**
2. **Using `head` and `tail` instead of pattern matching, causing runtime errors on empty lists**
3. **Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks**
These mistakes appear frequently in real-world PRISMA code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
## Practice Exercise
**Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.**
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
## FAQ
<details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: Can I hardcode the URL in schema.prisma instead of using env?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: Yes — but it's not recommended. Use <code>url = "<a href="/databases/postgresql/">postgresql</a>://..."</code> for development only.</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: Why does Prisma say "Invalid port number"?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: The URL has a typo like <code>5432</code> (should be <code>5432</code>) or a non‑numeric port.</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: Does Prisma support `postgres://` instead of `<a href="/databases/postgresql/">postgresql</a>://`?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: Yes — <code>postgres://</code> is an alias for <code><a href="/databases/postgresql/">postgresql</a>://</code>.</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: How do I use a Unix socket?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: Use:
DATABASE_URL="<a href="/databases/postgresql/">postgresql</a>://user:password@/dbname?host=/var/run/<a href="/databases/postgresql/">postgresql</a>"</p>
</div></details>
---
*Datasource configuration is covered in the [DodaTech Prisma Setup course](https://dodatech.com/courses/prisma).*
← Previous
Fix Prisma Client Not Found – 'PrismaClient' is Not a Constructor
Next →
Fix Prisma Generate Schema – Schema Validation Errors
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro