PostgreSQL: connection refused

psql: error: connection to server at "db.example.com" (10.0.0.12), port 5432 failed: Connection refused
        Is the server running on that host and accepting TCP/IP connections?

This error is not from PostgreSQL — it never saw the connection. "Connection refused" is the operating system on the target host saying that nothing is listening on that address and port (or a firewall actively rejected the packet). That distinction immediately narrows the search.

What this error means

Three failure modes at connect time look similar but mean different things:

SymptomMeaning
Connection refusedTCP reached the host, nothing listening on that port (or firewall REJECT). PostgreSQL not involved.
Connection timeoutPackets silently dropped — typically a firewall or security group, or wrong host.
no pg_hba.conf entry for host ...You did reach PostgreSQL; now it's an authentication-config problem, a different page of the manual.

So "Connection refused" means: wrong place, wrong port, or no server running there.

Common causes

How to diagnose it

From the client:

pg_isready -h db.example.com -p 5432

On the server host:

# Il servizio gira?
systemctl status postgresql

# Qualcosa ascolta, e su quale indirizzo/porta?
ss -lntp | grep -E '5432|postgres'

If the service is down, the reason for the failed start is in the logs (journalctl -u postgresql or the PostgreSQL log directory) — a typo in postgresql.conf after an edit is a classic. If it's running but bound to 127.0.0.1 only, it's listen_addresses.

How to fix it