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.
Tip: For a new application, use the newest stable major version your software supports. Avoid release candidates and beta builds in production.
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:
sudo apt update
sudo apt install -y postgresql-common
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.shPress 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:
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.listThe signed-by option ties the key to this one repository, so it cannot be used to sign packages from any other source.
Install PostgreSQL
sudo apt update
sudo apt install -y postgresql-18On 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:
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-$(rpm -E %{rhel})-x86_64/pgdg-redhat-repo-latest.noarch.rpmDisable the built-in PostgreSQL module so dnf does not pick the distribution's older packages instead:
sudo dnf -qy module disable postgresqlInstall the server, then initialise the data directory. Unlike on Debian, this step is not automatic:
sudo dnf install -y postgresql18-server
sudo /usr/pgsql-18/bin/postgresql-18-setup initdb
sudo systemctl enable --now postgresql-18Data and config live together in /var/lib/pgsql/18/data/, and the programs are in /usr/pgsql-18/bin/.
Note: On ARM servers, replace x86_64 in the repository URL with aarch64.
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:
sudo apt install -y postgresql-17
pg_lsclusterspg_lsclusters lists every cluster with its version, port and status:
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.logConnect 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:
port = 5433Then 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
sudo -u postgres psql -c "SELECT version();"The output should start with PostgreSQL 18. Confirm the service is listening:
sudo ss -tlnp | grep postgresBy 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 updatewas skipped. Check/etc/apt/sources.list.d/pgdg.listand runsudo apt updateagain. - dnf installs an old version: the distribution module is still active. Run
sudo dnf -qy module disable postgresqland install thepostgresql18-serverpackage by name. - "Data directory is not empty" on initdb: the cluster is already initialised. Skip
initdband just start the service. - Service starts but psql connects to the wrong version: two clusters are running. Pass the port with
-p, or checkpg_lsclusterson Debian and Ubuntu. - GPG or signature errors from apt: the key file is missing or empty. Download it again with the
curlcommand above.
Next steps
- Create a login and a database for your app: Create PostgreSQL users, roles and databases.
- Learn the client: psql cheat sheet.
- Plan your first backup: Back up and restore PostgreSQL with pg_dump.
