On This Page
Everyone's first read of a PostgreSQL connection refused error is "the database is down". Mine too. Say your FX rates loader fails its morning run with it, and the channel fills up with people suggesting a restart. Before anyone types systemctl restart, check. Very often Postgres is up, has been up for days, and is listening on 127.0.0.1 only, while the loader now runs on a different box.
That's this error in one story. "Refused" sounds like PostgreSQL said no. It didn't. PostgreSQL never got a word in.
What the client printed
This is what psql 14 and later prints (I captured this with psql 16.15 against a PostgreSQL 16.15 server, lc_messages = 'C'):
psql: error: connection to server at "localhost" (127.0.0.1), port 5433 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
When you connect by IP address instead of a host name, libpq drops the bit in parentheses, because there's nothing to resolve:
psql: error: connection to server at "192.0.2.2", port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
If your client is linked against libpq 13 or older, the same failure reads differently. This wording comes straight from fe-connect.c on the REL_13_STABLE branch, not from my lab:
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
Both say the same thing. On Windows you'll often see the Winsock code tacked on: Connection refused (0x0000274D/10061). And from Node you get the operating system's word for it, no Postgres text at all:
Error: connect ECONNREFUSED 127.0.0.1:5432
code=ECONNREFUSED address=127.0.0.1 port=5432 syscall=connect
(That one's from a bare net.connect in Node 22.23; node-postgres hands you the same socket error, because it never got far enough to wrap it in anything.) Python with psycopg2 or psycopg 3 shows the libpq text inside an OperationalError. Java's pgJDBC has its own message and its own article: PSQLException: Connection to localhost:5432 refused.
Notice what's missing from every one of these. No FATAL:, no ERROR:, no SQLSTATE. That absence is the most useful fact you have.
How far did the connection get?
Not far. Your client resolved the host name, picked an address, sent a TCP SYN to that address and port, and the operating system on the other end answered with a reset. Nothing was listening there. The postmaster (PostgreSQL's parent process, the one that accepts connections and forks a backend per client) never saw the attempt, so it never wrote anything to its log and never got to check pg_hba.conf or your password.
So a refused connection only has a few possible stories. Nothing is listening on that port at all, because the server is stopped or crashed or on another port. Or something is listening on that port, but on a different address than the one you hit. Or you're not talking to the machine you think you are (a container's own localhost, a VPN, a stale DNS record). A firewall that actively rejects instead of dropping can also produce it, but that's rarer than people assume; most cloud security groups drop silently, which gives you a timeout instead.
Two minutes of triage, run from the machine where the client runs:
pg_isready -h localhost -p 5432 # exit 0 = accepting, 1 = rejecting (starting up), 2 = no response, 3 = bad parameters
nc -vz localhost 5432 # is anything at all listening there?
env | grep '^PG' # PGHOST / PGPORT quietly override what you think you're connecting to
And on the database host itself (needs a shell there, no database privileges):
ss -ltnp | grep 5432 # or: lsof -nP -iTCP:5432 -sTCP:LISTEN on macOS
That ss line tells you which address the server bound. 127.0.0.1:5432 means local clients only. 0.0.0.0:5432 or *:5432 means every IPv4 interface. No line means nothing is there.
If pg_isready says accepting connections but your app still gets refused, stop looking at Postgres. Your app is connecting somewhere else. An exported PGPORT=5433 left over from a different project is a classic, and it doesn't show up anywhere in your app's config. Here's that exact trap, with a server happily running on 5432:
$ PGPORT=5433 psql -h localhost -U postgres -d postgres -c 'select 1'
psql: error: connection to server at "localhost" (127.0.0.1), port 5433 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
Make it fail on demand
You don't need a shared database for any of this, and you shouldn't use one. The lab below runs a throwaway cluster in a scratch directory. I ran it on Ubuntu 24.04 with the distro's PostgreSQL 16.15 binaries (/usr/lib/postgresql/16/bin); the same commands work with Homebrew's postgresql@16 or any PGDG package. I couldn't pull the official Docker image in the sandbox I was working in, so the Docker variant at the end is commands only.
Step 1. Create and start a disposable cluster. It listens on the default listen_addresses = 'localhost'.
B=/usr/lib/postgresql/16/bin
echo labpass > ~/pw
$B/initdb -D ~/pglab -U postgres --auth=scram-sha-256 --pwfile=$HOME/pw
$B/pg_ctl -D ~/pglab -l ~/pglab.log -o "-c unix_socket_directories=$HOME -c lc_messages=C" start
export PGPASSWORD=labpass
Step 2. Confirm what it bound.
$ grep listening ~/pglab.log
LOG: listening on IPv4 address "127.0.0.1", port 5432
LOG: listening on Unix socket "/home/pglab/.s.PGSQL.5432"
Step 3. Wrong port. The server is fine; you knocked on 5433.
$ psql "host=localhost port=5433 user=postgres dbname=postgres" -c 'select 1'
psql: error: connection to server at "localhost" (127.0.0.1), port 5433 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
Step 4. Right port, wrong address. Use any non-loopback IP of the same machine (hostname -I shows them; mine was 192.0.2.2). The server is listening on 5432, only not on that interface.
$ psql "host=192.0.2.2 port=5432 user=postgres dbname=postgres" -c 'select 1'
psql: error: connection to server at "192.0.2.2", port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
This is the "works from the box, refused from everywhere else" case. Same message as step 3, completely different cause.
Step 5. Server stopped. Now both TCP and the Unix socket fail, and they fail differently:
$ pg_ctl -D ~/pglab stop -m fast
$ psql "host=localhost user=postgres dbname=postgres" -c 'select 1'
psql: error: 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?
$ psql "host=$HOME user=postgres dbname=postgres" -c 'select 1'
psql: error: connection to server on socket "/home/pglab/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?
The socket version has its own article: .s.PGSQL.5432: No such file or directory.
How you know you reproduced the right thing: the error appears instantly (my runs returned in about 30 ms) and there's no FATAL: anywhere in it. If instead you wait and then get timeout expired, the packets were dropped, not refused, and you're in firewall territory. If you get a FATAL: line, you got further than this article; see the comparison in connection refused vs pg_hba vs password failed.
Teardown: pg_ctl -D ~/pglab stop -m fast if it's still running, then rm -rf ~/pglab ~/pglab.log ~/pw. Nothing else was touched.
Docker variant (not run here). docker run -d --name lab-pg -e POSTGRES_PASSWORD=labpass postgres:19 with no -p flag, then psql -h localhost -U postgres from the host. Refused, because the container's 5432 was never published to the host. Add -p 5432:5432 and it connects (or -p 5433:5432 and you must use port 5433). Remove it with docker rm -f lab-pg.
The short answer
Nothing was accepting connections at the host and port your client used. Either the server isn't running, it's on another port, it isn't listening on the address you reached, or you reached a different machine than you meant to. PostgreSQL itself didn't reject anything.
Where the message comes from
Since PostgreSQL never saw the attempt, the text is entirely libpq's, the C client library that psql, psycopg, Ruby's pg gem and plenty of others sit on. In fe-connect.c, when connect() fails, libpq prints connection to server at "%s" (%s), port %s failed: followed by the operating system's error string (Connection refused is the C library's strerror(ECONNREFUSED)), then appends the hint about whether the server is running. PostgreSQL 14 reworked this so every failed address attempt gets its own connection to server at prefix, which matters once a host name resolves to more than one address: libpq tries each one in turn and reports every failure, one after another. Before 14, the hint text carried the host and port instead.
That multiple-address behaviour is the trap I see most on laptops. On many systems localhost resolves to both ::1 and 127.0.0.1. If the server only bound one of them, libpq quietly falls through to the other and connects, but a client that only tries the first address (some drivers, some health checks) gets refused. My sandbox only had IPv4 for localhost, so I couldn't show the two-line version, but getent ahosts localhost (or dscacheutil -q host -a name localhost on macOS) tells you what yours resolves to.
The server side is listen_addresses. It's a postmaster-context setting: read once at startup, and the docs are blunt that it can only be set at server start. Default is localhost. I proved the restart rule to myself the slow way. I ran ALTER SYSTEM SET listen_addresses = '*', reloaded, retried from the other IP, and got refused again. I was annoyed enough to check whether ALTER SYSTEM had even written the file. It had. The answer was in two places I hadn't looked:
LOG: received SIGHUP, reloading configuration files
LOG: parameter "listen_addresses" cannot be changed without restarting the server
SELECT name, setting, pending_restart FROM pg_settings WHERE name = 'listen_addresses';
-- listen_addresses | localhost | t
After a restart the log said listening on IPv4 address "0.0.0.0", port 5432, and the remote attempt finally failed with a different error:
psql: error: connection to server at "192.0.2.2", port 5432 failed: FATAL: no pg_hba.conf entry for host "192.0.2.2", user "postgres", database "postgres", no encryption
That's progress, and it's the surprise worth remembering: fixing listen_addresses almost never makes remote connections work on its own. It moves you one layer down, to no pg_hba.conf entry. The default pg_hba.conf from initdb only has rules for local and loopback connections.
Docker's official image flips this around. Its Dockerfile rewrites the sample config to listen_addresses = '*', so inside a running container Postgres listens on every interface already. When a container gets refused, it's almost never listen_addresses. It's port publishing, or the app container using localhost (which inside a container means the container itself, not the db service), or timing: during first-time init the entrypoint starts a temporary server with listen_addresses='', socket only, so for a few seconds TCP really is refused. That last one belongs to the Compose healthcheck article.
Fixing it properly
Work out which story you're in first, then change exactly one thing.
| What you found | Cause | Fix |
|---|---|---|
pg_isready says no response, nothing in ss |
server not running | start it, then read the server log for why it stopped |
ss shows a different port |
wrong port (second cluster, custom port, Homebrew vs Postgres.app) |
point the client at the real port; don't move the server |
ss shows 127.0.0.1:5432, client is remote |
listen_addresses too narrow |
set it, restart, then add a pg_hba.conf rule |
| server fine, client still refused | PGHOST/PGPORT, wrong DNS, wrong container |
fix the client's connection string |
| Docker, host can't connect | port not published | -p 5432:5432 or ports: in Compose |
| Docker, app container can't connect | localhost inside the container |
use the service name, e.g. host=db |
For the listen_addresses case, set the specific address you need rather than everything:
# postgresql.conf (or ALTER SYSTEM, which writes postgresql.auto.conf)
listen_addresses = 'localhost,10.20.0.15' # PG 16; default 'localhost'; context postmaster = restart required
port = 5432 # default 5432; also restart-only
Then restart (systemctl restart postgresql, pg_ctl restart, brew services restart postgresql@16) and check pending_restart is back to false. A restart drops every session, so do it in a window.
'*' is fine on a box with one private interface behind a firewall. It's a bad habit on anything with a public address, because now the only thing between the internet and your login prompt is pg_hba.conf. And please don't follow it with the forum special, host all all 0.0.0.0/0 trust. That turns "connection refused" into "anyone can connect as anyone, no password". Add a rule for the client's actual subnet with scram-sha-256.
Also not a fix: restarting the server when it was running fine. If pg_isready said accepting connections, a restart changes nothing except who gets paged next.
How not to end up here again
The refused connections that eat the most time usually come from configuration living in too many places: a PGPORT in a shell profile, a localhost default baked into an app's config, a Compose file publishing 5433 because someone's laptop already had Postgres on 5432. Pick one source per environment and make it explicit, every parameter spelled out, nothing left to defaults:
# one connection definition per environment, no defaults guessed
DB_URL=postgresql://fx_loader@db.internal:5432/fxrates?sslmode=verify-full&connect_timeout=5
If you're juggling formats, the connection string formats comparison covers which client reads which. And keep the database's bind address and the firewall rule in the same change, reviewed together, so nobody opens one without the other.
For containers, have the app wait for readiness instead of racing it, and retry the first connection for a bounded time. A refused connection during the first ten seconds of a deploy is normal. A refused connection after a minute is a page.
Catching it before production does
Run pg_isready -h <host> -p <port> from where the client lives, not from the database host, as your readiness probe and in CI before integration tests. Alert on the exit code, not on log text. If you scrape PostgreSQL with an exporter, an absent up metric for more than a minute is the same signal earlier.
Integration tests against a Testcontainers PostgreSQL at your production major version catch most "works on my laptop" port and host mistakes, because the test has to use the mapped port the container hands back.
Version notes: libpq changed the message format in 14 (the connection to server at … failed: prefix and the per-address reporting). listen_addresses defaults to localhost and requires a restart in every supported version, 15 through 19, the current major as of November 2026. The official Docker image's 18 Dockerfile, the one I read, sets '*'. After any upgrade, grep the new server log for listening on before you blame anything else.
What I actually checked for this one: the message in libpq's source on the 13 and 19 branches, a live 16.15 server for every variant above, the reload-versus-restart behaviour, and the Docker image's Dockerfile and entrypoint. Most of the effort went into convincing myself the reload wasn't silently working. It wasn't.
Screenshot this bit
- No
FATAL:and no SQLSTATE means PostgreSQL never saw you. Look at the network path, not at grants or passwords. pg_isreadyfrom the client's machine, thenss -ltnp | grep 5432on the server, settles most cases in two minutes.listen_addressesneeds a restart, not a reload. Checkpending_restart.- Fix
listen_addressesand you'll usually hitno pg_hba.conf entrynext. That's the right next error. - In Docker, refused is almost always publishing or
localhostinside a container.
Questions people ask about this
Why can I connect with psql on the server but my app gets connection refused? psql on the server probably used the Unix socket or 127.0.0.1, while your app comes in over the network. If the server listens on localhost only (the default), remote TCP is refused. Set listen_addresses to include the server's network address, restart, and add a pg_hba.conf rule for the app's subnet.
How do I fix ECONNREFUSED 127.0.0.1:5432 in Node.js? It's the same failure, reported by Node's socket layer before node-postgres can do anything. Check that a server is listening on that port with pg_isready, and check that your app's host isn't localhost while running inside a container. In Compose, use the database service's name as the host.
Does connection refused mean my password is wrong? No. Password problems come back as FATAL: password authentication failed, which means the server answered. Refused means nothing answered on that address and port. See password authentication failed for that one.
*Is it safe to set listen_addresses = ''? It's safe only when something else limits who can reach the port (a private network, a firewall, a security group) and pg_hba.conf requires passwords. It makes PostgreSQL listen on every interface, including public ones. Prefer listing the specific addresses you need.