How to fix P1001: Can't reach database server at `localhost`:`5432` in Prisma
Prisma cannot establish a connection to your database server. This typically occurs due to incorrect connection strings, network issues, firewall restrictions, or serverless databases in idle state.
Quick fix
Fixes P1001
DATABASE_URL="postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=public"- Verify your DATABASE_URL connection string format
- Test database connectivity directly
- Increase connection timeout for serverless databases
- Check database server status
DATABASE_URL="postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=public"P1001: Can't reach database server at `localhost`:`5432`
What this error means
The P1001 error indicates that Prisma Client is unable to reach your database server at the specified host and port. This is a connection-level failure that occurs before any authentication or query execution attempts. This error commonly appears when using serverless database providers like Neon, Supabase, or PlanetScale, where databases may enter an idle state after periods of inactivity. The default Prisma connection timeout of 10 seconds may not be sufficient for serverless compute instances to wake up and accept connections. The error can also result from incorrect DATABASE_URL configuration, firewall rules blocking database access, the database server being stopped, or network connectivity issues between your application and the database server.
Typical symptoms
- Database migrations fail with "Can't reach database server" message
- Application crashes on startup when Prisma Client attempts to connect
- Intermittent connection failures in serverless or development environments
- Error occurs immediately after deploying to cloud platforms like Vercel or Railway
- Local development works but production deployment fails to connect
- Database operations timeout before completing
Common causes
- Incorrect DATABASE_URL connection string format or credentials
- Serverless database in idle state requiring time to wake up (Neon, Supabase)
- Database server is not running or has crashed
- Firewall or security group blocking access to database port
- Network connectivity issues between application and database
- Special characters in database password not properly URL-encoded
- Wrong host, port, or database name in connection string
- SSL/TLS configuration mismatch between client and server
- Connection timeout too short for database to respond
- Database server reached maximum connection limit
- VPC or private network restricting external access
How to fix "P1001: Can't reach database server at `localhost`:`5432`"
Check your .env file and ensure the DATABASE_URL follows the correct format for your database provider:
PostgreSQL:
DATABASE_URL="postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=public"MySQL:
DATABASE_URL="mysql://USER:PASSWORD@HOST:PORT/DATABASE"MongoDB:
DATABASE_URL="mongodb+srv://USER:PASSWORD@HOST/DATABASE?retryWrites=true&w=majority"Important: URL-encode special characters in passwords. For example, p@ssw0rd! becomes p%40ssw0rd%21.
Before troubleshooting Prisma, verify you can connect to the database using a database client or CLI:
PostgreSQL:
psql -h HOST -p PORT -U USER -d DATABASEMySQL:
mysql -h HOST -P PORT -u USER -p DATABASEIf the direct connection fails, the issue is with your database server or network, not Prisma.
Serverless databases like Neon may need extra time to wake from idle state. Add connection timeout parameters to your DATABASE_URL:
DATABASE_URL="postgresql://user:pass@host:5432/db?connect_timeout=30&pool_timeout=30"For production environments, consider these additional parameters:
DATABASE_URL="postgresql://user:pass@host:5432/db?connect_timeout=30&pool_timeout=30&connection_limit=5"This gives the database 30 seconds to respond instead of the default 10 seconds.
Ensure your database server is running:
For local PostgreSQL:
# macOS
brew services list | grep postgresql
# Linux
sudo systemctl status postgresqlFor local MySQL:
# macOS
brew services list | grep mysql
# Linux
sudo systemctl status mysqlFor cloud providers: Check your database dashboard (Railway, Render, Supabase, etc.) to verify the instance is active.
Check that your database port is accessible:
# Test connectivity to database port
nc -zv HOST PORT
# Example for PostgreSQL
nc -zv localhost 5432For cloud databases:
- Add your application's IP address to the database firewall allowlist
- For Vercel/serverless: Enable static IP and configure your database to allow those IPs
- For AWS RDS: Check security group inbound rules allow port 5432 (PostgreSQL) or 3306 (MySQL)
- For Railway/Render: Ensure the database is set to accept external connections
Many cloud database providers require SSL connections. Add the sslmode parameter:
PostgreSQL (Supabase, Neon, Railway):
DATABASE_URL="postgresql://user:pass@host:5432/db?sslmode=require"MySQL with SSL:
DATABASE_URL="mysql://user:pass@host:3306/db?ssl=true"Some providers may need sslmode=no-verify for development, but use sslmode=require or sslmode=verify-full in production.
If your password contains special characters and URL encoding doesn't work:
1. Reset your database password to use only alphanumeric characters (letters and numbers)
2. Update the password in your cloud provider's dashboard
3. Update your .env file with the new password
4. Test the connection again
This is particularly common with Supabase and other hosted databases.
Advanced notes
Connection Pooling: For serverless environments like Vercel or AWS Lambda, use a connection pooler like PgBouncer or Prisma Data Proxy (now Prisma Accelerate) to manage connection lifecycles efficiently. Set connection_limit=1 in serverless functions to prevent connection exhaustion.
Prisma Accelerate: If you frequently encounter P1001 errors with serverless databases, consider using Prisma Accelerate, which provides built-in connection pooling and query caching designed for serverless architectures.
IPv6 vs IPv4: Some hosting providers default to IPv6. If your database only supports IPv4, you may need to force IPv4 resolution or update your database configuration to accept both protocols.
Docker Networking: When running Prisma in a Docker container, use the service name from docker-compose.yml as the host instead of localhost. For example: postgresql://user:pass@postgres:5432/db where postgres is your database service name.
Schema Specification: For PostgreSQL, always specify the schema parameter in your connection string (e.g., ?schema=public) to avoid connection issues with multi-schema databases.
Maximum Connections: Database servers have connection limits. PostgreSQL defaults to 100 concurrent connections. If your application uses connection pooling aggressively or you have multiple instances running, you may hit this limit. Use SHOW max_connections; to check your limit and adjust accordingly.
Frequently asked questions
- What does "P1001" mean?
- The P1001 error indicates that Prisma Client is unable to reach your database server at the specified host and port. This is a connection-level failure that occurs before any authentication or query execution attempts. This error commonly appears when using serverless database providers like Neon, Supabase, or PlanetScale, where databases may enter an idle state after periods of inactivity.
- How do I fix "P1001"?
- Prisma cannot establish a connection to your database server. This typically occurs due to incorrect connection strings, network issues, firewall restrictions, or serverless databases in idle state.
- What causes "P1001"?
- Incorrect DATABASE_URL connection string format or credentials Serverless database in idle state requiring time to wake up (Neon, Supabase) Database server is not running or has crashed
- Is the "P1001" 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)
- ๐prisma.io - database troubleshooting(Official Docs)
- ๐prisma.io - databases connections(Official Docs)
- ๐github.com - 23589(GitHub)
- ๐github.com - 21666(GitHub)
- ๐supabase.com - prisma troubleshooting(Official Docs)
- ๐
- ๐github.com - 5652(GitHub)
- ๐github.com - 16816(GitHub)
- ๐prisma.io - connection to db timed out(Official Docs)