Postgres: connection refused / could not connect to server (psycopg2, pg, Prisma)
#147Error
psycopg2.OperationalError: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused. Is the server running on that host and accepting TCP/IP connections? (also ECONNREFUSED 127.0.0.1:5432, Prisma P1001)
Why it happens, and why this works
Connection refused means nothing accepted the TCP connection at that host:port: the server is stopped, the host name points at the wrong machine (localhost inside a container is the container itself), or it's on another port. Pointing the client at the address where Postgres actually listens fixes it.
Fix
Something isn't listening where you're connecting; find which part is wrong. 1. Is Postgres running? docker ps (start the container) or pg_isready -h localhost -p 5432. 2. Inside Docker Compose, the app must use the service name as host (e.g. postgres://[REDACTED]@db:5432/app), not localhost. 3. Hosted DB (Supabase, Neon, RDS): use the host from the dashboard, add ?sslmode=require, and for serverless use the pooler URL/port they give (Supabase pooler 6543). 4. Docker on Mac/Windows connecting to a DB on your machine: host.docker.internal. 5. Check the port isn't taken by another Postgres (lsof -i :5432 / netstat -ano | findstr 5432).
Did it work?
No reports yetRelated fixes
Error: connect ECONNREFUSED 127.0.0.1:5432 (Postgres)
#159Error: connect ECONNREFUSED 127.0.0.1:5432
MCP stdio server fetches its health helper at initialize so a blocked mirror stalls the handshake into a connection closed error
#188McpError: MCP error -32000: Connection closed (server answered initialize but the helper fetch inside the init path never returned and the client transport gave up)
Prisma on Vercel: outdated Prisma Client because Prisma's auto-generation isn't triggered
#129Prisma has detected that this project was built on Vercel, which caches dependencies. This leads to an outdated Prisma Client because Prisma's auto-generation isn't triggered. To fix this, make sure to run the `prisma generate` command during the build process.