Databases

Install PostgreSQL 16 on Ubuntu 24.04 / 22.04

Add the PGDG repository to Ubuntu 24.04 today and sudo apt install postgresql no longer gives you PostgreSQL 16. On our test box the unversioned metapackage resolved to PostgreSQL 19, so a server that has to stay on 16 must install the postgresql-16 package by name. That package name is the whole difference between a 16 install and an accidental major-version jump.

Original content from computingforgeeks.com - post 145060

This guide covers what is specific to 16 on Ubuntu 24.04 and 22.04: package sources, the support window, the 16-only features and the pg_upgradecluster path out. A fresh PGDG install of PostgreSQL 16.16 (run in November 2026 on Ubuntu 24.04 and 22.04 GCP VMs) took 12.8 seconds on 24.04 and 25.3 seconds on 22.04. If you are starting a new project with no version constraint, the PostgreSQL 17 install guide for Ubuntu covers TLS, roles and benchmarking in depth, and the PostgreSQL 19 guide covers the newest major.

When PostgreSQL 16 is still the right choice

PostgreSQL 16 is supported by the community until November 9, 2028, according to the PostgreSQL versioning policy. That is just under two years from now, which is enough for an application that is certified against 16, a managed-service replica that must match a 16 primary, or a vendor product whose support matrix stops at 16. If a search for “install postgresql 16 ubuntu 24.04” brought you here, one of those constraints is usually why. It is not enough for a new deployment that should run untouched until 2030. The table puts the 16 window next to the majors you can install from the same repository:

MajorFirst releaseCommunity end of lifeTime left (Nov 2026)
PostgreSQL 15October 13, 2022November 11, 2027about 1 year
PostgreSQL 16September 14, 2023November 9, 2028about 2 years
PostgreSQL 17September 26, 2024November 8, 2029about 3 years
PostgreSQL 18September 25, 2025November 14, 2030about 4 years
PostgreSQL 19November 19, 2026November 19, 2031about 5 years

The Ubuntu side matters as much as the PostgreSQL side. Ubuntu 24.04 ships 16 in its own archive and keeps it under standard security maintenance until May 2029, so the distro package outlives the community window by six months. Ubuntu 22.04 never shipped 16 at all (its archive stops at 14), and its standard support ends in May 2027. On 22.04 the PGDG repository is the only source, and the OS will need attention before the database does.

What 16 gives you over 15 is concrete. The PostgreSQL 16 release notes list the pg_stat_io view, SQL/JSON constructors, logical replication from standby servers and parallel apply of large transactions on subscribers. Step 3 checks the first two on the installed server.

Prerequisites

The lab used two e2-medium instances (2 vCPU, 4 GB RAM, 20 GB disk). That is a floor for testing, not a production size: shared_buffers, connection count and the working set of your largest tables decide the real RAM figure. You need:

  • Ubuntu 24.04 or 22.04: a server install with sudo access. The 24.04 host reported 24.04.5 LTS, the 22.04 host 22.04.5 LTS.
  • Outbound HTTPS: the server reaches apt.postgresql.org, which served a 1.3 MB package index on the first update.
  • Disk: the server and client packages took 69.4 MB after install, before any data.

Step 1: Add the PGDG repository

The PostgreSQL project packages every supported major for every supported Ubuntu LTS at apt.postgresql.org, and the postgresql-common package ships a script that adds the repository and its signing key in one step. That replaces the older echo ... > pgdg.list plus gpg --dearmor routine, which still works but leaves you to manage the key path by hand. Install the helper package first:

sudo apt update
sudo apt install -y postgresql-common

Then run the repository script with -y so it does not stop for a confirmation:

sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -y

On 24.04 the script picked the noble-pgdg suite, wrote a deb822 source file and refreshed the package lists:

PGDG apt.postgresql.org.sh script writing pgdg.sources for noble-pgdg on Ubuntu 24.04

The two releases end up with different source files. On 24.04 the script wrote /etc/apt/sources.list.d/pgdg.sources with an HTTPS URI and a Signed-By line pointing at the keyring under /usr/share/postgresql-common/pgdg/. On 22.04, with the older postgresql-common from the jammy archive, it wrote a one-line pgdg.list with a plain http:// URI. Both are signed, so the packages are verified either way, but configuration management that greps for pgdg.list will miss the 24.04 file.

