>_ blog · databases

How To Install PostgreSQL 16 on Ubuntu 24.04

A tested, production-minded guide to installing PostgreSQL 16 on Ubuntu 24.04 — from the official repository to secure remote access, tuning and backups.

Version:PostgreSQL 16
SupportedBy DevNextBD Team4 min read

What's different in PostgreSQL 16

  • Included in Ubuntu 24.04's own repositories — no extra repository needed
  • pg_stat_io shows exactly where reads and writes happen
  • Logical replication can run from standby servers
  • SQL/JSON constructors (JSON_ARRAY, JSON_OBJECT) and IS JSON checks
  • Supported until 9 November 2028 — plan an upgrade to 18 for systems that will live longer

Supported until 9 November 2028

PostgreSQL is the open-source database we reach for most often: reliable, fast and rich in features. This guide installs PostgreSQL 16 on Ubuntu 24.04 LTS, secures it, creates a database for your application, allows remote connections safely and sets up backups. Every command was written for a fresh server; read the explanation before running each one.

Which version should you install?

Each major version is supported for five years. Pick the newest one your application, drivers and hosting support.

VersionReleasedSupported untilChoose it when
1825 September 202514 November 2030Starting something new
1726 September 20248 November 2029Your tools don't support 18 yet
1614 September 20239 November 2028You want Ubuntu's own package

You are reading the guide for PostgreSQL 16, released on 14 September 2023 and supported until 9 November 2028.

Prerequisites

  • A server running Ubuntu 24.04 with at least 1 GB of RAM (2 GB or more for production).
  • A user with sudo rights. Don't work as root.
  • The UFW firewall enabled, with SSH allowed: sudo ufw allow OpenSSH && sudo ufw enable.

Step 1 — Choose where to install from

Ubuntu 24.04 ships PostgreSQL 16 in its own repositories, so you can install it without adding anything:

Server
sudo apt update
sudo apt install -y postgresql

Ubuntu's package receives security fixes for the life of the release. If you prefer to get every new 16.x minor release as soon as the PostgreSQL project publishes it — or you want the same steps on Ubuntu 22.04 — use the official PostgreSQL (PGDG) repository instead:

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

Step 2 — Check that the server is running

The package creates a database cluster called main and starts it straight away. List the clusters:

Server
pg_lsclusters
Output
Ver Cluster Port Status Owner    Data directory              Log file
16  main    5432 online postgres /var/lib/postgresql/16/main /var/log/postgresql/postgresql-16-main.log

Note the paths — every version has its own:

  • Configuration: /etc/postgresql/16/main/postgresql.conf
  • Client access rules: /etc/postgresql/16/main/pg_hba.conf
  • Data: /var/lib/postgresql/16/main

Make sure it starts after a reboot:

Server
sudo systemctl enable --now postgresql

Step 3 — Connect with psql

Installation creates an operating-system user and a database superuser, both called postgres. Open the PostgreSQL shell as that user:

Server
sudo -u postgres psql

Confirm the version, then quit with \q:

psql
SELECT version();
Output
PostgreSQL 16.x (Ubuntu 16.x-1.pgdg24.04+1) on x86_64-pc-linux-gnu, compiled by gcc …

Step 4 — Create a user and a database for your application

Never connect your application as postgres. Create a separate role with a strong password and a database it owns:

Server
sudo -u postgres createuser --pwprompt appuser
sudo -u postgres createdb --owner=appuser appdb

Passwords are stored with SCRAM-SHA-256, the secure default since PostgreSQL 14. Test the new login over a local TCP connection:

Server
psql -h 127.0.0.1 -U appuser -d appdb -c "SELECT current_user, current_database();"

Step 5 — Allow remote connections (only if you need them)

By default PostgreSQL listens only on localhost. If your application runs on the same server, skip this step — it is the safest setup.

To allow one application server (here 203.0.113.10) to connect, edit /etc/postgresql/16/main/postgresql.conf:

postgresql.conf
listen_addresses = 'localhost,10.0.0.5'   # this server's private IP

Then add one rule to the end of /etc/postgresql/16/main/pg_hba.conf. It allows only that database, that user and that address, over an encrypted connection:

pg_hba.conf
hostssl  appdb  appuser  203.0.113.10/32  scram-sha-256

Restart PostgreSQL and open the port only for that address:

Server
sudo systemctl restart postgresql
sudo ufw allow from 203.0.113.10 to any port 5432 proto tcp

Step 6 — Basic tuning

The defaults are deliberately small. For a server that runs mainly PostgreSQL, these are sensible starting points in postgresql.conf (example for 8 GB of RAM):

postgresql.conf
shared_buffers = 2GB            # about 25% of RAM
effective_cache_size = 6GB      # about 75% of RAM
work_mem = 16MB                 # per sort/hash operation, so keep it modest
maintenance_work_mem = 512MB

Restart after changing shared_buffers. Measure with your real queries before tuning further.

Step 7 — Back up your data

Take a compressed logical backup with pg_dump, and test that it restores:

Server
sudo -u postgres pg_dump -Fc appdb > appdb-$(date +%F).dump
sudo -u postgres createdb appdb_restore_test
sudo -u postgres pg_restore -d appdb_restore_test appdb-$(date +%F).dump

Run backups on a schedule and copy them to another server or to object storage — a backup on the same disk is not a backup.

Upgrading to a newer major version later

Debian and Ubuntu packages make major upgrades manageable with pg_upgradecluster. Install the new version, remove the empty cluster it creates, then upgrade your existing cluster (example: from 16 to a newer NEW):

Server
sudo apt install -y postgresql-NEW
sudo pg_dropcluster NEW main --stop
sudo pg_upgradecluster -m upgrade 16 main

Test your application on the new version, then remove the old cluster with sudo pg_dropcluster 16 main. Always take a full backup first.

Conclusion

You now have PostgreSQL 16 running on Ubuntu 24.04 with a dedicated application user, safe remote access rules, basic tuning and tested backups. Next steps: set up monitoring (for example pg_stat_statements), automate backups, and plan an upgrade well before 9 November 2028.

All versions of this guide

Each version is a complete guide written and tested for that release.

VersionStatusSupported untilGuide
PostgreSQL 18Current14 November 2030Open
PostgreSQL 17Supported8 November 2029Open
PostgreSQL 16Supported9 November 2028You're reading this

#PostgreSQL#Ubuntu#Linux#Databases