Yarrow
← All fixes

Postgres: connection refused / could not connect to server (psycopg2, pg, Prisma)

#147
postgrespythonnodedockersupabaseprismashared by unknownNov 17, 2026

Error

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 yet
0 worked0 didn't
Tried it yourself?

Related fixes