ServerHandbook live check: run log ------------------------------------------------------------------------------ Check scripts/live/checks/postgresql-connection-refused-ubuntu.mjs (sha256 df50517456912fa02127fa6ec79bf23e4e09e02fa5c3973815aeb01765def62b) Ran at 2026-11-19T17:42:09Z (118 s) Result PASS, all 28 claims held Tested on System Ubuntu 26.04.1 LTS Kernel 7.0.0-43-generic Package postgresql 18+290ubuntu1 Package postgresql-common 290ubuntu1 Package postgresql-18 18.6-0ubuntu0.26.04.1 Package postgresql-client-18 18.6-0ubuntu0.26.04.1 Setup A fresh Ubuntu 26.04 server (a VM from Ubuntu’s cloud image) with PostgreSQL 18 from Ubuntu’s archive, a role app with a password (in ~/.pgpass) and its database appdb, reached over SSH as the sudo user deploy. The connections from "another machine" go to a second address of the server, 192.0.2.10. Every version above was read from the server at run time. Passwords used on the throwaway test server are shown as [password]. How we test: https://serverhandbook.com/how-we-test ------------------------------------------------------------------------------ What the steps changed on the server (recorded before the first step and after the last) Packages added (0) Packages upgraded (0) Packages removed (0) Services enabled at boot (0) Services disabled (0) Listening sockets opened (2) tcp 0.0.0.0:5432 postgres tcp [::]:5432 postgres Listening sockets closed (0) Files added in /etc (0) Files changed in /etc (1) /etc/postgresql/18/main/pg_hba.conf Files removed from /etc (0) Disk used on / (includes logs written during the run): -0.2 MB ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Claims (28), each with what the run saw ------------------------------------------------------------------------------ 1. ok a bare Ubuntu 26.04 server box (web-server) starts 2. ok (setup) PostgreSQL 18 is installed, cluster 18/main online | Ver Cluster Port Status Owner Data directory Log file | 18 main 5432 online postgres /var/lib/postgresql/18/main /var/log/postgresql/postgresql-18-main.log 3. ok (setup) the server also has the address 192.0.2.10 4. ok (setup) app logs in to appdb over TCP on localhost | 1 5. ok psql: connection to server at "localhost" (::1), port 5432 failed: Connection refused … Is the server running on that host and accepting TCP/IP connections? | 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? 6. ok systemctl status postgresql: active (exited): the umbrella unit says nothing about the cluster | ● postgresql.service - PostgreSQL RDBMS | Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: enabled) | Active: active (exited) since Thu 2026-11-19 17:43:32 UTC; 15s ago 7. ok pg_lsclusters: 18 main 5432 down | Ver Cluster Port Status Owner Data directory Log file | 18 main 5432 down postgres /var/lib/postgresql/18/main /var/log/postgresql/postgresql-18-main.log 8. ok systemctl restart postgresql (restarts every cluster) 9. ok pg_lsclusters: 18 main 5432 online | Ver Cluster Port Status Owner Data directory Log file | 18 main 5432 online postgres /var/lib/postgresql/18/main /var/log/postgresql/postgresql-18-main.log 10. ok psql: the query answers (1 row) | ?column? | ---------- | 1 | (1 row) 11. ok the cluster log: database system is ready to accept connections | 2026-11-19 17:43:48.117 UTC [5345] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" | 2026-11-19 17:43:48.124 UTC [5351] LOG: database system was shut down at 2026-11-19 17:43:45 UTC | 2026-11-19 17:43:48.130 UTC [5345] LOG: database system is ready to accept connections 12. ok another port: port 5432 failed: Connection refused | 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? 13. ok pg_lsclusters: 18 main 5433 online | Ver Cluster Port Status Owner Data directory Log file | 18 main 5433 online postgres /var/lib/postgresql/18/main /var/log/postgresql/postgresql-18-main.log 14. ok ss: postgres listens on 127.0.0.1:5433 and [::1]:5433 | LISTEN 0 200 127.0.0.1:5433 0.0.0.0:* users:(("postgres",pid=5700,fd=7)) | LISTEN 0 200 [::1]:5433 [::]:* users:(("postgres",pid=5700,fd=6)) 15. ok with -p 5433 it works | 1 16. ok the port is set back to 5432 17. ok from the network address: connection to server at "192.0.2.10", port 5432 failed: Connection refused | psql: error: connection to server at "192.0.2.10", port 5432 failed: Connection refused | Is the server running on that host and accepting TCP/IP connections? 18. ok ss: only 127.0.0.1:5432 and [::1]:5432 (localhost) | State Recv-Q Send-Q Local Address:Port Peer Address:Port Process | LISTEN 0 200 127.0.0.1:5432 0.0.0.0:* users:(("postgres",pid=6135,fd=7)) | LISTEN 0 200 [::1]:5432 [::]:* users:(("postgres",pid=6135,fd=6)) 19. ok show listen_addresses: localhost | localhost 20. ok ALTER SYSTEM SET listen_addresses = '*': ALTER SYSTEM | ALTER SYSTEM 21. ok restart (listen_addresses needs a restart, not a reload) 22. ok ss: 0.0.0.0:5432 and [::]:5432 | State Recv-Q Send-Q Local Address:Port Peer Address:Port Process | LISTEN 0 200 0.0.0.0:5432 0.0.0.0:* users:(("postgres",pid=6664,fd=6)) | LISTEN 0 200 [::]:5432 [::]:* users:(("postgres",pid=6664,fd=7)) 23. ok the next error: no pg_hba.conf entry for host "192.0.2.10", user "app", database "appdb" | psql: error: connection to server at "192.0.2.10", port 5432 failed: FATAL: no pg_hba.conf entry for host "192.0.2.10", user "app", database "appdb", SSL encryption | connection to server at "192.0.2.10", port 5432 failed: FATAL: no pg_hba.conf entry for host "192.0.2.10", user "app", database "appdb", no encryption 24. ok pg_hba.conf gets: host appdb app 192.0.2.0/24 scram-sha-256 | host appdb app 192.0.2.0/24 scram-sha-256 25. ok reload (pg_hba.conf needs only a reload) 26. ok from the network address: the query answers (1 row) | ?column? | ---------- | 1 | (1 row) 27. ok (app logs in on localhost at the end of the steps) | 1 28. ok (tested on) the box reports its OS, kernel and the version of 4 package(s) | PRETTY_NAME="Ubuntu 26.04.1 LTS" | VIRT=none | KERNEL=7.0.0-43-generic | PKG postgresql 18+290ubuntu1 | PKG postgresql-18 18.6-0ubuntu0.26.04.1 | PKG postgresql-client-18 18.6-0ubuntu0.26.04.1 | PKG postgresql-common 290ubuntu1 ------------------------------------------------------------------------------ Commands and what they printed (22) ------------------------------------------------------------------------------ $ psql -h localhost -U app -d appdb -c 'select 1' 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? $ systemctl status postgresql --no-pager | head -n 3 ● postgresql.service - PostgreSQL RDBMS Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: enabled) Active: active (exited) since Thu 2026-11-19 17:43:32 UTC; 15s ago $ pg_lsclusters Ver Cluster Port Status Owner Data directory Log file 18 main 5432 down postgres /var/lib/postgresql/18/main /var/log/postgresql/postgresql-18-main.log $ sudo systemctl restart postgresql $ pg_lsclusters Ver Cluster Port Status Owner Data directory Log file 18 main 5432 online postgres /var/lib/postgresql/18/main /var/log/postgresql/postgresql-18-main.log $ psql -h localhost -U app -d appdb -c 'select 1' ?column? ---------- 1 (1 row) $ sudo tail -n 3 /var/log/postgresql/postgresql-18-main.log 2026-11-19 17:43:48.117 UTC [5345] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" 2026-11-19 17:43:48.124 UTC [5351] LOG: database system was shut down at 2026-11-19 17:43:45 UTC 2026-11-19 17:43:48.130 UTC [5345] LOG: database system is ready to accept connections $ psql -h localhost -U app -d appdb -c 'select 1' 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? $ pg_lsclusters Ver Cluster Port Status Owner Data directory Log file 18 main 5433 online postgres /var/lib/postgresql/18/main /var/log/postgresql/postgresql-18-main.log $ sudo ss -tlnp | grep postgres LISTEN 0 200 127.0.0.1:5433 0.0.0.0:* users:(("postgres",pid=5700,fd=7)) LISTEN 0 200 [::1]:5433 [::]:* users:(("postgres",pid=5700,fd=6)) $ psql -h localhost -p 5433 -U app -d appdb -tAc 'select 1' 1 $ sudo sed -i 's/^port = 5433/port = 5432/' /etc/postgresql/*/main/postgresql.conf && sudo systemctl restart postgresql $ psql -h 192.0.2.10 -U app -d appdb -c 'select 1' psql: error: connection to server at "192.0.2.10", port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections? $ sudo ss -tlnp 'sport = :5432' State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 200 127.0.0.1:5432 0.0.0.0:* users:(("postgres",pid=6135,fd=7)) LISTEN 0 200 [::1]:5432 [::]:* users:(("postgres",pid=6135,fd=6)) $ sudo -u postgres psql -tAc "show listen_addresses" localhost $ sudo -u postgres psql -c "ALTER SYSTEM SET listen_addresses = '*'" ALTER SYSTEM $ sudo systemctl restart postgresql $ sudo ss -tlnp 'sport = :5432' State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 200 0.0.0.0:5432 0.0.0.0:* users:(("postgres",pid=6664,fd=6)) LISTEN 0 200 [::]:5432 [::]:* users:(("postgres",pid=6664,fd=7)) $ psql -h 192.0.2.10 -U app -d appdb -c 'select 1' psql: error: connection to server at "192.0.2.10", port 5432 failed: FATAL: no pg_hba.conf entry for host "192.0.2.10", user "app", database "appdb", SSL encryption connection to server at "192.0.2.10", port 5432 failed: FATAL: no pg_hba.conf entry for host "192.0.2.10", user "app", database "appdb", no encryption $ echo 'host appdb app 192.0.2.0/24 scram-sha-256' | sudo tee -a /etc/postgresql/*/main/pg_hba.conf host appdb app 192.0.2.0/24 scram-sha-256 $ sudo systemctl reload postgresql $ psql -h 192.0.2.10 -U app -d appdb -c 'select 1' ?column? ---------- 1 (1 row) ------------------------------------------------------------------------------ The check's source (scripts/live/checks/postgresql-connection-refused-ubuntu.mjs) As it ran. ------------------------------------------------------------------------------ /** * KB fix: PostgreSQL "Connection refused" on Ubuntu. Break → see → fix → prove (plan04 §1): * 1. the cluster is down while postgresql.service still says "active (exited)": `systemctl start postgresql` does * nothing, pg_lsclusters shows "down", `systemctl restart postgresql` brings it back * 2. the cluster listens on another port (5433, as after an upgrade): pg_lsclusters / ss show it * 3. a connection to the server's network address is refused: listen_addresses is localhost; open it in conf.d; * the next error is pg_hba.conf ("no pg_hba.conf entry"); one host line; the login works * Commands are version-free (/etc/postgresql/*\/main globs, postgresql.service), so both releases share them. * The client is the server itself (a second address, 192.0.2.10, stands in for a remote client). Runs on the local VM. */ import { bareBox, reportBox } from '../lib.mjs' const RELEASE = process.env.LIVE_BOX_RELEASE || '24.04' const FORCE_V4 = `echo 'Acquire::ForceIPv4 "true";' | sudo tee /etc/apt/apt.conf.d/99force-ipv4 >/dev/null` const REFUSED = /psql: error: connection to server at "localhost" \((::1|127\.0\.0\.1)\), port 5432 failed: Connection refused/ const check = (ctx) => bareBox(ctx, async ({ sudoUser }) => { const { run, L } = sudoUser() const PW = ctx.secret('[password]' + Math.random().toString(36).slice(2, 12)) // ── hidden: PostgreSQL with an app database, as a reader has it ───────────────────────── L(`${FORCE_V4} && sudo apt-get update -qq && sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq postgresql >/dev/null`, 1800000) const V = L('ls /etc/postgresql').trim() ctx.check(`(setup) PostgreSQL ${V} is installed, cluster ${V}/main online`, /online/.test(L('pg_lsclusters')), L('pg_lsclusters')) L(`sudo -u postgres psql -qc "CREATE ROLE app LOGIN PASSWORD '${PW}'" && sudo -u postgres createdb -O app appdb`) // the runner masks the box's real addresses, so "another machine" connects to a fixed documentation address added to // the server (any address that is not loopback behaves the same for listen_addresses and pg_hba.conf) const IP = '192.0.2.10' L(`sudo ip addr add ${IP}/24 dev lo`) ctx.check(`(setup) the server also has the address ${IP}`, L(`ip -4 addr show dev lo`).includes(IP), '') L(`printf 'localhost:*:appdb:app:%s\\n${IP}:*:appdb:app:%s\\n' '${PW}' '${PW}' > ~/.pgpass && chmod 600 ~/.pgpass`) const ok0 = L("psql -h localhost -U app -d appdb -tAc 'select 1'") ctx.check('(setup) app logs in to appdb over TCP on localhost', ok0 === '1', ok0) // ── 1. the cluster is down ────────────────────────────────────────────────────────────── L(`sudo systemctl stop postgresql@${V}-main`) const c1 = run("psql -h localhost -U app -d appdb -c 'select 1'", { id: 'down-psql' }) ctx.check('psql: connection to server at "localhost" (::1), port 5432 failed: Connection refused … Is the server running on that host and accepting TCP/IP connections?', REFUSED.test(c1.out) && /Is the server running on that host and accepting TCP\/IP connections\?/.test(c1.out), c1.out) const s1 = run('systemctl status postgresql --no-pager | head -n 3', { id: 'down-status' }) ctx.check('systemctl status postgresql: active (exited): the umbrella unit says nothing about the cluster', /Active: active \(exited\)/.test(s1.out), s1.out) const p1 = run('pg_lsclusters', { id: 'down-lsclusters' }) ctx.check(`pg_lsclusters: ${V} main 5432 down`, new RegExp(`^${V}\\s+main\\s+5432\\s+down\\s`, 'm').test(p1.out), p1.out) const rs = run('sudo systemctl restart postgresql', { id: 'down-restart' }) ctx.check('systemctl restart postgresql (restarts every cluster)', rs.exit === 0, rs.out) const p3 = run('pg_lsclusters', { id: 'down-lsclusters-3' }) ctx.check(`pg_lsclusters: ${V} main 5432 online`, new RegExp(`^${V}\\s+main\\s+5432\\s+online\\s`, 'm').test(p3.out), p3.out) const c2 = run("psql -h localhost -U app -d appdb -c 'select 1'", { id: 'down-ok' }) ctx.check('psql: the query answers (1 row)', /\(1 row\)/.test(c2.out), c2.out) const j1 = run(`sudo tail -n 3 /var/log/postgresql/postgresql-${V}-main.log`, { id: 'down-log' }) ctx.check('the cluster log: database system is ready to accept connections', /database system is ready to accept connections/.test(j1.out), j1.out) // ── 2. another port ───────────────────────────────────────────────────────────────────── L(`sudo sed -i 's/^port = 5432/port = 5433/' /etc/postgresql/${V}/main/postgresql.conf && sudo systemctl restart postgresql`) const q1 = run("psql -h localhost -U app -d appdb -c 'select 1'", { id: 'port-psql' }) ctx.check('another port: port 5432 failed: Connection refused', REFUSED.test(q1.out), q1.out) const q2 = run('pg_lsclusters', { id: 'port-lsclusters' }) ctx.check(`pg_lsclusters: ${V} main 5433 online`, new RegExp(`^${V}\\s+main\\s+5433\\s+online\\s`, 'm').test(q2.out), q2.out) const q3 = run("sudo ss -tlnp | grep postgres", { id: 'port-ss' }) ctx.check('ss: postgres listens on 127.0.0.1:5433 and [::1]:5433', /127\.0\.0\.1:5433/.test(q3.out) && /\[::1\]:5433/.test(q3.out), q3.out) const q4 = run("psql -h localhost -p 5433 -U app -d appdb -tAc 'select 1'", { id: 'port-5433' }) ctx.check('with -p 5433 it works', q4.out === '1', q4.out) const q5 = run("sudo sed -i 's/^port = 5433/port = 5432/' /etc/postgresql/*/main/postgresql.conf && sudo systemctl restart postgresql", { id: 'port-fix' }) ctx.check('the port is set back to 5432', q5.exit === 0 && new RegExp(`^${V}\\s+main\\s+5432\\s+online`, 'm').test(L('pg_lsclusters')), q5.out) // ── 3. from another machine: listen_addresses, then pg_hba.conf ───────────────────────── const r1 = run(`psql -h ${IP} -U app -d appdb -c 'select 1'`, { id: 'remote-psql' }) ctx.check(`from the network address: connection to server at "${IP}", port 5432 failed: Connection refused`, new RegExp(`connection to server at "${IP.replace(/\./g, '\\.')}", port 5432 failed: Connection refused`).test(r1.out), r1.out) const r2 = run("sudo ss -tlnp 'sport = :5432'", { id: 'remote-ss' }) ctx.check('ss: only 127.0.0.1:5432 and [::1]:5432 (localhost)', /127\.0\.0\.1:5432/.test(r2.out) && /\[::1\]:5432/.test(r2.out) && !/0\.0\.0\.0:5432/.test(r2.out), r2.out) const r3 = run('sudo -u postgres psql -tAc "show listen_addresses"', { id: 'remote-show' }) ctx.check('show listen_addresses: localhost', r3.out === 'localhost', r3.out) const r4 = run(`sudo -u postgres psql -c "ALTER SYSTEM SET listen_addresses = '*'"`, { id: 'remote-conf' }) ctx.check("ALTER SYSTEM SET listen_addresses = '*': ALTER SYSTEM", /^ALTER SYSTEM$/m.test(r4.out), r4.out) const r5 = run('sudo systemctl restart postgresql', { id: 'remote-restart' }) ctx.check('restart (listen_addresses needs a restart, not a reload)', r5.exit === 0, r5.out) const r6 = run("sudo ss -tlnp 'sport = :5432'", { id: 'remote-ss-2' }) ctx.check('ss: 0.0.0.0:5432 and [::]:5432', /0\.0\.0\.0:5432/.test(r6.out) && /\[::\]:5432/.test(r6.out), r6.out) const r7 = run(`psql -h ${IP} -U app -d appdb -c 'select 1'`, { id: 'remote-hba' }) ctx.check(`the next error: no pg_hba.conf entry for host "${IP}", user "app", database "appdb"`, new RegExp(`no pg_hba\\.conf entry for host "${IP.replace(/\./g, '\\.')}", user "app", database "appdb"`).test(r7.out), r7.out) const NET = IP.replace(/\.\d+$/, '.0/24') const r8 = run(`echo 'host appdb app ${NET} scram-sha-256' | sudo tee -a /etc/postgresql/*/main/pg_hba.conf`, { id: 'remote-hba-add' }) ctx.check(`pg_hba.conf gets: host appdb app ${NET} scram-sha-256`, r8.out === `host appdb app ${NET} scram-sha-256`, r8.out) const r9 = run('sudo systemctl reload postgresql', { id: 'remote-reload' }) ctx.check('reload (pg_hba.conf needs only a reload)', r9.exit === 0, r9.out) const r10 = run(`psql -h ${IP} -U app -d appdb -c 'select 1'`, { id: 'remote-ok' }) ctx.check('from the network address: the query answers (1 row)', /\(1 row\)/.test(r10.out), r10.out) // ── end state ─────────────────────────────────────────────────────────────────────────── const e = L("psql -h localhost -U app -d appdb -tAc 'select 1'") ctx.check('(app logs in on localhost at the end of the steps)', e === '1', e) ctx.stepsDone() reportBox( ctx, L, `A fresh Ubuntu ${RELEASE} server (${process.env.LIVE_VPS ? 'a rented cloud server, rebuilt to the provider’s stock image' : 'a VM from Ubuntu’s cloud image'}) with PostgreSQL ${V} from Ubuntu’s archive, a role app with a password (in ~/.pgpass) and its database appdb, reached over SSH as the sudo user deploy. The connections from "another machine" go to a second address of the server, ${IP}.`, ['postgresql', 'postgresql-common', `postgresql-${V}`, `postgresql-client-${V}`], ) }) export default check