Step 2: Install postgresql-16 by name

With PGDG enabled, 24.04 sees five builds of postgresql-16, three from PGDG and two from the Ubuntu archive, and apt prefers the PGDG one because its version string sorts higher. The unversioned postgresql metapackage is a different story. Simulating its install shows what it would pull in:

apt-cache policy postgresql-16
apt-get install -s postgresql | grep ^Inst

The PGDG candidate for 16 was one minor release ahead of the Ubuntu archive build, while the metapackage resolved to PostgreSQL 19, a new postgresql-common and a JIT package:

apt-cache policy postgresql-16 showing PGDG and Ubuntu candidates, and the postgresql metapackage pulling PostgreSQL 19

That is the pin. The postgresql-16 package only ever moves within the 16 line, so naming it keeps every future apt upgrade on minor releases, which is what the PostgreSQL project recommends you always run. Do not apt-mark hold it, because a hold also blocks the security fixes that arrive in those minors. Install the server and the matching client:

sudo apt install -y postgresql-16 postgresql-client-16

The 18.7 MB download installed three packages in 12.8 seconds on the 24.04 VM. The maintainer script runs initdb for you, with peer authentication on the local socket and scram-sha-256 on TCP:

Creating new PostgreSQL cluster 16/main ...
/usr/lib/postgresql/16/bin/initdb -D /var/lib/postgresql/16/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 disabled.

Note the last line. PostgreSQL 16 does not enable data checksums by default, and they cannot be switched on later without taking the cluster offline for pg_checksums. If checksums matter to you, drop and recreate the empty cluster now with sudo pg_dropcluster 16 main --stop followed by sudo pg_createcluster 16 main --start -- --data-checksums. The libpq5 pulled in alongside came from the 19 branch (19.0-2.pgdg24.04+1), which is fine because libpq talks to older servers.

Check the cluster and the service unit. On Ubuntu each cluster runs as its own postgresql@<version>-<name> unit, and the plain postgresql.service is only a wrapper that starts them all:

pg_lsclusters
psql --version
systemctl status postgresql@16-main --no-pager

The cluster came up online on port 5432 with 18.5 MB of memory in its cgroup and six tasks, which is the idle footprint of the default 128 MB shared_buffers configuration:

pg_lsclusters, psql --version and systemctl status for the PostgreSQL 16 main cluster on Ubuntu 24.04

Confirm from inside the server that the binary is the PGDG build rather than the Ubuntu one. The suffix after the version tells you which repository it came from:

sudo -u postgres psql -c 'SELECT version();'

