SaaS architecture
Connection refused: is the PostgreSQL server running?
psql's Connection refused, measured on PostgreSQL 17: what the message means, how it differs from timeouts and DNS errors, and the Docker cases behind it.
Now-Next builds SaaS products and takes over existing ones. What we build
psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused, followed by Is the server running on that host and accepting TCP/IP connections?, is not a PostgreSQL server error and has no server SQLSTATE: it comes from libpq, the client library, before any server has said a word. The operating system answered the connection attempt with “nothing is listening on this address and port”. The obvious cause is that no server is listening on that port. In Docker setups the server is often running, just at a different address than the one the client uses.
We measured this with psql 10.23 to 18.6 against PostgreSQL 17.11 in the official postgres Docker images, on 22 October 2026. One detail for anyone reproducing it: the postgres:12 image ships libpq 17.2, so its psql prints the new text; we needed postgres:10-alpine (libpq 10) to get the old wording.
What does “Connection refused” mean in psql?
Nothing accepted a TCP connection on that address and port. With no server on port 55528:
psql: error: connection to server at "localhost" (127.0.0.1), port 55528 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
psql 12.22 to 18.6 printed exactly this and exited with code 2. The message comes from libpq, not from psql, and these images ship libpq 17.2 (postgres:12), 18.1 (postgres:13) or 18.6 (14 to 18). Only psql 10.23 with libpq 10 printed the older wording, which you will find in older forum threads; according to the libpq source (fe-connect.c), the wording changed in libpq 14:
psql: 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 55528?
Same meaning. Neither version reached a server, so nothing about users or passwords has been checked yet; errors such as password authentication failed or role does not exist only appear once the connection itself works.
Is it refused, a timeout, or a host name problem?
The word after failed: tells you which. These are the texts we got:
| What we did | Exact message (psql 17.11, libpq 18.6) | What it means |
|---|---|---|
| nothing listening on the port | … port 55528 failed: Connection refused | host reachable, no server on that address and port |
non-routable address, connect_timeout=3 | connection to server at "10.255.255.1", port 5432 failed: timeout expired (after 3.2 s) | no answer at all (we measured a non-routable address); typical causes are a firewall that drops packets or the wrong network |
| unknown host name | could not translate host name "nn6cr-nonexistent-host" to address: Temporary failure in name resolution | DNS: the name does not resolve where the client runs |
no -h, no server in that container | connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory | psql used the Unix socket, and nothing created it |
| port published by Docker, server not accepting yet | … failed: server closed the connection unexpectedly | something accepted the connection and then closed it — in our case the Docker port mapping |
A refusal is a definite answer from the host; a timeout means the packets got no answer at all, so look at the network rather than at PostgreSQL. If the connection does get through but the server is full, you get a server error instead, covered in remaining connection slots are reserved.
Why does the message mention both ::1 and 127.0.0.1?
Because localhost resolved to two addresses and libpq tried both. Inside a regular container, /etc/hosts had both 127.0.0.1 localhost and ::1 localhost, and psql reported each failure:
psql: error: connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
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 10.23 did the same in its old wording. The PostgreSQL documentation describes this: when “a single host name is translated to multiple addresses, all the hosts and addresses will be tried in order, until one succeeds”. Two lines are one problem, reported per address. On our host, where localhost maps only to 127.0.0.1, there was one line.
Why is the connection refused when PostgreSQL runs in Docker?
Because the server is reachable at a different address than the one the client uses. We measured four setups:
- No
-p. Apostgres:17container without a published port was running and healthy, but nothing listened on the host port. From the host, only the container’s own bridge IP (172.17.0.3:5432) answered. -p 127.0.0.1:55525:5432. Worked from the host, both as127.0.0.1and aslocalhost.- From another container, with
localhost. Refused on::1and127.0.0.1, on 5432 and on the published 55525 alike: inside a container,localhostis that container itself. With the server’s container name on a shared user-defined network (-h nn6cr-pub -p 5432) it worked. The published port 55525 was refused there: containers talk to each other on the container port. On the default bridge network the name did not even resolve:could not translate host name "nn6cr-pub" to address. - Right after
docker run. We polled every ~50 ms from before the container started. The first attempts were refused; once Docker had published the port, one attempt gotserver closed the connection unexpectedly, and the first successful query came about 1.2 s afterdocker run. In 148 logged attempts we did not seethe database system is starting up: the image’s initialisation server listened only on the Unix socket, according to its log. Code that connects at startup should retry refusals, closed connections and57P03(below) for a few seconds.
Why does it say the database system is starting up?
FATAL: the database system is starting up means the connection did reach a running PostgreSQL server, but that server is not ready for queries yet. In our measurements it was replaying WAL after a crash, or it was a standby that had not replayed far enough. Unlike Connection refused, this is a server error with SQLSTATE 57P03, and the fix is usually to wait and retry, not to restart. The exception is a standby with hot_standby = off: it never accepts connections (on 13 it says “starting up” forever), so connect to the primary or turn hot_standby on. On PostgreSQL 15 and later the same wait mostly reads the database system is not yet accepting connections.
We wrote 6.5 GB of WAL into a PostgreSQL 17.11 container with checkpoint_timeout = 1d and max_wal_size = 50GB, so that no checkpoint ran in between, killed it with docker kill -s KILL, started it again and connected every ~0.17 s. Before Docker had published the port the attempts were refused; then psql (16.15, exit code 2) printed:
psql: error: connection to server at "127.0.0.1", port 55850 failed: FATAL: the database system is starting up
psql: error: connection to server at "127.0.0.1", port 55850 failed: FATAL: the database system is not yet accepting connections
DETAIL: Consistent recovery state has not been yet reached.
The first message lasted about 0.15 s, the second the remaining 32 s. The server log shows why:
LOG: database system was interrupted; last known up at 2026-10-27 22:55:23 UTC
FATAL: the database system is starting up
LOG: database system was not properly shut down; automatic recovery in progress
LOG: redo starts at 0/14EF878
LOG: redo in progress, elapsed time: 10.00 s, current LSN: 0/A4596940
LOG: redo done at 1/9A3AC600 system usage: CPU: user: 24.95 s, system: 5.16 s, elapsed: 30.12 s
LOG: checkpoint starting: end-of-recovery immediate wait
LOG: database system is ready to accept connections
Redo took 30.1 s and the end-of-recovery checkpoint 2.1 s; the first query succeeded about 32.7 s after docker start. With log_error_verbosity = verbose the log lines read FATAL: 57P03: the database system is starting up and FATAL: 57P03: the database system is not yet accepting connections. pg_isready printed rejecting connections with exit code 1 for the whole period, no response (exit 2) before the port was open, and accepting connections (exit 0) afterwards.
Which message you get depends on the version and on the situation. We measured three situations:
| Situation | 13.23 | 14.24 | 15.19–18.6 |
|---|---|---|---|
| crash recovery (redo after a kill) | starting up | starting up | not yet accepting |
| standby before a consistent state | starting up | not yet accepting | not yet accepting (17.11) |
standby with hot_standby = off | starting up | not accepting | not accepting (17.11) |
“Not yet accepting” is the message with DETAIL: Consistent recovery state has not been yet reached. above. “Not accepting” is FATAL: the database system is not accepting connections with DETAIL: Hot standby mode is disabled., and it does not go away: the documentation describes hot_standby as whether “you can connect and run queries during recovery”, and a standby stays in recovery. On 17.11 and 18.6 we saw “starting up” for a single attempt, before the log said automatic recovery in progress; on 15.19 and 16.15 not at all. On 13.23 and 14.24 it lasted the whole redo (21.6 s and 11.8 s for about 2.6 GB of WAL). The reason is in the source: from 15 on, the startup process also signals the postmaster that redo has begun during crash recovery, not only on a standby (a side effect of running the checkpointer during crash recovery, PostgreSQL 15 release notes). The standby that was not yet consistent was a base backup taken while the primary was writing: on 17.11 it had to replay 4.0 GB of WAL, and connections were rejected with “not yet accepting” for 31.3 s until consistent recovery state reached. All of these are SQLSTATE 57P03. A fourth situation, measured on 17.11: when a single backend was killed with kill -9, as the OOM killer does, new connections got FATAL: 57P03: the database system is in recovery mode and the other open session got server closed the connection unexpectedly; PostgreSQL restarted inside the container without a full redo wait, and PID 1 kept running.
During a smart shutdown (SIGTERM) with a session still open, new connections got FATAL: 57P03: the database system is shutting down on 14.24, 17.11 and 18.6, while the open session finished its query normally. docker stop does not do that: the official images set SIGINT as their stop signal, a fast shutdown, so the open session got FATAL: 57P01: terminating connection due to administrator command and the container stopped in 0.38 s.
What helps:
- Wait, and retry in the client. As in the Docker case above: try again after a second, with an upper limit, until the server accepts. Redo started at the checkpoint we had forced before writing (
0/14EF878), so its length is set by the WAL written since the last checkpoint. - Do not restart during the redo. We killed an 18.6 container again after 7 s of redo; the next start logged
database system was interrupted while in recoveryand began again at the sameredo starts at 0/A7240D88. The 7 s were lost. That includesdocker stop,docker compose restartand orchestrators: after its timeout (10 s by default)docker stopsendsSIGKILL. - Use
pg_isreadyin a healthcheck, with a start period. With--health-cmd 'pg_isready -U postgres -h 127.0.0.1' --health-interval 1s --health-retries 3and no start period, a container in a 9.7 s redo went fromstartingtounhealthyafter 3.4 s and tohealthyafter 11.0 s. With a start period, for example--health-start-period 120s, it stayedstartingand becamehealthyafter 15.5 s, without passing throughunhealthy. In Compose these are thehealthcheck:keys, anddepends_onwithcondition: service_healthywaits for that status; we measured the healthcheck withdocker run, not Compose itself. Use this for readiness (depends_on, a readiness probe), not for anything that restarts the container on failure, such as a Kubernetes liveness probe: every restart begins the redo again, and a redo longer than the probe’s patience turns into a restart loop.
Can listen_addresses cause Connection refused?
Yes. listen_addresses decides which interfaces the server listens on, and according to the documentation its default is localhost, “which allows only local TCP/IP ‘loopback’ connections to be made”. The official Docker image sets it to * — show listen_addresses returned * in our container — which is why the Docker cases above worked at all.
We started a container with -c listen_addresses=localhost and -p 127.0.0.1:55526:5432. The log said listening on IPv6 address "::1" and listening on IPv4 address "127.0.0.1", and inside the container psql -h localhost worked. From outside:
-- from the host, through the published port:
psql: error: connection to server at "127.0.0.1", port 55526 failed: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
-- from the host, to the container's own IP:
psql: error: connection to server at "172.17.0.4", port 5432 failed: Connection refused
So through Docker’s port mapping this misconfiguration does not even say “refused”: Docker’s proxy process (docker-proxy) accepts the connection on the host port, cannot reach the server on the container’s address, and closes it. A client that connected to the container’s own address got the plain refusal; set listen_addresses to the address the clients reach, and allow them in pg_hba.conf. If that file has no line for them, the server does answer, with a different error: no pg_hba.conf entry for host, which names the address the server saw. For the full list of codes a server does send once you are connected, see our measured SQLSTATE overview.
What we did not measure
The SQLSTATE that drivers attach to this client-side failure (we had no driver such as psycopg installed). For 57P03: PostgreSQL 12 and older, standbys on 15, 16 and 18, Docker Compose’s depends_on behaviour, and how drivers and connection pools retry it. Docker Desktop on macOS and Windows, firewalls that reject rather than drop, Windows and macOS installers, and pgAdmin or DBeaver — which connect over the same TCP mechanism, so when they run in a container, localhost is that container, not the database.
Sources: PostgreSQL 17 documentation on connection settings (listen_addresses) and libpq connection strings (multiple addresses), read on 22 October 2026, and the libpq source (src/interfaces/libpq/fe-connect.c in the PostgreSQL repository, branches REL_13_STABLE and REL_14_STABLE, read on the same day); hot standby, replication settings (hot_standby), shutting down the server and pg_isready, and the PostgreSQL 15 release notes, read on 28 October 2026; the server source (postmaster.c in REL_13_STABLE, REL_14_STABLE and REL_15_STABLE, xlog.c in REL_14_STABLE, xlogrecovery.c in REL_15_STABLE), read on 27 October 2026. All measurements are our own, on PostgreSQL 17.11 and psql 10.23–18.6 with libpq 10, 17.2, 18.1 and 18.6 in containers, on 22 October 2026; the startup and shutdown messages on PostgreSQL 13.23, 14.24, 15.19, 16.15, 17.11 and 18.6 with psql and pg_isready 16.15, on 27 and 28 October 2026.