How to fix P1013: The provided database string is invalid in Prisma
Prisma cannot parse your database connection string. This occurs when your DATABASE_URL has formatting errors, missing components, invalid characters, or incompatible parameters. Common causes include unescaped special characters in passwords, missing database names, and incorrect URL schemes.
Quick fix
Fixes P1013
DATABASE_URL="postgresql://user:password@localhost:5432/mydatabase?schema=public"
# or
DATABASE_URL="postgres://user:password@localhost:5432/mydatabase?schema=public"- Verify your DATABASE_URL format matches your database type
- URL-encode special characters in your password and username
- Remove quotes around the DATABASE_URL value
- Verify your host and port are correct
DATABASE_URL="postgresql://user:password@localhost:5432/mydatabase?schema=public"# orDATABASE_URL="postgres://user:password@localhost:5432/mydatabase?schema=public"P1013: The provided database string is invalid
What this error means
The P1013 error indicates that Prisma encountered a malformed or unparseable database connection string. This is a validation error that occurs during the schema parsing phase, before any actual connection attempt. Prisma validates the DATABASE_URL format against strict URL parsing rules. If the URL syntax is invalid, contains unsupported parameters, or lacks required components for your database type, Prisma will reject it with P1013. This error can manifest in several ways depending on what's wrong with your connection string: missing database names (especially MongoDB), unescaped special characters, incorrect URL schemes, empty hosts, or unsupported parameter values.
Typical symptoms
- Error occurs immediately when starting your application or running Prisma commands
- Error message mentions "Unable to parse URL" or similar parsing errors
- Connection string appears correct but still fails validation
- Error occurs with specific database providers (MongoDB, SQL Server, etc.)
- Changes to DATABASE_URL in .env files cause validation to fail
- Previous working connection string fails after copying to different environment
Common causes
- Special characters in password or username not URL-encoded (%, @, :, etc.)
- Missing database name in connection string (required for MongoDB)
- Incorrect URL scheme (postgresql:// instead of mysql://, etc.)
- Empty or missing host in connection string
- Invalid parameter values (pool_timeout as string instead of integer)
- Unsupported parameters for your database type
- Double quotes around DATABASE_URL in .env file
- Whitespace or line breaks in connection string
- Typo in URL scheme or malformed port number
- Missing required components like database name for MongoDB
How to fix "P1013: The provided database string is invalid"
Ensure your connection string follows the correct format for your database provider:
PostgreSQL/CockroachDB:
DATABASE_URL="postgresql://user:password@localhost:5432/mydatabase?schema=public"
# or
DATABASE_URL="postgres://user:password@localhost:5432/mydatabase?schema=public"MySQL:
DATABASE_URL="mysql://user:password@localhost:3306/mydatabase"MongoDB (requires database name):
DATABASE_URL="mongodb+srv://user:password@cluster.mongodb.net/mydatabase?retryWrites=true&w=majority"SQLite:
DATABASE_URL="file:./dev.db"SQL Server:
DATABASE_URL="sqlserver://localhost:1433;database=mydatabase;user=sa;password=Password123;encrypt=true"Check the [Prisma connection URL reference](https://www.prisma.io/docs/orm/reference/connection-urls) for your specific database.
If your password or username contains special characters, they must be percent-encoded:
Common characters to encode:
- @ → %40
- : → %3A
- / → %2F
- ? → %3F
- # → %23
- ! → %21
- $ → %24
- & → %26
Example:
# Original password: p@ssw0rd!
# Encoded: p%40ssw0rd%21
DATABASE_URL="postgresql://user:p%40ssw0rd%21@localhost:5432/mydb"You can use online URL encoders or encode manually. Ensure the encoded string is placed directly in your connection URL without additional quotes or escaping.
Ensure your .env file has no quotes around the connection string:
INCORRECT (will fail):
DATABASE_URL='postgresql://user:pass@localhost:5432/db'
DATABASE_URL="postgresql://user:pass@localhost:5432/db"
DATABASE_URL=`postgresql://user:pass@localhost:5432/db`CORRECT:
DATABASE_URL=postgresql://user:pass@localhost:5432/dbPrisma loads environment variables and parses them without quotes. Adding quotes causes them to be included in the parsed value, breaking URL validation.
Check that the host and port are properly formatted:
Valid host formats:
- localhost (local development)
- 127.0.0.1 (local IP)
- mydb.example.com (domain name)
- db.c.mongodb.net (MongoDB Atlas)
- postgres.railway.app (Railway)
Valid port formats:
- PostgreSQL: 5432 (default)
- MySQL: 3306 (default)
- SQL Server: 1433 (default)
- MongoDB: Included in hostname for Atlas
Example of correct format:
DATABASE_URL="postgresql://user:pass@mydb.example.com:5432/mydb"If your port contains non-numeric characters, the URL parser will reject it.
Different databases have different requirements:
MongoDB requires database name:
# INCORRECT (no database)
DATABASE_URL="mongodb+srv://user:pass@cluster.mongodb.net"
# CORRECT (includes /dbname)
DATABASE_URL="mongodb+srv://user:pass@cluster.mongodb.net/mydatabase"PostgreSQL should include schema parameter:
DATABASE_URL="postgresql://user:pass@localhost:5432/db?schema=public"Parameter values must be correct type:
# INCORRECT (pool_timeout as string)
DATABASE_URL="postgresql://user:pass@localhost:5432/db?pool_timeout=30s"
# CORRECT (pool_timeout as integer)
DATABASE_URL="postgresql://user:pass@localhost:5432/db?pool_timeout=30"Ensure your DATABASE_URL has no extra whitespace:
INCORRECT (has newline):
DATABASE_URL=postgresql://user:pass@localhost:5432/db
?sslmode=requireCORRECT (single line):
DATABASE_URL=postgresql://user:pass@localhost:5432/db?sslmode=requireIf you copy connection strings from multiple lines, ensure you remove all line breaks and extra spaces before using them in your .env file.
Prisma looks for .env files in specific locations:
1. Same directory as package.json
2. The prisma/ directory (if schema.prisma is there)
3. Parent directories (Prisma traverses up the tree)
For development:
# Restart your dev server to reload .env
npm run devFor Prisma CLI commands:
# Explicitly reload environment
npx prisma generate
npx prisma db pushIf you're using Next.js, make sure your variables are in .env.local (not just .env), as Prisma reads from .env by default.
Advanced notes
Connection String Validation: Prisma validates the entire connection URL structure before attempting any connection. The validation is strict and follows RFC 3986 URL standards. Even if a connection string looks correct to a human, minor syntax errors will fail validation.
Environment Variable Loading: Prisma uses the dotenv library internally to load .env files. The loading priority is: project root .env > prisma/.env > parent directory .env. If you have multiple .env files at different directory levels, the one closest to your project root takes precedence.
Special Characters in Passwords: Always URL-encode special characters in passwords and usernames. This includes characters that have special meaning in URLs like @, :, /, ?, #, and others. Some tools provide connection string builders that handle this automatically.
Database-Specific Requirements: MongoDB and SQLite have stricter URL requirements than SQL databases. MongoDB requires the database name after the hostname. SQLite uses a file:// scheme. Make sure your provider documentation is consulted for exact format requirements.
Railway and Other Platforms: Cloud providers often provide pre-formatted connection strings. Copy them exactly as provided. Adding, removing, or modifying any part (even seemingly redundant parameters) can cause P1013 errors.
Testing Connection Strings: Before using a connection string in your application, test it with your database provider's CLI tool:
- PostgreSQL: psql <connection-string>
- MySQL: mysql <connection-string>
- MongoDB: mongosh <connection-string>
If the CLI tool can parse it, the format is valid for that database.
Frequently asked questions
- What does "P1013" mean?
- The P1013 error indicates that Prisma encountered a malformed or unparseable database connection string. This is a validation error that occurs during the schema parsing phase, before any actual connection attempt. Prisma validates the DATABASE_URL format against strict URL parsing rules.
- How do I fix "P1013"?
- Prisma cannot parse your database connection string. This occurs when your DATABASE_URL has formatting errors, missing components, invalid characters, or incompatible parameters. Common causes include unescaped special characters in passwords, missing database names, and incorrect URL schemes.
- What causes "P1013"?
- Special characters in password or username not URL-encoded (%, @, :, etc.) Missing database name in connection string (required for MongoDB) Incorrect URL scheme (postgresql:// instead of mysql://, etc.)
- Is the "P1013" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- 📚Prisma Official Documentation
- 📖prisma.io - connection urls(Official Docs)
- 🐙github.com - 11883(GitHub)
- 🐙github.com - 13388(GitHub)
- 🐙github.com - 20230(GitHub)
- 🐙github.com - 13764(GitHub)
- 📖prisma.io - error reference(Official Docs)
- 🔗
- 📝