Fix "Error: connect ECONNREFUSED 127.0.0.1:port" in Node.js & Databases
The target host IP and port actively rejected the TCP handshake. This means no service or database is listening on that port, or Node is attempting IPv6 (::1) while the service listens on IPv4 (127.0.0.1).
Error: connect ECONNREFUSED 127.0.0.1:5432 / :::3000
Quick Fix Checklist
- Verify target database is running: sudo systemctl status postgresql (or docker ps)
- Change "localhost" to "127.0.0.1" in your connection string to force IPv4
- Inside Docker containers: Use "host.docker.internal" instead of "localhost" to contact host machine