ServerHandbook live check: run log ------------------------------------------------------------------------------ Article https://serverhandbook.com/kb/install-postgresql-ubuntu Check scripts/live/checks/install-postgresql-ubuntu.mjs (sha256 36d062ead642563c4da64491aeb6744aa9e191c4e3d1a0e7245a86fb7594be83) Ran at 2026-11-18T22:58:17Z (119 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-18 18.6-0ubuntu0.26.04.1 Package postgresql-common 290ubuntu1 Package postgresql-client-18 18.6-0ubuntu0.26.04.1 Package ufw 0.36.2-9build1 Setup A fresh Ubuntu 26.04 server from Ubuntu’s cloud image (or a provider’s stock image) with ufw on, as the ufw article leaves it, reached over SSH as a sudo user. The “app server” is a second machine on its own network address (192.0.2.2), joined to the server by a virtual cable, so its connections pass through the server’s real firewall. 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 (13) libcommon-sense-perl 3.75-3build5 libjson-perl 4.10000-1 libjson-xs-perl 4.040-1 libpq5 18.6-0ubuntu0.26.04.1 libtypes-serialiser-perl 1.01-1 liburing2 2.14-1 postgresql 18+290ubuntu1 postgresql-18 18.6-0ubuntu0.26.04.1 postgresql-18-jit 18.6-0ubuntu0.26.04.1 postgresql-client-18 18.6-0ubuntu0.26.04.1 postgresql-client-common 290ubuntu1 postgresql-common 290ubuntu1 ssl-cert 1.1.3ubuntu2 Packages upgraded (0) Packages removed (0) Services enabled at boot (2) postgresql.service ssl-cert.service 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 (246) /etc/alternatives/ABORT.7.gz /etc/alternatives/ALTER_AGGREGATE.7.gz /etc/alternatives/ALTER_COLLATION.7.gz /etc/alternatives/ALTER_CONVERSION.7.gz /etc/alternatives/ALTER_DATABASE.7.gz /etc/alternatives/ALTER_DEFAULT_PRIVILEGES.7.gz /etc/alternatives/ALTER_DOMAIN.7.gz /etc/alternatives/ALTER_EVENT_TRIGGER.7.gz /etc/alternatives/ALTER_EXTENSION.7.gz /etc/alternatives/ALTER_FOREIGN_DATA_WRAPPER.7.gz /etc/alternatives/ALTER_FOREIGN_TABLE.7.gz /etc/alternatives/ALTER_FUNCTION.7.gz /etc/alternatives/ALTER_GROUP.7.gz /etc/alternatives/ALTER_INDEX.7.gz /etc/alternatives/ALTER_LANGUAGE.7.gz /etc/alternatives/ALTER_LARGE_OBJECT.7.gz /etc/alternatives/ALTER_MATERIALIZED_VIEW.7.gz /etc/alternatives/ALTER_OPERATOR.7.gz /etc/alternatives/ALTER_OPERATOR_CLASS.7.gz /etc/alternatives/ALTER_OPERATOR_FAMILY.7.gz /etc/alternatives/ALTER_POLICY.7.gz /etc/alternatives/ALTER_PROCEDURE.7.gz /etc/alternatives/ALTER_PUBLICATION.7.gz /etc/alternatives/ALTER_ROLE.7.gz /etc/alternatives/ALTER_ROUTINE.7.gz /etc/alternatives/ALTER_RULE.7.gz /etc/alternatives/ALTER_SCHEMA.7.gz /etc/alternatives/ALTER_SEQUENCE.7.gz /etc/alternatives/ALTER_SERVER.7.gz /etc/alternatives/ALTER_STATISTICS.7.gz /etc/alternatives/ALTER_SUBSCRIPTION.7.gz /etc/alternatives/ALTER_SYSTEM.7.gz /etc/alternatives/ALTER_TABLE.7.gz /etc/alternatives/ALTER_TABLESPACE.7.gz /etc/alternatives/ALTER_TEXT_SEARCH_CONFIGURATION.7.gz /etc/alternatives/ALTER_TEXT_SEARCH_DICTIONARY.7.gz /etc/alternatives/ALTER_TEXT_SEARCH_PARSER.7.gz /etc/alternatives/ALTER_TEXT_SEARCH_TEMPLATE.7.gz /etc/alternatives/ALTER_TRIGGER.7.gz /etc/alternatives/ALTER_TYPE.7.gz /etc/alternatives/ALTER_USER.7.gz /etc/alternatives/ALTER_USER_MAPPING.7.gz /etc/alternatives/ALTER_VIEW.7.gz /etc/alternatives/ANALYZE.7.gz /etc/alternatives/BEGIN.7.gz /etc/alternatives/CALL.7.gz /etc/alternatives/CHECKPOINT.7.gz /etc/alternatives/CLOSE.7.gz /etc/alternatives/CLUSTER.7.gz /etc/alternatives/COMMENT.7.gz /etc/alternatives/COMMIT.7.gz /etc/alternatives/COMMIT_PREPARED.7.gz /etc/alternatives/COPY.7.gz /etc/alternatives/CREATE_ACCESS_METHOD.7.gz /etc/alternatives/CREATE_AGGREGATE.7.gz /etc/alternatives/CREATE_CAST.7.gz /etc/alternatives/CREATE_COLLATION.7.gz /etc/alternatives/CREATE_CONVERSION.7.gz /etc/alternatives/CREATE_DATABASE.7.gz /etc/alternatives/CREATE_DOMAIN.7.gz /etc/alternatives/CREATE_EVENT_TRIGGER.7.gz /etc/alternatives/CREATE_EXTENSION.7.gz /etc/alternatives/CREATE_FOREIGN_DATA_WRAPPER.7.gz /etc/alternatives/CREATE_FOREIGN_TABLE.7.gz /etc/alternatives/CREATE_FUNCTION.7.gz /etc/alternatives/CREATE_GROUP.7.gz /etc/alternatives/CREATE_INDEX.7.gz /etc/alternatives/CREATE_LANGUAGE.7.gz /etc/alternatives/CREATE_MATERIALIZED_VIEW.7.gz /etc/alternatives/CREATE_OPERATOR.7.gz /etc/alternatives/CREATE_OPERATOR_CLASS.7.gz /etc/alternatives/CREATE_OPERATOR_FAMILY.7.gz /etc/alternatives/CREATE_POLICY.7.gz /etc/alternatives/CREATE_PROCEDURE.7.gz /etc/alternatives/CREATE_PUBLICATION.7.gz /etc/alternatives/CREATE_ROLE.7.gz /etc/alternatives/CREATE_RULE.7.gz /etc/alternatives/CREATE_SCHEMA.7.gz /etc/alternatives/CREATE_SEQUENCE.7.gz /etc/alternatives/CREATE_SERVER.7.gz /etc/alternatives/CREATE_STATISTICS.7.gz /etc/alternatives/CREATE_SUBSCRIPTION.7.gz /etc/alternatives/CREATE_TABLE.7.gz /etc/alternatives/CREATE_TABLESPACE.7.gz /etc/alternatives/CREATE_TABLE_AS.7.gz /etc/alternatives/CREATE_TEXT_SEARCH_CONFIGURATION.7.gz /etc/alternatives/CREATE_TEXT_SEARCH_DICTIONARY.7.gz /etc/alternatives/CREATE_TEXT_SEARCH_PARSER.7.gz /etc/alternatives/CREATE_TEXT_SEARCH_TEMPLATE.7.gz /etc/alternatives/CREATE_TRANSFORM.7.gz /etc/alternatives/CREATE_TRIGGER.7.gz /etc/alternatives/CREATE_TYPE.7.gz /etc/alternatives/CREATE_USER.7.gz /etc/alternatives/CREATE_USER_MAPPING.7.gz /etc/alternatives/CREATE_VIEW.7.gz /etc/alternatives/DEALLOCATE.7.gz /etc/alternatives/DECLARE.7.gz /etc/alternatives/DELETE.7.gz /etc/alternatives/DISCARD.7.gz /etc/alternatives/DO.7.gz /etc/alternatives/DROP_ACCESS_METHOD.7.gz /etc/alternatives/DROP_AGGREGATE.7.gz /etc/alternatives/DROP_CAST.7.gz /etc/alternatives/DROP_COLLATION.7.gz /etc/alternatives/DROP_CONVERSION.7.gz /etc/alternatives/DROP_DATABASE.7.gz /etc/alternatives/DROP_DOMAIN.7.gz /etc/alternatives/DROP_EVENT_TRIGGER.7.gz /etc/alternatives/DROP_EXTENSION.7.gz /etc/alternatives/DROP_FOREIGN_DATA_WRAPPER.7.gz /etc/alternatives/DROP_FOREIGN_TABLE.7.gz /etc/alternatives/DROP_FUNCTION.7.gz /etc/alternatives/DROP_GROUP.7.gz /etc/alternatives/DROP_INDEX.7.gz /etc/alternatives/DROP_LANGUAGE.7.gz /etc/alternatives/DROP_MATERIALIZED_VIEW.7.gz /etc/alternatives/DROP_OPERATOR.7.gz /etc/alternatives/DROP_OPERATOR_CLASS.7.gz /etc/alternatives/DROP_OPERATOR_FAMILY.7.gz /etc/alternatives/DROP_OWNED.7.gz /etc/alternatives/DROP_POLICY.7.gz /etc/alternatives/DROP_PROCEDURE.7.gz /etc/alternatives/DROP_PUBLICATION.7.gz /etc/alternatives/DROP_ROLE.7.gz /etc/alternatives/DROP_ROUTINE.7.gz /etc/alternatives/DROP_RULE.7.gz /etc/alternatives/DROP_SCHEMA.7.gz /etc/alternatives/DROP_SEQUENCE.7.gz /etc/alternatives/DROP_SERVER.7.gz /etc/alternatives/DROP_STATISTICS.7.gz /etc/alternatives/DROP_SUBSCRIPTION.7.gz /etc/alternatives/DROP_TABLE.7.gz /etc/alternatives/DROP_TABLESPACE.7.gz /etc/alternatives/DROP_TEXT_SEARCH_CONFIGURATION.7.gz /etc/alternatives/DROP_TEXT_SEARCH_DICTIONARY.7.gz /etc/alternatives/DROP_TEXT_SEARCH_PARSER.7.gz /etc/alternatives/DROP_TEXT_SEARCH_TEMPLATE.7.gz /etc/alternatives/DROP_TRANSFORM.7.gz /etc/alternatives/DROP_TRIGGER.7.gz /etc/alternatives/DROP_TYPE.7.gz /etc/alternatives/DROP_USER.7.gz /etc/alternatives/DROP_USER_MAPPING.7.gz /etc/alternatives/DROP_VIEW.7.gz /etc/alternatives/END.7.gz /etc/alternatives/EXECUTE.7.gz /etc/alternatives/EXPLAIN.7.gz /etc/alternatives/FETCH.7.gz /etc/alternatives/GRANT.7.gz /etc/alternatives/IMPORT_FOREIGN_SCHEMA.7.gz /etc/alternatives/INSERT.7.gz /etc/alternatives/LISTEN.7.gz /etc/alternatives/LOAD.7.gz /etc/alternatives/LOCK.7.gz /etc/alternatives/MERGE.7.gz /etc/alternatives/MOVE.7.gz /etc/alternatives/NOTIFY.7.gz /etc/alternatives/PREPARE.7.gz /etc/alternatives/PREPARE_TRANSACTION.7.gz /etc/alternatives/REASSIGN_OWNED.7.gz /etc/alternatives/REFRESH_MATERIALIZED_VIEW.7.gz /etc/alternatives/REINDEX.7.gz /etc/alternatives/RELEASE_SAVEPOINT.7.gz /etc/alternatives/RESET.7.gz /etc/alternatives/REVOKE.7.gz /etc/alternatives/ROLLBACK.7.gz /etc/alternatives/ROLLBACK_PREPARED.7.gz /etc/alternatives/ROLLBACK_TO_SAVEPOINT.7.gz /etc/alternatives/SAVEPOINT.7.gz /etc/alternatives/SECURITY_LABEL.7.gz /etc/alternatives/SELECT.7.gz /etc/alternatives/SELECT_INTO.7.gz /etc/alternatives/SET.7.gz /etc/alternatives/SET_CONSTRAINTS.7.gz /etc/alternatives/SET_ROLE.7.gz /etc/alternatives/SET_SESSION_AUTHORIZATION.7.gz /etc/alternatives/SET_TRANSACTION.7.gz /etc/alternatives/SHOW.7.gz /etc/alternatives/START_TRANSACTION.7.gz /etc/alternatives/TABLE.7.gz /etc/alternatives/TRUNCATE.7.gz /etc/alternatives/UNLISTEN.7.gz /etc/alternatives/UPDATE.7.gz /etc/alternatives/VACUUM.7.gz /etc/alternatives/VALUES.7.gz /etc/alternatives/WITH.7.gz /etc/alternatives/clusterdb.1.gz /etc/alternatives/createdb.1.gz /etc/alternatives/createuser.1.gz /etc/alternatives/dropdb.1.gz /etc/alternatives/dropuser.1.gz /etc/alternatives/initdb.1.gz /etc/alternatives/oid2name.1.gz /etc/alternatives/pg_amcheck.1.gz /etc/alternatives/pg_archivecleanup.1.gz /etc/alternatives/pg_basebackup.1.gz /etc/alternatives/pg_checksums.1.gz /etc/alternatives/pg_combinebackup.1.gz /etc/alternatives/pg_controldata.1.gz /etc/alternatives/pg_createsubscriber.1.gz /etc/alternatives/pg_ctl.1.gz /etc/alternatives/pg_dump.1.gz /etc/alternatives/pg_dumpall.1.gz /etc/alternatives/pg_isready.1.gz /etc/alternatives/pg_receivewal.1.gz /etc/alternatives/pg_recvlogical.1.gz /etc/alternatives/pg_resetwal.1.gz /etc/alternatives/pg_restore.1.gz /etc/alternatives/pg_rewind.1.gz /etc/alternatives/pg_test_fsync.1.gz /etc/alternatives/pg_test_timing.1.gz /etc/alternatives/pg_upgrade.1.gz /etc/alternatives/pg_verifybackup.1.gz /etc/alternatives/pg_waldump.1.gz /etc/alternatives/pg_walsummary.1.gz /etc/alternatives/pgbench.1.gz /etc/alternatives/postgres.1.gz /etc/alternatives/psql.1.gz /etc/alternatives/reindexdb.1.gz /etc/alternatives/vacuumdb.1.gz /etc/alternatives/vacuumlo.1.gz /etc/apt/apt.conf.d/02autoremove-postgresql /etc/init.d/postgresql /etc/logrotate.d/postgresql-common /etc/postgresql-common/createcluster.conf /etc/postgresql-common/pg_upgradecluster.d/analyze /etc/postgresql-common/root.crt /etc/postgresql-common/user_clusters /etc/postgresql/18/main/conf.d/listen.conf /etc/postgresql/18/main/environment /etc/postgresql/18/main/pg_ctl.conf /etc/postgresql/18/main/pg_hba.conf /etc/postgresql/18/main/pg_ident.conf /etc/postgresql/18/main/postgresql.conf /etc/postgresql/18/main/start.conf /etc/rc0.d/K01postgresql /etc/rc1.d/K01postgresql /etc/rc2.d/S01postgresql /etc/rc3.d/S01postgresql /etc/rc4.d/S01postgresql /etc/rc5.d/S01postgresql /etc/rc6.d/K01postgresql /etc/ssl/certs/e45582bc.0 /etc/ssl/certs/ssl-cert-snakeoil.pem /etc/ssl/private/ssl-cert-snakeoil.key /etc/systemd/system/multi-user.target.wants/postgresql.service /etc/systemd/system/multi-user.target.wants/ssl-cert.service Files changed in /etc (10) /etc/group /etc/group- /etc/gshadow /etc/gshadow- /etc/ld.so.cache /etc/passwd /etc/passwd- /etc/shadow /etc/shadow- /etc/ufw/user.rules Files removed from /etc (0) Disk used on / (includes logs written during the run): +314.8 MB ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Check-your-server script (2 items): https://cdn.serverhandbook.com/check/install-postgresql-ubuntu/ubuntu-26.04.1-20261118t225817z.sh SHA-256 93d55d5d7a62b14a0f192d441b495237afc4be3b7a8c2ff339a3c62820a7528b ------------------------------------------------------------------------------ On the stock server, before the first step: FAIL a PostgreSQL cluster "main" is online on port 5432 FAIL PostgreSQL starts at boot After the last step: PASS a PostgreSQL cluster "main" is online on port 5432 PASS PostgreSQL starts at boot ------------------------------------------------------------------------------ Claims (28), each with what the run saw ------------------------------------------------------------------------------ 1. ok a bare Ubuntu 26.04 server box (web-server) starts 2. ok apt update reads the package lists | ] | Get:47 http://archive.ubuntu.com/ubuntu resolute-backports/multiverse amd64 Components [216 B] | Get:48 http://archive.ubuntu.com/ubuntu resolute-backports/multiverse amd64 c-n-f Metadata [120 B] | Fetched 32.7 MB in 17s (1929 kB/s) | 23 packages can be upgraded. Run 'apt list --upgradable' to see them. 3. ok apt install -y postgresql installs PostgreSQL 18 and creates the cluster 18/main | ting up postgresql-18 (18.6-0ubuntu0.26.04.1) ... | Creating new PostgreSQL cluster 18/main ... | /usr/lib/postgresql/18/bin/initdb -D /var/lib/postgresql/18/main --auth-local peer --auth-host scram-sha-256 --no-instructions | The files belonging to this database system will be owned by user "postgres". | This user must also own the server process. | | The database cluster will be initialized with locale "C.UTF-8". | The default database encoding has accordingly been set to "UTF8". | The default text search configuration will be set to "english". | | Data page checksums are enabled. | | fixing permissions on existing directory /var/lib/postgresql/18/main ... ok | creating subdirectories ... ok | selecting dynamic shared memory implementation ... posix | selecting default "max_connections" ... 100 | selecting default "shared_buffers" ... 128MB | selecting default time zone ... Etc/UTC | creating configuration files ... ok | running bootstrap script ... ok | performing post-bootstrap initialization ... ok | syncing data to disk ... ok | Setting up postgresql-18-jit (18.6-0ubuntu0.26.04.1) ... | Setting up postgresql (18+290ubuntu1) ... | Processing triggers for man-db (2.13.1-1build1) ... | Processing triggers for libc-bin (2.43-2ubuntu2.4) ... | | Scanning processes... | Scanning linux images... | | Running kernel seems to be up-to-date. | | No services need to be restarted. | | No containers need to be restarted. | | No user sessions are running outdated binaries. | | No VM guests are running outdated hypervisor (qemu) binaries on this host. 4. ok pg_lsclusters: 18 main 5432 online, data in /var/lib/postgresql/18/main | 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 5. ok the postgresql unit is enabled and "active (exited)": a wrapper, not the server | ● postgresql.service - PostgreSQL RDBMS | Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: enabled) | Active: active (exited) since Wed 2026-11-18 22:59:37 UTC; 11s ago | Invocation: 50b54d1a329545748cec1a20f518eea7 | Main PID: 4158 (code=exited, status=0/SUCCESS) | Mem peak: 2M | CPU: 6ms 6. ok postgresql@18-main is "active (running)": the server itself | ● postgresql@18-main.service - PostgreSQL Cluster 18-main | Loaded: loaded (/usr/lib/systemd/system/postgresql@.service; enabled-runtime; preset: enabled) | Active: active (running) since Wed 2026-11-18 22:59:42 UTC; 6s ago | Invocation: a244473ff3ab4a2293ade05da10fcf0f | Process: 5044 ExecStart=/usr/bin/pg_ctlcluster --skip-systemctl-redirect 18-main start (code=exited, status=0/SUCCESS) | Main PID: 5049 (postgres) | Tasks: 9[0;38:5:245m (limit: 1486) | Memory: 23M (peak: 31.6M) | CPU: 182ms | CGroup: /system.slice/system-postgresql.slice/postgresql@18-main.service | ├─[0;38:5:245m5049 /usr/lib/postgresql/18/bin/postgres -D /var/lib/postgresql/18/main -c config_file=/etc/postgresql/1… | ├─[0;38:5:245m5050 "postgres: 18/main: io worker 0" | ├─[0;38:5:245m5051 "postgres: 18/main: io worker 2" | ├─[0;38:5:245m5052 "postgres: 18/main: io worker 1" | ├─[0;38:5:245m5053 "postgres: 18/main: checkpointer " | ├─[0;38:5:245m5054 "postgres: 18/main: background writer " | ├─[0;38:5:245m5056 "postgres: 18/main: walwriter " | ├─[0;38:5:245m5057 "postgres: 18/main: autovacuum launcher " | └─[0;38:5:245m5058 "postgres: 18/main: logical replication launcher " 7. ok it listens on 127.0.0.1:5432 and [::1]:5432 only | LISTEN 0 200 127.0.0.1:5432 0.0.0.0:* users:(("postgres",pid=5049,fd=7)) | LISTEN 0 200 [::1]:5432 [::]:* users:(("postgres",pid=5049,fd=6)) 8. ok sudo -u postgres psql works: server_version 18.x (Ubuntu 18.x-…) | server_version | ------------------------------------- | 18.6 (Ubuntu 18.6-0ubuntu0.26.04.1) | (1 row) 9. ok plain psql as deploy: FATAL: role "deploy" does not exist | psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "deploy" does not exist 10. ok psql -U postgres as deploy: FATAL: Peer authentication failed for user "postgres" | psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: Peer authentication failed for user "postgres" 11. ok pg_hba.conf: local = peer, host 127.0.0.1/32 and ::1/128 = scram-sha-256 | local all postgres peer | local all all peer | host all all 127.0.0.1/32 scram-sha-256 | host all all ::1/128 scram-sha-256 | local replication all peer | host replication all 127.0.0.1/32 scram-sha-256 | host replication all ::1/128 scram-sha-256 12. ok createuser --pwprompt asks for the password twice and creates the role | Enter password for new role: | Enter it again: 13. ok createdb --owner appuser appdb prints nothing on success 14. ok psql -h localhost -U appuser asks for the password and connects to appdb | Password for user appuser: | current_user | current_database | --------------+------------------ | appuser | appdb | (1 row) 15. ok the password is stored as a SCRAM-SHA-256 verifier, not as text | rolname | stored | ---------+---------------- | appuser | SCRAM-SHA-256$ | (1 row) 16. ok after a reload it still listens on localhost only | LISTEN 0 200 127.0.0.1:5432 0.0.0.0:* users:(("postgres",pid=5049,fd=7)) | LISTEN 0 200 [::1]:5432 [::]:* users:(("postgres",pid=5049,fd=6)) 17. ok the log says: parameter "listen_addresses" cannot be changed without restarting the server | 2026-11-18 22:59:55.546 UTC [5049] LOG: parameter "listen_addresses" cannot be changed without restarting the server 18. ok after a restart it listens on every address (0.0.0.0:5432 and [::]:5432) | LISTEN 0 200 0.0.0.0:5432 0.0.0.0:* users:(("postgres",pid=6701,fd=6)) | LISTEN 0 200 [::]:5432 [::]:* users:(("postgres",pid=6701,fd=7)) 19. ok from the app server, before the firewall rule: timeout expired (ufw drops it) | psql: error: connection to server at "203.0.113.10", port 5432 failed: timeout expired 20. ok ufw adds the rule for that address only | Rule added 21. ok from the app server: password, then connected as appuser over TLS (ssl = t, TLSv1.3) | Password for user appuser: | current_user | ssl | tls | --------------+-----+--------- | appuser | t | TLSv1.3 | (1 row) 22. ok as postgres from the app server: no pg_hba.conf entry for host "192.0.2.2", user "postgres" | psql: error: connection to server at "203.0.113.10", port 5432 failed: FATAL: no pg_hba.conf entry for host "192.0.2.2", user "postgres", database "postgres", SSL encryption | connection to server at "203.0.113.10", port 5432 failed: FATAL: no pg_hba.conf entry for host "192.0.2.2", user "postgres", database "postgres", no encryption 23. ok ufw status: 5432/tcp ALLOW from 192.0.2.2 only | Status: active | | To Action From | -- ------ ---- | OpenSSH ALLOW Anywhere | 5432/tcp ALLOW 192.0.2.2 | OpenSSH (v6) ALLOW Anywhere (v6) 24. ok (the server is running at the end of the steps) | active 25. ok (tested on) the box reports its OS, kernel and the version of 5 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 | PKG ufw 0.36.2-9build1 26. ok undo: localhost only again | LISTEN 0 200 127.0.0.1:5432 0.0.0.0:* users:(("postgres",pid=8167,fd=7)) | LISTEN 0 200 [::1]:5432 [::]:* users:(("postgres",pid=8167,fd=6)) 27. ok undo: dropdb and dropuser print nothing on success 28. ok undo: the role is gone (count 0) | 0 ------------------------------------------------------------------------------ Commands and what they printed (33) ------------------------------------------------------------------------------ $ sudo apt update Hit:1 http://archive.ubuntu.com/ubuntu resolute InRelease Get:2 http://security.ubuntu.com/ubuntu resolute-security InRelease [137 kB] Get:3 http://archive.ubuntu.com/ubuntu resolute-updates InRelease [137 kB] Get:4 http://archive.ubuntu.com/ubuntu resolute-backports InRelease [137 kB] Get:5 http://archive.ubuntu.com/ubuntu resolute/universe amd64 Packages [16.0 MB] Get:6 http://security.ubuntu.com/ubuntu resolute-security/main amd64 Packages [720 kB] Get:7 http://security.ubuntu.com/ubuntu resolute-security/main Translation-en [158 kB] Get:8 http://security.ubuntu.com/ubuntu resolute-security/main amd64 Components [46.7 kB] Get:9 http://archive.ubuntu.com/ubuntu resolute/universe Translation-en [6329 kB] Get:10 http://security.ubuntu.com/ubuntu resolute-security/universe amd64 Packages [221 kB] Get:11 http://security.ubuntu.com/ubuntu resolute-security/universe Translation-en [72.7 kB] Get:12 http://security.ubuntu.com/ubuntu resolute-security/universe amd64 Components [62.8 kB] Get:13 http://security.ubuntu.com/ubuntu resolute-security/universe amd64 c-n-f Metadata [3552 B] Get:14 http://security.ubuntu.com/ubuntu resolute-security/restricted amd64 Packages [586 kB] Get:15 http://archive.ubuntu.com/ubuntu resolute/universe amd64 Components [4556 kB] Get:16 http://archive.ubuntu.com/ubuntu resolute/universe amd64 c-n-f Metadata [313 kB] Get:17 http://archive.ubuntu.com/ubuntu resolute/multiverse amd64 Packages [290 kB] Get:18 http://archive.ubuntu.com/ubuntu resolute/multiverse Translation-en [127 kB] Get:19 http://archive.ubuntu.com/ubuntu resolute/multiverse amd64 Components [50.0 kB] Get:20 http://archive.ubuntu.com/ubuntu resolute/multiverse amd64 c-n-f Metadata [8276 B] Get:21 http://archive.ubuntu.com/ubuntu resolute-updates/main amd64 Packages [873 kB] Get:22 http://archive.ubuntu.com/ubuntu resolute-updates/main Translation-en [196 kB] Get:23 http://archive.ubuntu.com/ubuntu resolute-updates/main amd64 Components [110 kB] Get:24 http://archive.ubuntu.com/ubuntu resolute-updates/universe amd64 Packages [392 kB] Get:25 http://archive.ubuntu.com/ubuntu resolute-updates/universe Translation-en [124 kB] Get:26 http://security.ubuntu.com/ubuntu resolute-security/restricted Translation-en [122 kB] Get:27 http://archive.ubuntu.com/ubuntu resolute-updates/universe amd64 Components [201 kB] Get:28 http://archive.ubuntu.com/ubuntu resolute-updates/universe amd64 c-n-f Metadata [4704 B] Get:29 http://archive.ubuntu.com/ubuntu resolute-updates/restricted amd64 Packages [602 kB] Get:30 http://security.ubuntu.com/ubuntu resolute-security/multiverse amd64 Packages [20.0 kB] Get:31 http://archive.ubuntu.com/ubuntu resolute-updates/restricted Translation-en [125 kB] Get:32 http://archive.ubuntu.com/ubuntu resolute-updates/multiverse amd64 Packages [21.0 kB] Get:33 http://archive.ubuntu.com/ubuntu resolute-updates/multiverse Translation-en [4168 B] Get:34 http://archive.ubuntu.com/ubuntu resolute-updates/multiverse amd64 Components [216 B] Get:35 http://archive.ubuntu.com/ubuntu resolute-updates/multiverse amd64 c-n-f Metadata [256 B] Get:36 http://archive.ubuntu.com/ubuntu resolute-backports/main amd64 Components [212 B] Get:37 http://archive.ubuntu.com/ubuntu resolute-backports/main amd64 c-n-f Metadata [112 B] Get:38 http://archive.ubuntu.com/ubuntu resolute-backports/universe amd64 Packages [3136 B] Get:39 http://archive.ubuntu.com/ubuntu resolute-backports/universe Translation-en [7988 B] Get:40 http://security.ubuntu.com/ubuntu resolute-security/multiverse Translation-en [3728 B] Get:41 http://security.ubuntu.com/ubuntu resolute-security/multiverse amd64 Components [212 B] Get:42 http://security.ubuntu.com/ubuntu resolute-security/multiverse amd64 c-n-f Metadata [120 B] Get:43 http://archive.ubuntu.com/ubuntu resolute-backports/universe amd64 Components [1056 B] Get:44 http://archive.ubuntu.com/ubuntu resolute-backports/universe amd64 c-n-f Metadata [116 B] Get:45 http://archive.ubuntu.com/ubuntu resolute-backports/restricted amd64 Components [216 B] Get:46 http://archive.ubuntu.com/ubuntu resolute-backports/restricted amd64 c-n-f Metadata [120 B] Get:47 http://archive.ubuntu.com/ubuntu resolute-backports/multiverse amd64 Components [216 B] Get:48 http://archive.ubuntu.com/ubuntu resolute-backports/multiverse amd64 c-n-f Metadata [120 B] Fetched 32.7 MB in 17s (1929 kB/s) 23 packages can be upgraded. Run 'apt list --upgradable' to see them. $ sudo apt install -y postgresql Installing: postgresql Installing dependencies: libcommon-sense-perl libpq5 postgresql-18 postgresql-client-common libjson-perl libtypes-serialiser-perl postgresql-18-jit postgresql-common libjson-xs-perl liburing2 postgresql-client-18 ssl-cert Suggested packages: libpq-oauth postgresql-doc postgresql-doc-18 Summary: Upgrading: 0, Installing: 13, Removing: 0, Not Upgrading: 23 Download size: 18.4 MB Space needed: 52.5 MB / 22.5 GB available Get:1 http://archive.ubuntu.com/ubuntu resolute/main amd64 libjson-perl all 4.10000-1 [81.9 kB] Get:2 http://archive.ubuntu.com/ubuntu resolute/main amd64 postgresql-client-common all 290ubuntu1 [49.4 kB] Get:3 http://archive.ubuntu.com/ubuntu resolute/main amd64 ssl-cert all 1.1.3ubuntu2 [18.8 kB] Get:4 http://archive.ubuntu.com/ubuntu resolute/main amd64 postgresql-common all 290ubuntu1 [101 kB] Get:5 http://archive.ubuntu.com/ubuntu resolute/main amd64 libcommon-sense-perl amd64 3.75-3build5 [20.5 kB] Get:6 http://archive.ubuntu.com/ubuntu resolute/main amd64 libtypes-serialiser-perl all 1.01-1 [11.6 kB] Get:7 http://archive.ubuntu.com/ubuntu resolute/main amd64 libjson-xs-perl amd64 4.040-1 [84.4 kB] Get:8 http://archive.ubuntu.com/ubuntu resolute-updates/main amd64 libpq5 amd64 18.6-0ubuntu0.26.04.1 [163 kB] Get:9 http://archive.ubuntu.com/ubuntu resolute/main amd64 liburing2 amd64 2.14-1 [25.4 kB] Get:10 http://archive.ubuntu.com/ubuntu resolute-updates/main amd64 postgresql-client-18 amd64 18.6-0ubuntu0.26.04.1 [1401 kB] Get:11 http://archive.ubuntu.com/ubuntu resolute-updates/main amd64 postgresql-18 amd64 18.6-0ubuntu0.26.04.1 [6002 kB] Get:12 http://archive.ubuntu.com/ubuntu resolute/main amd64 postgresql all 18+290ubuntu1 [18.6 kB] Get:13 http://archive.ubuntu.com/ubuntu resolute-updates/main amd64 postgresql-18-jit amd64 18.6-0ubuntu0.26.04.1 [10.4 MB] Fetched 18.4 MB in 24s (768 kB/s) Preconfiguring packages ... /var/cache/debconf/tmp.ci/postgresql.config.draOY4: 12: pg_lsclusters: not found Selecting previously unselected package libjson-perl. (Reading database ... 91510 files and directories currently installed.) Preparing to unpack .../00-libjson-perl_4.10000-1_all.deb ... Unpacking libjson-perl (4.10000-1) ... Selecting previously unselected package postgresql-client-common. Preparing to unpack .../01-postgresql-client-common_290ubuntu1_all.deb ... Unpacking postgresql-client-common (290ubuntu1) ... Selecting previously unselected package ssl-cert. Preparing to unpack .../02-ssl-cert_1.1.3ubuntu2_all.deb ... Unpacking ssl-cert (1.1.3ubuntu2) ... Selecting previously unselected package postgresql-common. Preparing to unpack .../03-postgresql-common_290ubuntu1_all.deb ... Adding 'diversion of /usr/bin/pg_config to /usr/bin/pg_config.libpq-dev by postgresql-common' Unpacking postgresql-common (290ubuntu1) ... Selecting previously unselected package libcommon-sense-perl:amd64. Preparing to unpack .../04-libcommon-sense-perl_3.75-3build5_amd64.deb ... Unpacking libcommon-sense-perl:amd64 (3.75-3build5) ... Selecting previously unselected package libtypes-serialiser-perl. Preparing to unpack .../05-libtypes-serialiser-perl_1.01-1_all.deb ... Unpacking libtypes-serialiser-perl (1.01-1) ... Selecting previously unselected package libjson-xs-perl. Preparing to unpack .../06-libjson-xs-perl_4.040-1_amd64.deb ... Unpacking libjson-xs-perl (4.040-1) ... Selecting previously unselected package libpq5:amd64. Preparing to unpack .../07-libpq5_18.6-0ubuntu0.26.04.1_amd64.deb ... Unpacking libpq5:amd64 (18.6-0ubuntu0.26.04.1) ... Selecting previously unselected package liburing2:amd64. Preparing to unpack .../08-liburing2_2.14-1_amd64.deb ... Unpacking liburing2:amd64 (2.14-1) ... Selecting previously unselected package postgresql-client-18. Preparing to unpack .../09-postgresql-client-18_18.6-0ubuntu0.26.04.1_amd64.deb ... Unpacking postgresql-client-18 (18.6-0ubuntu0.26.04.1) ... Selecting previously unselected package postgresql-18. Preparing to unpack .../10-postgresql-18_18.6-0ubuntu0.26.04.1_amd64.deb ... Unpacking postgresql-18 (18.6-0ubuntu0.26.04.1) ... Selecting previously unselected package postgresql. Preparing to unpack .../11-postgresql_18+290ubuntu1_all.deb ... Unpacking postgresql (18+290ubuntu1) ... Selecting previously unselected package postgresql-18-jit. Preparing to unpack .../12-postgresql-18-jit_18.6-0ubuntu0.26.04.1_amd64.deb ... Unpacking postgresql-18-jit (18.6-0ubuntu0.26.04.1) ... Setting up postgresql-client-common (290ubuntu1) ... Setting up libpq5:amd64 (18.6-0ubuntu0.26.04.1) ... Setting up libcommon-sense-perl:amd64 (3.75-3build5) ... Setting up ssl-cert (1.1.3ubuntu2) ... Created symlink '/etc/systemd/system/multi-user.target.wants/ssl-cert.service' → '/usr/lib/systemd/system/ssl-cert.service'. Setting up libtypes-serialiser-perl (1.01-1) ... Setting up libjson-perl (4.10000-1) ... Setting up liburing2:amd64 (2.14-1) ... Setting up libjson-xs-perl (4.040-1) ... Setting up postgresql-client-18 (18.6-0ubuntu0.26.04.1) ... update-alternatives: using /usr/share/postgresql/18/man/man1/psql.1.gz to provide /usr/share/man/man1/psql.1.gz (psql.1.gz) in auto mode Setting up postgresql-common (290ubuntu1) ... Creating config file /etc/postgresql-common/createcluster.conf with new version Building PostgreSQL dictionaries from installed myspell/hunspell packages... Removing obsolete dictionary files: Created symlink '/etc/systemd/system/multi-user.target.wants/postgresql.service' → '/usr/lib/systemd/system/postgresql.service'. Setting up postgresql-18 (18.6-0ubuntu0.26.04.1) ... Creating new PostgreSQL cluster 18/main ... /usr/lib/postgresql/18/bin/initdb -D /var/lib/postgresql/18/main --auth-local peer --auth-host scram-sha-256 --no-instructions The files belonging to this database system will be owned by user "postgres". This user must also own the server process. The database cluster will be initialized with locale "C.UTF-8". The default database encoding has accordingly been set to "UTF8". The default text search configuration will be set to "english". Data page checksums are enabled. fixing permissions on existing directory /var/lib/postgresql/18/main ... ok creating subdirectories ... ok selecting dynamic shared memory implementation ... posix selecting default "max_connections" ... 100 selecting default "shared_buffers" ... 128MB selecting default time zone ... Etc/UTC creating configuration files ... ok running bootstrap script ... ok performing post-bootstrap initialization ... ok syncing data to disk ... ok Setting up postgresql-18-jit (18.6-0ubuntu0.26.04.1) ... Setting up postgresql (18+290ubuntu1) ... Processing triggers for man-db (2.13.1-1build1) ... Processing triggers for libc-bin (2.43-2ubuntu2.4) ... Scanning processes... Scanning linux images... Running kernel seems to be up-to-date. No services need to be restarted. No containers need to be restarted. No user sessions are running outdated binaries. No VM guests are running outdated hypervisor (qemu) binaries on this host. $ 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 $ systemctl status postgresql --no-pager ● postgresql.service - PostgreSQL RDBMS Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: enabled) Active: active (exited) since Wed 2026-11-18 22:59:37 UTC; 11s ago Invocation: 50b54d1a329545748cec1a20f518eea7 Main PID: 4158 (code=exited, status=0/SUCCESS) Mem peak: 2M CPU: 6ms $ systemctl status postgresql@18-main --no-pager -n 0 ● postgresql@18-main.service - PostgreSQL Cluster 18-main Loaded: loaded (/usr/lib/systemd/system/postgresql@.service; enabled-runtime; preset: enabled) Active: active (running) since Wed 2026-11-18 22:59:42 UTC; 6s ago Invocation: a244473ff3ab4a2293ade05da10fcf0f Process: 5044 ExecStart=/usr/bin/pg_ctlcluster --skip-systemctl-redirect 18-main start (code=exited, status=0/SUCCESS) Main PID: 5049 (postgres) Tasks: 9[0;38:5:245m (limit: 1486) Memory: 23M (peak: 31.6M) CPU: 182ms CGroup: /system.slice/system-postgresql.slice/postgresql@18-main.service ├─[0;38:5:245m5049 /usr/lib/postgresql/18/bin/postgres -D /var/lib/postgresql/18/main -c config_file=/etc/postgresql/1… ├─[0;38:5:245m5050 "postgres: 18/main: io worker 0" ├─[0;38:5:245m5051 "postgres: 18/main: io worker 2" ├─[0;38:5:245m5052 "postgres: 18/main: io worker 1" ├─[0;38:5:245m5053 "postgres: 18/main: checkpointer " ├─[0;38:5:245m5054 "postgres: 18/main: background writer " ├─[0;38:5:245m5056 "postgres: 18/main: walwriter " ├─[0;38:5:245m5057 "postgres: 18/main: autovacuum launcher " └─[0;38:5:245m5058 "postgres: 18/main: logical replication launcher " $ sudo ss -ltnp | grep 5432 LISTEN 0 200 127.0.0.1:5432 0.0.0.0:* users:(("postgres",pid=5049,fd=7)) LISTEN 0 200 [::1]:5432 [::]:* users:(("postgres",pid=5049,fd=6)) $ sudo -u postgres psql -c 'SHOW server_version;' server_version ------------------------------------- 18.6 (Ubuntu 18.6-0ubuntu0.26.04.1) (1 row) $ psql psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "deploy" does not exist $ psql -U postgres psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: Peer authentication failed for user "postgres" $ sudo grep -v -e '^#' -e '^$' /etc/postgresql/18/main/pg_hba.conf local all postgres peer local all all peer host all all 127.0.0.1/32 scram-sha-256 host all all ::1/128 scram-sha-256 local replication all peer host replication all 127.0.0.1/32 scram-sha-256 host replication all ::1/128 scram-sha-256 $ sudo -u postgres createuser --pwprompt appuser Enter password for new role: Enter it again: $ sudo -u postgres createdb --owner appuser appdb $ psql -h localhost -U appuser -d appdb -c 'SELECT current_user, current_database();' Password for user appuser: current_user | current_database --------------+------------------ appuser | appdb (1 row) $ sudo -u postgres psql -c "SELECT rolname, left(rolpassword, 14) AS stored FROM pg_authid WHERE rolname = 'appuser';" rolname | stored ---------+---------------- appuser | SCRAM-SHA-256$ (1 row) $ echo "listen_addresses = '*'" | sudo tee /etc/postgresql/18/main/conf.d/listen.conf listen_addresses = '*' $ echo 'host appdb appuser 192.0.2.2/32 scram-sha-256' | sudo tee -a /etc/postgresql/18/main/pg_hba.conf host appdb appuser 192.0.2.2/32 scram-sha-256 $ sudo systemctl reload postgresql $ sudo ss -ltnp | grep 5432 LISTEN 0 200 127.0.0.1:5432 0.0.0.0:* users:(("postgres",pid=5049,fd=7)) LISTEN 0 200 [::1]:5432 [::]:* users:(("postgres",pid=5049,fd=6)) $ sudo grep listen_addresses /var/log/postgresql/postgresql-18-main.log 2026-11-18 22:59:55.546 UTC [5049] LOG: parameter "listen_addresses" cannot be changed without restarting the server $ sudo systemctl restart postgresql $ sudo ss -ltnp | grep 5432 LISTEN 0 200 0.0.0.0:5432 0.0.0.0:* users:(("postgres",pid=6701,fd=6)) LISTEN 0 200 [::]:5432 [::]:* users:(("postgres",pid=6701,fd=7)) $ psql 'host=203.0.113.10 user=appuser dbname=appdb connect_timeout=5' -c 'SELECT current_user, ssl, version AS tls FROM pg_stat_ssl WHERE pid = pg_backend_pid();' psql: error: connection to server at "203.0.113.10", port 5432 failed: timeout expired $ sudo ufw allow from 192.0.2.2 to any port 5432 proto tcp Rule added $ psql 'host=203.0.113.10 user=appuser dbname=appdb connect_timeout=5' -c 'SELECT current_user, ssl, version AS tls FROM pg_stat_ssl WHERE pid = pg_backend_pid();' Password for user appuser: current_user | ssl | tls --------------+-----+--------- appuser | t | TLSv1.3 (1 row) $ psql 'host=203.0.113.10 user=postgres dbname=postgres connect_timeout=5' psql: error: connection to server at "203.0.113.10", port 5432 failed: FATAL: no pg_hba.conf entry for host "192.0.2.2", user "postgres", database "postgres", SSL encryption connection to server at "203.0.113.10", port 5432 failed: FATAL: no pg_hba.conf entry for host "192.0.2.2", user "postgres", database "postgres", no encryption $ sudo ufw status Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere 5432/tcp ALLOW 192.0.2.2 OpenSSH (v6) ALLOW Anywhere (v6) $ sudo ufw delete allow from 192.0.2.2 to any port 5432 proto tcp Rule deleted $ sudo rm /etc/postgresql/18/main/conf.d/listen.conf $ sudo sed -i '/^host appdb appuser/d' /etc/postgresql/18/main/pg_hba.conf $ sudo systemctl restart postgresql $ sudo ss -ltnp | grep 5432 LISTEN 0 200 127.0.0.1:5432 0.0.0.0:* users:(("postgres",pid=8167,fd=7)) LISTEN 0 200 [::1]:5432 [::]:* users:(("postgres",pid=8167,fd=6)) $ sudo -u postgres dropdb appdb && sudo -u postgres dropuser appuser $ sudo -u postgres psql -Atc "SELECT count(*) FROM pg_roles WHERE rolname = 'appuser';" 0 ------------------------------------------------------------------------------ The check-your-server script, its full screen before the steps ------------------------------------------------------------------------------ ServerHandbook check, install-postgresql-ubuntu (proved 2026-11-18) FAIL a PostgreSQL cluster "main" is online on port 5432 fix: https://serverhandbook.com/kb/install-postgresql-ubuntu#step-1-install FAIL PostgreSQL starts at boot fix: https://serverhandbook.com/kb/install-postgresql-ubuntu#step-2-running 0 passed, 2 failed To fix a FAIL, follow the steps in https://serverhandbook.com/kb/install-postgresql-ubuntu ------------------------------------------------------------------------------ The check-your-server script, its full screen after the steps ------------------------------------------------------------------------------ ServerHandbook check, install-postgresql-ubuntu (proved 2026-11-18) PASS a PostgreSQL cluster "main" is online on port 5432 PASS PostgreSQL starts at boot 2 passed, 0 failed ------------------------------------------------------------------------------ The check's source (scripts/live/checks/install-postgresql-ubuntu.mjs) As it ran. ------------------------------------------------------------------------------ /** * KB: install PostgreSQL on Ubuntu 26.04 and 24.04 (run per release, --releases=26.04,24.04). apt install postgresql * (the major version Ubuntu's archive has for that release, read from the box: never assumed); the cluster * (pg_lsclusters) and its two systemd units (postgresql = active (exited), postgresql@16-main = the server); the first * login as the postgres user; the two errors everyone hits next (role "deploy" does not exist; Peer authentication * failed) and why (pg_hba.conf as Ubuntu writes it); a role with a password and a database for an app; connect with * the password over localhost; the password is stored as SCRAM-SHA-256; it listens on localhost only. Optional: let * ONE other server connect (listen_addresses needs a restart, not a reload; pg_hba line for one address; ufw for one * address), over TLS by default; what a missing pg_hba line says. Undo. * * Runs on a fresh server, the local VM box or the rented server (LIVE_VPS). The "other server" is a network namespace * ON the box ("app", 192.0.2.2) joined by a virtual cable whose server end is 203.0.113.10, so it connects exactly as * the article shows (`psql -h 203.0.113.10`), through the server's real kernel, ufw and PostgreSQL. */ import { bareBox, reportBox } from '../lib.mjs' export const meta = { // The reader's check-your-server script (plan05 phase 3): proved by the runner on the stock server and after the steps. reader: [ { item: "a PostgreSQL cluster \"main\" is online on port 5432", cmd: "pg_lsclusters", expect: "\\s+main\\s+5432\\s+online\\s", step: "step-1-install" }, { item: "PostgreSQL starts at boot", cmd: "systemctl is-enabled postgresql", expect: "^enabled$", step: "step-2-running" }, ], } const RELEASE = process.env.LIVE_BOX_RELEASE || '24.04' const SERVER = '203.0.113.10' const APP = '192.0.2.2' // A throwaway test password, scrubbed from every saved file (ctx.secret). const PASS = '[password]' const check = (ctx) => bareBox(ctx, async ({ sudoUser }) => { ctx.secret(PASS) const { run, ask, L } = sudoUser() L('sudo true') // ── hidden: the firewall from the ufw article, and the app server's cable ──────────────── L('sudo ufw allow OpenSSH >/dev/null && sudo ufw --force enable >/dev/null') L( [ 'sudo ip netns add app', 'sudo ip link add v-app type veth peer name eth0 netns app', `sudo ip addr add ${SERVER} peer ${APP} dev v-app`, 'sudo ip link set v-app up', `sudo ip netns exec app ip addr add ${APP} peer ${SERVER} dev eth0`, 'sudo ip netns exec app ip link set eth0 up', 'sudo ip netns exec app ip link set lo up', ].join(' && '), ) /** A command the app server runs (as a plain user there). */ // runuser, not sudo: sudo inside the namespace warns that it cannot resolve the host name. const onApp = (cmd) => `sudo ip netns exec app runuser -u nobody -- ${cmd}` // ── step 1: install ────────────────────────────────────────────────────────────────────── const upd = run('sudo apt update', { id: 'apt-update', timeout: 600000 }) ctx.check('apt update reads the package lists', upd.exit === 0 && /Reading package lists|can be upgraded|All packages are up to date/.test(upd.out), upd.out.slice(-300)) const inst = run('sudo apt install -y postgresql', { id: 'apt-install', timeout: 900000 }) // the major version Ubuntu's archive installs on this release (16 on 24.04): read from the box const PG = L('ls /etc/postgresql') ctx.check(`apt install -y postgresql installs PostgreSQL ${PG} and creates the cluster ${PG}/main`, /^\d+$/.test(PG) && inst.exit === 0 && inst.out.includes(`Setting up postgresql-${PG} `) && inst.out.includes(`Creating new PostgreSQL cluster ${PG}/main`), inst.out.slice(-1500)) // ── step 2: is it running ──────────────────────────────────────────────────────────────── const ls = run('pg_lsclusters', { id: 'lsclusters' }) ctx.check(`pg_lsclusters: ${PG} main 5432 online, data in /var/lib/postgresql/${PG}/main`, new RegExp(`${PG}\\s+main\\s+5432\\s+online\\s+postgres\\s+/var/lib/postgresql/${PG}/main`).test(ls.out), ls.out, { reader: { cmd: 'pg_lsclusters', expect: '\\s+main\\s+5432\\s+online\\s' }, }) const st1 = run('systemctl status postgresql --no-pager', { id: 'status-wrapper' }) ctx.check('the postgresql unit is enabled and "active (exited)": a wrapper, not the server', /enabled;/.test(st1.out) && /Active: active \(exited\)/.test(st1.out), st1.out) const st2 = run(`systemctl status postgresql@${PG}-main --no-pager -n 0`, { id: 'status-cluster' }) ctx.check(`postgresql@${PG}-main is "active (running)": the server itself`, /Active: active \(running\)/.test(st2.out) && st2.out.includes(`postgres -D /var/lib/postgresql/${PG}/main`), st2.out) const ss1 = run(`sudo ss -ltnp | grep 5432`, { id: 'ss-local' }) ctx.check('it listens on 127.0.0.1:5432 and [::1]:5432 only', /127\.0\.0\.1:5432/.test(ss1.out) && /\[::1\]:5432/.test(ss1.out) && !/0\.0\.0\.0:5432/.test(ss1.out), ss1.out) // ── step 3: first login ────────────────────────────────────────────────────────────────── // (SELECT version() prints a line wider than the terminal, and psql then waits in its pager: SHOW is narrow.) const v = run(`sudo -u postgres psql -c 'SHOW server_version;'`, { id: 'version' }) ctx.check(`sudo -u postgres psql works: server_version ${PG}.x (Ubuntu ${PG}.x-…)`, v.exit === 0 && new RegExp(`${PG}\\.\\d+ \\(Ubuntu ${PG}\\.\\d+-\\S+\\)`).test(v.out), v.out) // ── the two errors ─────────────────────────────────────────────────────────────────────── const e1 = run('psql', { id: 'err-role' }) ctx.check('plain psql as deploy: FATAL: role "deploy" does not exist', e1.exit !== 0 && /connection to server on socket "\/var\/run\/postgresql\/\.s\.PGSQL\.5432" failed: FATAL:\s+role "deploy" does not exist/.test(e1.out), e1.out) const e2 = run('psql -U postgres', { id: 'err-peer' }) ctx.check('psql -U postgres as deploy: FATAL: Peer authentication failed for user "postgres"', e2.exit !== 0 && /FATAL:\s+Peer authentication failed for user "postgres"/.test(e2.out), e2.out) const hba = run(`sudo grep -v -e '^#' -e '^$' /etc/postgresql/${PG}/main/pg_hba.conf`, { id: 'hba' }) ctx.check('pg_hba.conf: local = peer, host 127.0.0.1/32 and ::1/128 = scram-sha-256', /^local\s+all\s+postgres\s+peer$/m.test(hba.out) && /^local\s+all\s+all\s+peer$/m.test(hba.out) && /^host\s+all\s+all\s+127\.0\.0\.1\/32\s+scram-sha-256$/m.test(hba.out), hba.out) // ── step 4: a role and a database for an app ───────────────────────────────────────────── const cu = ask('sudo -u postgres createuser --pwprompt appuser', [['Enter password for new role:', PASS], ['Enter it again:', PASS]], { id: 'createuser', timeout: 60 }) ctx.check('createuser --pwprompt asks for the password twice and creates the role', cu.exit === 0 && /Enter password for new role:/.test(cu.out) && /Enter it again:/.test(cu.out), cu.out) const cd = run('sudo -u postgres createdb --owner appuser appdb', { id: 'createdb' }) ctx.check('createdb --owner appuser appdb prints nothing on success', cd.exit === 0 && cd.out.trim() === '', cd.out) const conn = ask(`psql -h localhost -U appuser -d appdb -c 'SELECT current_user, current_database();'`, [['Password for user appuser:', PASS]], { id: 'connect-local', timeout: 60 }) ctx.check('psql -h localhost -U appuser asks for the password and connects to appdb', conn.exit === 0 && /Password for user appuser:/.test(conn.out) && /appuser\s+\|\s+appdb/.test(conn.out), conn.out) const scram = run(`sudo -u postgres psql -c "SELECT rolname, left(rolpassword, 14) AS stored FROM pg_authid WHERE rolname = 'appuser';"`, { id: 'scram' }) ctx.check('the password is stored as a SCRAM-SHA-256 verifier, not as text', /appuser\s+\|\s+SCRAM-SHA-256\$/.test(scram.out), scram.out) // ── optional: one other server ─────────────────────────────────────────────────────────── run(`echo "listen_addresses = '*'" | sudo tee /etc/postgresql/${PG}/main/conf.d/listen.conf`, { id: 'listen-conf' }) run(`echo 'host appdb appuser ${APP}/32 scram-sha-256' | sudo tee -a /etc/postgresql/${PG}/main/pg_hba.conf`, { id: 'hba-add' }) run('sudo systemctl reload postgresql', { id: 'reload' }) const ss2 = run(`sudo ss -ltnp | grep 5432`, { id: 'ss-after-reload' }) ctx.check('after a reload it still listens on localhost only', /127\.0\.0\.1:5432/.test(ss2.out) && !/0\.0\.0\.0:5432/.test(ss2.out), ss2.out) const log = run(`sudo grep listen_addresses /var/log/postgresql/postgresql-${PG}-main.log`, { id: 'log-restart' }) ctx.check('the log says: parameter "listen_addresses" cannot be changed without restarting the server', /parameter "listen_addresses" cannot be changed without restarting the server/.test(log.out), log.out) run('sudo systemctl restart postgresql', { id: 'restart' }) const ss3 = run(`sudo ss -ltnp | grep 5432`, { id: 'ss-after-restart' }) ctx.check('after a restart it listens on every address (0.0.0.0:5432 and [::]:5432)', /0\.0\.0\.0:5432/.test(ss3.out) && /\[::\]:5432/.test(ss3.out), ss3.out) const REMOTE = `psql 'host=${SERVER} user=appuser dbname=appdb connect_timeout=5' -c 'SELECT current_user, ssl, version AS tls FROM pg_stat_ssl WHERE pid = pg_backend_pid();'` const r1 = run(onApp(REMOTE), { show: REMOTE, id: 'remote-blocked', timeout: 60000 }) ctx.check('from the app server, before the firewall rule: timeout expired (ufw drops it)', r1.exit !== 0 && /timeout expired/.test(r1.out), r1.out) const uf = run(`sudo ufw allow from ${APP} to any port 5432 proto tcp`, { id: 'ufw-allow' }) ctx.check('ufw adds the rule for that address only', /Rule added/.test(uf.out), uf.out) const r2 = ask(onApp(REMOTE), [['Password for user appuser:', PASS]], { show: REMOTE, id: 'remote-ok', timeout: 60 }) ctx.check('from the app server: password, then connected as appuser over TLS (ssl = t, TLSv1.3)', r2.exit === 0 && /appuser\s+\|\s+t\s+\|\s+TLSv1\.3/.test(r2.out), r2.out) const PGQ = `psql 'host=${SERVER} user=postgres dbname=postgres connect_timeout=5'` const r3 = run(onApp(PGQ), { show: PGQ, id: 'remote-no-entry' }) ctx.check('as postgres from the app server: no pg_hba.conf entry for host "192.0.2.2", user "postgres"', r3.exit !== 0 && /no pg_hba\.conf entry for host "192\.0\.2\.2", user "postgres", database "postgres"/.test(r3.out), r3.out) const uls = run('sudo ufw status', { id: 'ufw-status' }) ctx.check('ufw status: 5432/tcp ALLOW from 192.0.2.2 only', /5432\/tcp\s+ALLOW\s+192\.0\.2\.2/.test(uls.out) && !/5432\/tcp\s+ALLOW\s+Anywhere/.test(uls.out), uls.out) const act = L(`systemctl is-active postgresql@${PG}-main`) ctx.check('(the server is running at the end of the steps)', act === 'active', act, { reader: { cmd: 'systemctl is-enabled postgresql', expect: '^enabled$' }, }) ctx.stepsDone() // the article's steps end here; the undo follows reportBox( ctx, L, `A fresh Ubuntu ${RELEASE} server from Ubuntu’s cloud image (or a provider’s stock image) with ufw on, as the ufw article leaves it, reached over SSH as a sudo user. The “app server” is a second machine on its own network address (192.0.2.2), joined to the server by a virtual cable, so its connections pass through the server’s real firewall.`, ['postgresql', `postgresql-${PG}`, 'postgresql-common', `postgresql-client-${PG}`, 'ufw'], ) // ── undo ───────────────────────────────────────────────────────────────────────────────── run(`sudo ufw delete allow from ${APP} to any port 5432 proto tcp`, { id: 'undo-ufw' }) run(`sudo rm /etc/postgresql/${PG}/main/conf.d/listen.conf`, { id: 'undo-listen' }) run(`sudo sed -i '/^host appdb appuser/d' /etc/postgresql/${PG}/main/pg_hba.conf`, { id: 'undo-hba' }) run('sudo systemctl restart postgresql', { id: 'undo-restart' }) const ss4 = run(`sudo ss -ltnp | grep 5432`, { id: 'undo-ss' }) ctx.check('undo: localhost only again', /127\.0\.0\.1:5432/.test(ss4.out) && !/0\.0\.0\.0:5432/.test(ss4.out), ss4.out) const dd = run('sudo -u postgres dropdb appdb && sudo -u postgres dropuser appuser', { id: 'undo-drop' }) ctx.check('undo: dropdb and dropuser print nothing on success', dd.exit === 0 && dd.out.trim() === '', dd.out) const gone = run(`sudo -u postgres psql -Atc "SELECT count(*) FROM pg_roles WHERE rolname = 'appuser';"`, { id: 'undo-check' }) ctx.check('undo: the role is gone (count 0)', gone.out.trim() === '0', gone.out) }) export default check