Install the latest PostgreSQL from the official PGDG repository

Add the PostgreSQL project's own apt or dnf repository to get a current major version on Ubuntu, Debian, AlmaLinux or Rocky Linux, and run more than one version side by side.

  • Beginner
  • Linux VPS
  • 5 min read

By RunRacks Team · Published

The PostgreSQL version in your distribution's own repositories is fixed for the life of that release, so it can be a few major versions behind. The PostgreSQL Global Development Group (PGDG) runs official apt and yum/dnf repositories with every supported major version. This guide adds that repository, installs a current version, and shows how to run two versions side by side.

Before you start

  • A Linux VPS with Ubuntu 22.04 or 24.04, Debian 12, or AlmaLinux / Rocky Linux 8 or 9.
  • SSH access as root or a sudo user. See Connect to your Linux VPS with SSH.
  • Pick the major version you want. The examples use 18. PostgreSQL 19 is in its release-candidate stage at the time of writing, with general availability planned for later in November 2026. Check the versioning page on postgresql.org for the current release and its support dates, and change the number in the commands to match.

Ubuntu and Debian (apt)

Option 1: the helper script

PGDG ships a small script in the postgresql-common package that adds the repository and its signing key for you:

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

Press Enter when it asks to continue.

Option 2: add the repository by hand

If you prefer to see each step, do the same work manually:

Terminal
sudo apt install -y curl ca-certificates
sudo install -d /usr/share/postgresql-common/pgdg
sudo curl -fsSL -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc \
  https://www.postgresql.org/media/keys/ACCC4CF8.asc
. /etc/os-release
echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt ${VERSION_CODENAME}-pgdg main" \
  | sudo tee /etc/apt/sources.list.d/pgdg.list

The signed-by option ties the key to this one repository, so it cannot be used to sign packages from any other source.

Install PostgreSQL

Terminal
sudo apt update
sudo apt install -y postgresql-18

On Debian-based systems the package creates a cluster called 18/main, initialises it and starts it straight away. The data lives in /var/lib/postgresql/18/main and the config in /etc/postgresql/18/main/.

AlmaLinux and Rocky Linux (dnf)

Install the PGDG repository package for your EL version and CPU. rpm -E %{rhel} prints 8 or 9:

Terminal
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-$(rpm -E %{rhel})-x86_64/pgdg-redhat-repo-latest.noarch.rpm

Disable the built-in PostgreSQL module so dnf does not pick the distribution's older packages instead:

Terminal
sudo dnf -qy module disable postgresql

Install the server, then initialise the data directory. Unlike on Debian, this step is not automatic:

Terminal
sudo dnf install -y postgresql18-server
sudo /usr/pgsql-18/bin/postgresql-18-setup initdb
sudo systemctl enable --now postgresql-18

Data and config live together in /var/lib/pgsql/18/data/, and the programs are in /usr/pgsql-18/bin/.

Run more than one version

PGDG packages are built to install side by side. Each major version has its own binaries, data directory and service. The only thing that can clash is the network port, because two servers cannot both listen on 5432.

On Ubuntu and Debian

Install a second version and the packaging picks the next free port for you, usually 5433:

Terminal
sudo apt install -y postgresql-17
pg_lsclusters

pg_lsclusters lists every cluster with its version, port and status:

Text
Ver Cluster Port Status Owner    Data directory              Log file
17  main    5433 online postgres /var/lib/postgresql/17/main /var/log/postgresql/postgresql-17-main.log
18  main    5432 online postgres /var/lib/postgresql/18/main /var/log/postgresql/postgresql-18-main.log

Connect to a specific one with -p, for example sudo -u postgres psql -p 5433. To move data from an older cluster to a newer one, Debian provides pg_upgradecluster. Read its manual page and take a backup before you use it.

On AlmaLinux and Rocky Linux

Install and initialise the second version the same way. Before you start it, change its port in /var/lib/pgsql/17/data/postgresql.conf:

INI
port = 5433

Then enable it with sudo systemctl enable --now postgresql-17. If SELinux is enforcing, it may also need to be told about the new port. Check sudo journalctl -u postgresql-17 if the service fails to start.

Check it works

Terminal
sudo -u postgres psql -c "SELECT version();"

The output should start with PostgreSQL 18. Confirm the service is listening:

Terminal
sudo ss -tlnp | grep postgres

By default it listens on 127.0.0.1:5432 only, which is what you want until you have a reason to allow remote connections.

Troubleshooting

  • "Unable to locate package postgresql-18": the repository was not added or apt update was skipped. Check /etc/apt/sources.list.d/pgdg.list and run sudo apt update again.
  • dnf installs an old version: the distribution module is still active. Run sudo dnf -qy module disable postgresql and install the postgresql18-server package by name.
  • "Data directory is not empty" on initdb: the cluster is already initialised. Skip initdb and just start the service.
  • Service starts but psql connects to the wrong version: two clusters are running. Pass the port with -p, or check pg_lsclusters on Debian and Ubuntu.
  • GPG or signature errors from apt: the key file is missing or empty. Download it again with the curl command above.

Next steps

Was this guide helpful?

Still stuck?

Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.

Databases

Reset a Forgotten MySQL Root Password on Linux

Recover access to MySQL or MariaDB on a Linux VPS by starting the server with skip-grant-tables, setting a new root password and restarting normally.

  • Intermediate
  • Linux VPS
  • 5 min read

Databases

MySQL Point-in-Time Recovery Using Binary Logs

Recover a MySQL or MariaDB database to an exact moment after a bad write or dropped table by restoring a full backup and replaying binary logs up to the incident.

  • Advanced
  • Linux VPS
  • 7 min read