A pgdg24.04 suffix means the PGDG package; an ubuntu0.24.04 suffix would mean the archive build won:

 PostgreSQL 16.16 (Ubuntu 16.16-1.pgdg24.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0, 64-bit
(1 row)

Skipping PGDG on 24.04 is a legitimate choice. sudo apt install postgresql-16 without Step 1 installs the archive build, which Canonical patches through noble-security. You trade a few weeks of delay on each minor release for one fewer third-party repository. On 22.04 there is no such choice.

Step 3: Check the features that are new in 16

If an application was written against 16, two of its additions are easy to verify from psql: the SQL/JSON IS JSON predicate and constructors, and the pg_stat_io view. Neither exists on 15, so these queries also make a quick check that a client is talking to the right server:

sudo -u postgres psql -c "SELECT '{\"a\":1}' IS JSON OBJECT AS is_obj, JSON_ARRAY(1,2,3) AS arr;"
sudo -u postgres psql -c "SELECT backend_type, object, context, reads, writes FROM pg_stat_io WHERE reads > 0 OR writes > 0 LIMIT 6;"

Both returned rows on the fresh cluster. The 1,020 writes by the standalone backend are the initdb bootstrap, and the client backend’s 158 reads are the catalog lookups from the psql sessions themselves:

PostgreSQL 16 IS JSON predicate, JSON_ARRAY constructor and pg_stat_io view output in psql

On a production server, pg_stat_io is the view to watch for the ratio of reads to hits per backend type, since it is the first release where that breakdown exists without an extension.

Step 4: Open the 16 cluster to your network

Every configuration file for this cluster sits under /etc/postgresql/16/main/, and the data directory under /var/lib/postgresql/16/main. The version in the path is the part that differs from the 17 guide; the settings themselves do not. Password hashing already defaults to scram-sha-256, so no md5 or trust edits are needed. Clients from a 10.0.1.0/24 application subnet need two edits:

  • postgresql.conf: set listen_addresses to *, because the package binds to localhost only.
  • pg_hba.conf: append a host rule for the subnet with scram-sha-256, since the four default host rules cover loopback addresses only.

Start with the listener:

sudo sed -i "s/^#listen_addresses = 'localhost'/listen_addresses = '*'/" /etc/postgresql/16/main/postgresql.conf

Then allow that subnet with password authentication and restart the cluster unit:

echo "host    all    all    10.0.1.0/24    scram-sha-256" | sudo tee -a /etc/postgresql/16/main/pg_hba.conf
sudo systemctl restart postgresql@16-main
sudo ss -tlnp | grep 5432
sudo -u postgres psql -Atc "SELECT type, database, user_name, address, auth_method FROM pg_hba_file_rules WHERE type='host';"

The listener moved to 0.0.0.0 and [::], and pg_hba_file_rules shows the new rule as the fifth host entry, after the four localhost rules the package created:

PostgreSQL 16 listening on 0.0.0.0:5432 with a scram-sha-256 pg_hba rule for 10.0.1.0/24 on Ubuntu 24.04

If UFW is active, allow the same subnet with sudo ufw allow from 10.0.1.0/24 to any port 5432 proto tcp. On later edits, run the pg_hba_file_rules query before the restart rather than after it: the view reads the file on disk, so a typo shows up as a row with an error value while the running server still uses the old rules. For server certificates, client sslmode settings and the common “no pg_hba.conf entry” errors, follow the remote access section of the 17 install guide and change 17 to 16 in each path.

Step 5: Create an application role and database

Create a login role and a database it owns. We also loaded 200,000 rows into a test table so the upgrade in Step 6 had real data to carry across:

sudo -u postgres psql -c "CREATE ROLE appuser LOGIN PASSWORD 'StrongPass#2026';"
sudo -u postgres createdb -O appuser appdb
sudo -u postgres psql -d appdb -c "CREATE TABLE orders AS SELECT g AS id, now() - g * interval '1 min' AS created FROM generate_series(1,200000) g;"

Connecting over TCP as the new role proves scram-sha-256 over TCP and the default TLS setup together (it matches the loopback rule, not the new subnet rule), since the Ubuntu packaging enables SSL with the system snakeoil certificate:

PGPASSWORD='StrongPass#2026' psql -h 127.0.0.1 -U appuser -d appdb -c '\conninfo'

The session negotiated TLS 1.3 without any certificate work on our side:

You are connected to database "appdb" as user "appuser" on host "127.0.0.1" at port "5432".
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)

The table measured 8,704 kB on disk. Replace the example password before you run this anywhere that matters, and keep it out of shell history by setting it from psql with \password appuser instead.

Step 6: Upgrade from 16 with pg_upgradecluster

The 16 line has two years left, so plan the exit now. Ubuntu’s pg_upgradecluster handles the whole move: it creates the new cluster, can run pg_upgrade, swaps the ports and copies postgresql.conf and pg_hba.conf. The target can be any newer major; we moved to 18 because that is where a 16 shop usually lands next. The 18 packages come from PGDG, so this step needs the repository from Step 1 even if you installed 16 from the Ubuntu archive. Install the new server packages alongside 16:

sudo apt install -y postgresql-18
pg_lsclusters

Many older guides tell you to drop an empty 18/main cluster at this point. On our 24.04 box installing postgresql-18 created no cluster at all, so pg_lsclusters still listed only 16 and the drop failed:

Error: specified cluster does not exist

