Short answer
Run sudo apt update and sudo apt install -y postgresql. Ubuntu 26.04 installs PostgreSQL 18 (24.04: 16), creates the cluster 18/main and starts it on port 5432, for this server only. Log in with sudo -u postgres psql, then make a user with createuser --pwprompt and a database with createdb --owner.
Tested on Ubuntu 26.04 and 24.04, two separate runs. On 24.04? 4 sections differ, each step marked “On Ubuntu 24.04” right under it: Step 2: Check that it is running, “role does not exist” and “Peer authentication failed”, Optional: let one other server connect, Undo. Everything that differed
Before you start
- An Ubuntu 26.04 or 24.04 server and a user with sudo rights (create a sudo user). The test server had the firewall from the ufw article on, with only SSH open.
- This page installs the PostgreSQL that Ubuntu ships: version 18 on 26.04, 16 on 24.04. The version is part of every config path (
/etc/postgresql/18/main); on 24.04 write 16 where this page says 18. It gets security updates through the normalapt upgrade, with no extra repository. PostgreSQL’s own apt repository has newer major versions; that is a separate install and not covered here.
Step 1: Install PostgreSQL
$ sudo apt updateHit:1 http://archive.ubuntu.com/ubuntu resolute InReleaseGet: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 postgresqlInstalling: 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 modeSetting up postgresql-common (290ubuntu1) ...Creating config file /etc/postgresql-common/createcluster.conf with new versionBuilding 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-instructionsThe 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 ... okcreating subdirectories ... okselecting dynamic shared memory implementation ... posixselecting default "max_connections" ... 100selecting default "shared_buffers" ... 128MBselecting default time zone ... Etc/UTCcreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... oksyncing data to disk ... okSetting 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.The install creates a database cluster called main, the set of databases one PostgreSQL server runs, and starts it. There is nothing more to run before you can use it.
Step 2: Check that it is running
pg_lsclusters lists every cluster with its version, port, status and where its data lives:
$ pg_lsclustersVer Cluster Port Status Owner Data directory Log file18 main 5432 online postgres /var/lib/postgresql/18/main /var/log/postgresql/postgresql-18-main.logMany guides check systemctl status postgresql and stop there. Look at what it says:
$ 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“active (exited)” is normal, but it proves nothing: this unit only runs /bin/true. It is a switch for all clusters at once. The server itself is the unit named after the cluster:
$ 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 (limit: 1486) Memory: 23M (peak: 31.6M) CPU: 182ms CGroup: /system.slice/system-postgresql.slice/postgresql@18-main.service ├─5049 /usr/lib/postgresql/18/bin/postgres -D /var/lib/postgresql/18/main -c config_file=/etc/postgresql/1… ├─5050 "postgres: 18/main: io worker 0" ├─5051 "postgres: 18/main: io worker 2" ├─5052 "postgres: 18/main: io worker 1" ├─5053 "postgres: 18/main: checkpointer " ├─5054 "postgres: 18/main: background writer " ├─5056 "postgres: 18/main: walwriter " ├─5057 "postgres: 18/main: autovacuum launcher " └─5058 "postgres: 18/main: logical replication launcher "On Ubuntu 24.04
On 24.04 the version is 16, so the path says 16:
$ systemctl status postgresql@16-main --no-pager -n 0● postgresql@16-main.service - PostgreSQL Cluster 16-main Loaded: loaded (/usr/lib/systemd/system/postgresql@.service; enabled-runtime; preset: enabled) Active: active (running) since Wed 2026-11-18 23:02:29 UTC; 5s ago Process: 4664 ExecStart=/usr/bin/pg_ctlcluster --skip-systemctl-redirect 16-main start (code=exited, status=0/SUCCESS) Main PID: 4669 (postgres) Tasks: 6 (limit: 2315) Memory: 18.6M (peak: 27.5M) CPU: 183ms CGroup: /system.slice/system-postgresql.slice/postgresql@16-main.service ├─4669 /usr/lib/postgresql/16/bin/postgres -D /var/lib/postgresql/16/main -c config_file=/etc/postgresql/1… ├─4670 "postgres: 16/main: checkpointer " ├─4671 "postgres: 16/main: background writer " ├─4673 "postgres: 16/main: walwriter " ├─4674 "postgres: 16/main: autovacuum launcher " └─4675 "postgres: 16/main: logical replication launcher "“active (running)” with the postgres processes under it: that is the database server. Both units are enabled, so systemd starts them at boot. It listens only on this server’s own addresses, so nothing outside can reach it yet:
$ sudo ss -ltnp | grep 5432LISTEN 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))Step 3: Log in for the first time
The install made a Linux user postgres and a database role with the same name, the superuser. Run psql as that Linux user:
$ sudo -u postgres psql -c 'SHOW server_version;' server_version------------------------------------- 18.6 (Ubuntu 18.6-0ubuntu0.26.04.1)(1 row)Without -c you get the postgres=# prompt; leave it with \q. No password was asked, and none is set. That is safe: only someone who can already use sudo on this server can log in this way.
“role does not exist” and “Peer authentication failed”
These are the two errors almost everyone sees next. Plain psql as your own user:
$ psqlpsql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "deploy" does not existpsql logs in with your Linux user name, deploy here, and there is no database role with that name. Asking for the postgres role instead:
$ psql -U postgrespsql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: Peer authentication failed for user "postgres"This one is not about a password. Ubuntu sets up local logins with the peer method: PostgreSQL asks the system which Linux user is on the other end of the socket, and lets you in only as the role with that same name. deploy is not postgres, so it says no. You can see the rules in pg_hba.conf:
$ sudo grep -v -e '^#' -e '^$' /etc/postgresql/18/main/pg_hba.conflocal all postgres peerlocal all all peerhost all all 127.0.0.1/32 scram-sha-256host all all ::1/128 scram-sha-256local replication all peerhost replication all 127.0.0.1/32 scram-sha-256host replication all ::1/128 scram-sha-256On Ubuntu 24.04
On 24.04 the version is 16, so the path says 16:
$ sudo grep -v -e '^#' -e '^$' /etc/postgresql/16/main/pg_hba.conflocal all postgres peerlocal all all peerhost all all 127.0.0.1/32 scram-sha-256host all all ::1/128 scram-sha-256local replication all peerhost replication all 127.0.0.1/32 scram-sha-256host replication all ::1/128 scram-sha-256local … peer: logins over the socket file, which is whatpsqluses without-h, need a matching Linux user.sudo -u postgres psqlis how you pass that check.host … 127.0.0.1/32 scram-sha-256: logins over TCP from this server need a password. That is what your apps use, with step 4’s user.- Do not change
peertotrustto make the error go away:trustlets anyone on the server log in as any role, the superuser too.
Step 4: Make a user and a database for your app
Give each app its own role with a password and its own database, never the postgres superuser. --pwprompt asks for the password, so it does not end up in your shell history:
$ sudo -u postgres createuser --pwprompt appuserEnter password for new role:Enter it again:$ sudo -u postgres createdb --owner appuser appdbBoth print nothing when they work. Now log in as the app would, over TCP with the password:
$ 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)An app uses the same details as a connection string: postgresql://appuser:<password>@localhost:5432/appdb. PostgreSQL keeps only a SCRAM-SHA-256 verifier of the password, not the password itself:
$ 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)Optional: let one other server connect
Skip this when the app runs on the same server; localhost is safer. When an app server elsewhere needs the database, open it to that ONE address, in three places: PostgreSQL must listen on the network, pg_hba.conf must allow the address, and the firewall must let it through. In the test the app server was at 192.0.2.2 and the database server at 203.0.113.10.
Listen on all addresses. Put the line in its own file in conf.d, a folder Ubuntu’s postgresql.conf reads, so the main file stays as the package wrote it:
$ echo "listen_addresses = '*'" | sudo tee /etc/postgresql/18/main/conf.d/listen.conflisten_addresses = '*'On Ubuntu 24.04
On 24.04 the version is 16, so the path says 16:
$ echo "listen_addresses = '*'" | sudo tee /etc/postgresql/16/main/conf.d/listen.conflisten_addresses = '*'Allow the app server, for one database and one role. /32 means exactly one address:
$ echo 'host appdb appuser 192.0.2.2/32 scram-sha-256' | sudo tee -a /etc/postgresql/18/main/pg_hba.confhost appdb appuser 192.0.2.2/32 scram-sha-256On Ubuntu 24.04
On 24.04 the version is 16, so the path says 16:
$ echo 'host appdb appuser 192.0.2.2/32 scram-sha-256' | sudo tee -a /etc/postgresql/16/main/pg_hba.confhost appdb appuser 192.0.2.2/32 scram-sha-256A reload is enough for pg_hba.conf, but not for listen_addresses. After a reload PostgreSQL still listens on localhost only:
$ sudo systemctl reload postgresql$ sudo ss -ltnp | grep 5432LISTEN 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))And the log says why:
$ sudo grep listen_addresses /var/log/postgresql/postgresql-18-main.log2026-11-18 22:59:55.546 UTC [5049] LOG: parameter "listen_addresses" cannot be changed without restarting the serverOn Ubuntu 24.04
On 24.04 the version is 16, so the path says 16:
$ sudo grep listen_addresses /var/log/postgresql/postgresql-16-main.log2026-11-18 23:02:40.233 UTC [4669] LOG: parameter "listen_addresses" cannot be changed without restarting the server$ sudo systemctl restart postgresql$ sudo ss -ltnp | grep 5432LISTEN 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))Now it listens on every address. The firewall still stops the app server. On the app server, which needs the PostgreSQL client (the postgresql-client package):
$ 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“timeout expired” means the packets were dropped: that is ufw. Allow the app server’s address on port 5432, and only that address:
$ sudo ufw allow from 192.0.2.2 to any port 5432 proto tcpRule 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)ssl is t: the connection is encrypted with TLS 1.3 although nothing was set up for it. Ubuntu turns SSL on for every new cluster, with the server’s self-signed certificate. That encrypts the connection, but it does not prove to the app that it reached your server. For that, give PostgreSQL a real certificate and connect with sslmode=verify-full.
“no pg_hba.conf entry for host”
This is what an address, role or database that no line allows gets. In the test the app server asked for the postgres role, which its line does not cover:
$ 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 encryptionconnection 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 encryptionIt is refused twice because psql tries with SSL first, then without. Read the message: it names the address, role and database PostgreSQL saw, and those three must match one host line. Change the line, then sudo systemctl reload postgresql. A reload is enough for pg_hba.conf. The firewall rule, for reference:
$ sudo ufw statusStatus: active To Action From-- ------ ----OpenSSH ALLOW Anywhere5432/tcp ALLOW 192.0.2.2OpenSSH (v6) ALLOW Anywhere (v6)What these steps changed on the server
Both runs recorded the server before the first step and after the last, with remote access still open (see the Tested on box below): 13 packages on 26.04 and 12 on 24.04, two services enabled at boot (postgresql.service and ssl-cert.service, which makes the self-signed certificate), port 5432 listening on every address, the postgres user and group added to /etc/passwd and /etc/group, the ufw rule, and about 245 files in /etc (most of them /etc/postgresql and the certificate files). The disk holds about 315 MB more on 26.04 and 445 MB on 24.04, including the package lists from apt update.
Ubuntu 26.04 vs 24.04: what changed
Every step, error message and result was the same on both runs, the TLS 1.3 connection too. What differed:
| What | Ubuntu 26.04 | Ubuntu 24.04 |
|---|---|---|
| System | Ubuntu 26.04.1 LTS7.0.0-43-generic | Ubuntu 24.04.5 LTS6.8.0-147-generic |
| postgresql | 18+290ubuntu1 | 16+257build1.1 |
| postgresql-18 | 18.6-0ubuntu0.26.04.1 | not installed |
| postgresql-common | 290ubuntu1 | 257build1.1 |
| postgresql-client-18 | 18.6-0ubuntu0.26.04.1 | not installed |
| ufw | 0.36.2-9build1 | 0.36.2-6 |
| postgresql-16 | not installed | 16.15-0ubuntu0.24.04.1 |
| postgresql-client-16 | not installed | 16.15-0ubuntu0.24.04.1 |
| PostgreSQL version from Ubuntu’s archive | 18.6 | 16.15 |
| The version in every path and unit name | /etc/postgresql/18/main, postgresql@18-main | /etc/postgresql/16/main, postgresql@16-main |
Extra packages apt install postgresql pulls in | postgresql-18-jit and liburing2 (13 packages in all) | libllvm17t64 (12 packages in all) |
apt install output | apt 3: Installing: list and a Summary: block | apt 2: The following NEW packages will be installed: |
| Proof | Run log, 28 checks passed | Run log, 28 checks passed |
Undo
Close remote access again: the firewall rule, the listen file and the pg_hba.conf line, then a restart:
$ sudo ufw delete allow from 192.0.2.2 to any port 5432 proto tcpRule deleted$ sudo rm /etc/postgresql/18/main/conf.d/listen.conf$ sudo sed -i '/^host appdb appuser/d' /etc/postgresql/18/main/pg_hba.confOn Ubuntu 24.04
On 24.04 the paths say 16:
$ sudo rm /etc/postgresql/16/main/conf.d/listen.conf$ sudo sed -i '/^host appdb appuser/d' /etc/postgresql/16/main/pg_hba.conf$ sudo systemctl restart postgresql$ sudo ss -ltnp | grep 5432LISTEN 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';"00: the role is gone, and its database with it.
