Error Encyclopedia

Connection Refused Error

Fix 'ECONNREFUSED' and 'Connection refused' errors when connecting to databases, APIs, or other services.

What Does This Error Mean?

The 'Connection refused' error (ECONNREFUSED) means the operating system actively rejected a connection attempt to a specific IP address and port. This typically happens when no service is listening on that port, or a firewall is blocking the connection.

Common Causes

1

The target service (database, API, server) is not running

2

Wrong port number in the connection string

3

Service is listening on a different network interface (e.g., 127.0.0.1 instead of 0.0.0.0)

4

Firewall rules are blocking the connection

5

Docker container port is not mapped to the host

6

Service crashed or was stopped and not restarted

How to Fix It

Check if the service is running

Verify the service is running and listening on the expected port.

# Check if process is listening on port
netstat -an | findstr :5432
# or
netstat -tulpn | grep 5432

# Check if the service is running
systemctl status postgresql
# or
pg_isready

Verify the connection string

Check that the host, port, and other connection parameters are correct.

# Example connection strings
postgresql://user:pass@localhost:5432/mydb      # PostgreSQL
mysql://user:pass@localhost:3306/mydb            # MySQL
mongodb://localhost:27017/mydb                   # MongoDB
redis://localhost:6379                           # Redis

Test connectivity

Use telnet or nc to test if the port is reachable.

# Test TCP connection
telnet localhost 5432
# or
nc -zv localhost 5432

# Test from another machine
nc -zv server-hostname 5432

Related Tools

Use these tools to debug and fix this error:

Related Errors

Other common errors in this category:

Frequently Asked Questions

What is the difference between connection refused and timeout?

Connection refused (ECONNREFUSED) means the server actively rejected the connection — the port is reachable but nothing is listening. Timeout (ETIMEDOUT) means the server did not respond at all, often due to firewall or network issues.

Why does localhost work but 127.0.0.1 doesn't?

Some applications bind to IPv6 (::1) by default. Use 'localhost' which resolves to both IPv4 and IPv6, or check if the service is listening on IPv6 only with `netstat -an`.