Check pg_lsclusters first and only run pg_dropcluster 18 main --stop if an 18 row is listed. Next, pick the method on purpose. The man page for pg_upgradecluster states “The default is dump” on both the Ubuntu and the PGDG builds of postgresql-common, which means a full pg_dump and restore. That is safe but slow on a large database. --method=upgrade uses pg_upgrade and copies data files instead. Without -v the target is the newest major installed, so if postgresql-19 is also on the box, add -v 18 to stay on the path shown here:

sudo pg_upgradecluster --method=upgrade 16 main

The upgrade of the 200,000-row database finished in 9.2 seconds wall time, including the three-stage statistics rebuild that pg_upgradecluster runs after pg_upgrade. The 16 cluster stays on disk, stopped and moved to port 5433, and all rows were present on 18:

pg_upgradecluster moving PostgreSQL 16 to 18 on Ubuntu 24.04 with the 16 cluster down on port 5433

Leave the stopped 16 cluster in place until the application has run on the new major for a while, because it is your fastest rollback. Rolling back means starting 16 on 5433 (or swapping the ports back) and pointing the application at it, and every write made on 18 since the upgrade is lost. Settings you placed in /etc/postgresql/16/main/conf.d/ are a separate matter: the 17 guide found that drop-in files did not follow the cluster on upgrade, so copy them across by hand. Once you are satisfied, remove the old cluster with sudo pg_dropcluster 16 main and purge postgresql-16. For backups before the move, link mode, extension handling and the same procedure on Debian and Rocky Linux, see our PostgreSQL major version upgrade guide.

PostgreSQL 16 on Ubuntu 22.04 vs 24.04

The commands above are identical on 22.04; the measured results are not. Before PGDG, the jammy archive offered only the PostgreSQL 14 metapackage and no postgresql-16 at all. After Steps 1 and 2, the 22.04 VM ran the same 16 minor release from jammy-pgdg:

Ubuntu 22.04 archive offering PostgreSQL 14 and the PGDG postgresql-16 package installed with a 16 main cluster

Side by side, the two test hosts differed in these measured ways:

Measured on the test VMsUbuntu 24.04Ubuntu 22.04
PostgreSQL in the Ubuntu archive16 (one minor behind PGDG)14 only
PGDG suitenoble-pgdgjammy-pgdg
Source file written by the scriptpgdg.sources (deb822, https)pgdg.list (one line, http)
postgresql-16 install time12.8 s25.3 s
Cluster port and status after install5432, online5432, online
Ubuntu standard support endsMay 2029May 2027

Treat the install times as a single run each on shared cloud vCPUs, not a benchmark. The row that should decide your plan is the last one. A 22.04 server running 16 hits the end of Ubuntu standard support in May 2027, eighteen months before PostgreSQL 16 does, so the realistic sequence on 22.04 is to move the OS to 24.04 first and keep 16 as it is. On 24.04, the database reaches end of life first, in November 2028, and Step 6 is the work to schedule.

Keep reading

Upgrade Ubuntu 24.04 to Ubuntu 26.04 LTS (Step by Step) Ubuntu Upgrade Ubuntu 24.04 to Ubuntu 26.04 LTS (Step by Step) UFW Firewall Commands with Examples on Ubuntu 26.04 / 24.04 Security UFW Firewall Commands with Examples on Ubuntu 26.04 / 24.04 Backup and Restore Linux Systems with Timeshift Debian Backup and Restore Linux Systems with Timeshift Migrate Kafka from ZooKeeper to KRaft and Upgrade to 4.x Databases Migrate Kafka from ZooKeeper to KRaft and Upgrade to 4.x Build a FHIR MCP Server for MedGemma 2 with Ollama AI Build a FHIR MCP Server for MedGemma 2 with Ollama Install Ansible AWX on Ubuntu 24.04 / Debian 13 Ansible Install Ansible AWX on Ubuntu 24.04 / Debian 13

4 thoughts on “Install PostgreSQL 16 on Ubuntu 24.04 / 22.04”

    • Correct. PGDG has dropped focal β€” there is no focal-pgdg distribution anymore, so postgresql-16 from apt.postgresql.org is not available on 20.04. Options: upgrade to 22.04 (jammy) or 24.04 (noble), use Ubuntu 20.04 distro postgresql-12, or run PG 16 in Docker. The article will be refreshed to drop 20.04.

      Reply

Leave a Comment

Press ESC to close