Updated: 28 September 2026 · Applies to: Ubuntu 24.04, Debian 12, AlmaLinux 9 and Rocky Linux 9 (PostgreSQL 16 and 18)

Ubuntu 16.04 and PostgreSQL 10 are end of life. This guide installs a supported PostgreSQL on Ubuntu 24.04 (and Debian 12 / AlmaLinux 9 / Rocky Linux 9).

Option 1: the distribution's version

apt install postgresql                   # Ubuntu 24.04: PostgreSQL 16, Debian 12: 15
dnf install postgresql-server             # AlmaLinux / Rocky 9 (choose a stream with: dnf module list postgresql)
postgresql-setup --initdb && systemctl enable --now postgresql   # AlmaLinux / Rocky only

Option 2: the latest PostgreSQL (18) from the PostgreSQL project

# Ubuntu / Debian
apt install postgresql-common
/usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
apt install postgresql-18

# AlmaLinux / Rocky 9
dnf install https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
dnf -qy module disable postgresql
dnf install postgresql18-server
/usr/pgsql-18/bin/postgresql-18-setup initdb
systemctl enable --now postgresql-18

First steps

sudo -u postgres psql                        # log in as the admin role
CREATE ROLE appuser WITH LOGIN PASSWORD 'Strong-Password';
CREATE DATABASE appdb OWNER appuser;
\l                                            -- list databases
\du                                           -- list roles
\q                                            -- quit

Connect as the new user: psql -h 127.0.0.1 -U appuser appdb.

Allow connections from another server (optional)

  • In postgresql.conf set listen_addresses = '*' (or the server's private IP).
  • In pg_hba.conf add host appdb appuser 203.0.113.10/32 scram-sha-256.
  • Restart PostgreSQL and open port 5432 only for that IP in the firewall.

The files are in /etc/postgresql/<version>/main/ (Ubuntu/Debian), /var/lib/pgsql/data/ (AlmaLinux/Rocky packages) or /var/lib/pgsql/18/data/ (PostgreSQL project packages).

Backup and restore

sudo -u postgres pg_dump -Fc appdb > appdb.dump
sudo -u postgres pg_restore -d appdb appdb.dump

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Managed dedicated servers

our engineers look after the server for you, from $135 a month.

Was this answer helpful?

Still stuck? Open a ticket