psql: error: connection to server at "localhost" (::1), port 15299 failed: Connection refused (0x0000274D/10061) — nothing is listening on that host and port. The server or its container is stopped, or psql was given the wrong port. Start it, or point psql at the port it really uses.

Two commands tell you which: docker ps lists running containers with their published ports, and netstat -ano | findstr :15299 (with your port) prints a LISTENING line only when something listens there.

The error

PS> & 'C:\Program Files\PostgreSQL\18\bin\psql.exe' -h localhost -p 15299 -U postgres
psql: error: connection to server at "localhost" (::1), port 15299 failed: Connection refused (0x0000274D/10061)
	Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 15299 failed: Connection refused (0x0000274D/10061)
	Is the server running on that host and accepting TCP/IP connections?

The same mistake in WSL reads a little differently, and without -h it names the Unix socket instead of a TCP port:

$ psql -h localhost -p 15299 -U postgres
psql: error: connection to server at "localhost" (127.0.0.1), port 15299 failed: Connection refused
	Is the server running on that host and accepting TCP/IP connections?
$ psql -p 15299
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.15299" failed: No such file or directory
	Is the server running locally and accepting connections on that socket?

Why it happens

The machine answered, but no program had that port open, so the connection was refused at once. 0x0000274D is 10061 in hexadecimal, the Windows socket code for a refused connection. On Windows, localhost gave psql two addresses, IPv6 ::1 and IPv4 127.0.0.1; it tried both and printed both failures. Each attempt took about two seconds here: 4.1 s for the pair, 2.1 s with -h 127.0.0.1.

A stopped container gives exactly the same message. After docker stop, the container dropped out of docker ps, its port disappeared from netstat, and psql on that port was refused.

The fix

PS> docker ps -a --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" --filter name=fix6-
NAMES       STATUS                              PORTS
fix6-pg18   Exited (0) Less than a second ago
PS> docker start fix6-pg18
fix6-pg18
PS> docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" --filter name=fix6-
NAMES       STATUS         PORTS
fix6-pg18   Up 4 seconds   127.0.0.1:15460->5432/tcp
PS> netstat -ano | findstr LISTENING | findstr :15460
  TCP    127.0.0.1:15460        0.0.0.0:0              LISTENING       43908
PS> & 'C:\Program Files\PostgreSQL\18\bin\psql.exe' -h localhost -p 15460 -U postgres -c "SELECT 1 AS ok;"
 ok
----
  1
(1 row)

The PORTS column is the answer to "which port": the number left of the arrow goes after -p. In WSL, pg_lsclusters shows the port and the status of each cluster, and sudo systemctl start postgresql@16-main started the stopped one again. A wrong port is not always refused: on a PC that also runs PostgreSQL on 5432, a connection string left on 5432 reaches that other server and fails with a password error instead, as shown in LocalDB, Express and PostgreSQL in Docker.

How it was reproduced

Windows 11 with psql 18.3 from PostgreSQL 18 for Windows, whose own service on 5432 was left alone; port 15299 had nothing on it, and a postgres:18 container (PostgreSQL 18.4) published on 127.0.0.1:15460 was stopped and started with Docker 29.9.2. The WSL lines came from a throwaway Ubuntu 24.04.5 distribution with psql 16.15, whose cluster on port 15201 was stopped and started with systemctl. The names fix6-pg18 and fix6-pg are this test's own; both were removed afterwards.

Frequently asked

What does Connection refused (0x0000274D/10061) mean in psql?
psql reached the machine, but nothing was listening on that port, so the connection was refused at once. 0x0000274D is 10061 in hexadecimal, the Windows socket code for a refused connection. Start the server or container, or use the port it really listens on.
How do I check if PostgreSQL is running on Windows?
For a container, docker ps lists running containers with their published ports, while a stopped one appears only in docker ps -a. For any server, netstat -ano piped to findstr with your port shows a LISTENING line when something listens there. A Windows install also shows up in Get-Service postgresql*.
Why does psql try both ::1 and 127.0.0.1 for localhost?
On Windows, localhost gave psql the IPv6 and the IPv4 loopback address, and psql tried each in turn, printing every failure only because all of them failed. In WSL only 127.0.0.1 was tried in this test.

More decoded errors in the Fixes category, including psql is not recognized for when Windows cannot find psql at all.