How to Install PostgreSQL 18 on Ubuntu 26.04, 24.04 and 22.04

Install PostgreSQL 18 on Ubuntu 26.04, 24.04, and 22.04 with Ubuntu main or PGDG. Covers service checks, remote access, updates, and removal.

Last updatedAuthorJoshua JamesRead time9 minGuide typeUbuntu

Installing PostgreSQL 18 on Ubuntu is now a package-source decision. Ubuntu 26.04 ships PostgreSQL 18 in the default archive, and the official PostgreSQL Apt Repository also supports Ubuntu 26.04 when you want the latest PGDG minor release or PGDG extension packages. Ubuntu 24.04 and 22.04 still need PGDG for the PostgreSQL 18 server packages.

PostgreSQL 18 adds features that are useful right after installation, including asynchronous I/O support, timestamp-ordered uuidv7() values, page checksums enabled for new clusters, and continued movement away from MD5 password authentication. The PostgreSQL versioning policy lists 18.4 as the current 18.x minor release and keeps the 18 branch supported through November 14, 2030.

Install PostgreSQL 18 on Ubuntu

Use Ubuntu’s own package on Ubuntu 26.04 when you prefer the distro-managed source. Use PGDG on Ubuntu 26.04 when you want the current PGDG minor release, and add PGDG on Ubuntu 24.04 or 22.04 because their default repositories do not provide the 18 server package.

Compare PostgreSQL 18 Package Sources on Ubuntu

The package source changes by Ubuntu release and update preference, but the installed service layout stays consistent once PostgreSQL 18 is installed.

Ubuntu ReleaseUbuntu Archive BranchPGDG PostgreSQL 18 SourceBest Choice
Ubuntu 26.04 LTSPostgreSQL 18.xSupported through resolute-pgdgUse Ubuntu main for the simplest source, or PGDG for faster PostgreSQL minor updates.
Ubuntu 24.04 LTSPostgreSQL 16.xSupported through noble-pgdgUse PGDG for the 18.x server and client packages.
Ubuntu 22.04 LTSPostgreSQL 14.xSupported through jammy-pgdgUse PGDG for the 18.x server and client packages.

PGDG support is available but optional on Ubuntu 26.04. Once enabled, APT currently prefers the newer PGDG PostgreSQL 18 candidate over Ubuntu main, so treat PGDG on 26.04 as a deliberate package-source choice. If your application still targets an older PostgreSQL branch, use the matching Ubuntu guide instead: install PostgreSQL 17 on Ubuntu, install PostgreSQL 16 on Ubuntu, or install PostgreSQL 15 on Ubuntu.

Update Ubuntu Before Installing PostgreSQL 18

Refresh package metadata and apply pending upgrades before installing a database service. Review the upgrade list before confirming upgrades on production systems.

sudo apt update
sudo apt upgrade

Commands that modify packages or services use sudo. If your account does not have administrator rights yet, follow the guide to add a new user to sudoers on Ubuntu.

Install PostgreSQL 18 from Ubuntu on Ubuntu 26.04

Ubuntu 26.04 provides PostgreSQL 18 directly from the main repository. Use this path when you want Ubuntu to own the package source and update cadence. Confirm the candidate before installation so you know whether APT will use Ubuntu or PGDG.

apt-cache policy postgresql-18 postgresql-client-18
postgresql-18:
  Installed: (none)
  Candidate: 18.3-1
  Version table:
     18.3-1 500
        500 http://archive.ubuntu.com/ubuntu resolute/main amd64 Packages
postgresql-client-18:
  Installed: (none)
  Candidate: 18.3-1
  Version table:
     18.3-1 500
        500 http://archive.ubuntu.com/ubuntu resolute/main amd64 Packages

Install the server and matching client tools:

sudo apt install -y postgresql-18 postgresql-client-18

Add PGDG on Ubuntu 26.04, 24.04, or 22.04

Use the PostgreSQL project’s APT repository on Ubuntu 26.04 when you want the current PGDG package stream. Ubuntu 24.04 and 22.04 need the same repository for PostgreSQL 18 because their default repositories stay on older branches. Install the HTTPS and key-download prerequisites first; minimal Ubuntu images may not include the curl command.

sudo apt install -y curl ca-certificates

Store the PGDG signing key in the path used by PostgreSQL’s Ubuntu instructions. The curl flags fail on HTTP errors, stay quiet unless an error occurs, follow redirects, and save the key to the exact output path.

sudo install -d /usr/share/postgresql-common/pgdg
sudo curl -fsSLo /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc https://www.postgresql.org/media/keys/ACCC4CF8.asc

Create the DEB822 source file. This uses the same upstream repository and signing key, but stores the source as a .sources file for easier auditing and cleanup. The command reads your Ubuntu codename from /etc/os-release, then writes resolute-pgdg on Ubuntu 26.04, noble-pgdg on Ubuntu 24.04, or jammy-pgdg on Ubuntu 22.04. It also allows PGDG’s supported LTS architectures, amd64, arm64, and ppc64el, so other releases or unsupported architectures print a message and skip the source write instead of leaving a broken APT file.

. /etc/os-release
pgdg_arch=$(dpkg --print-architecture)

case "${VERSION_CODENAME:-}" in
  resolute | noble | jammy)
    case "$pgdg_arch" in
      amd64 | arm64 | ppc64el)
        printf '%s\n' \
          'Types: deb' \
          'URIs: https://apt.postgresql.org/pub/repos/apt' \
          "Suites: ${VERSION_CODENAME}-pgdg" \
          'Components: main' \
          "Architectures: ${pgdg_arch}" \
          'Signed-By: /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc' |
          sudo tee /etc/apt/sources.list.d/pgdg.sources >/dev/null
        ;;
      *)
        printf 'PGDG does not publish PostgreSQL 18 packages for architecture: %s\n' "$pgdg_arch" >&2
        ;;
    esac
    ;;
  *)
    printf 'This PGDG setup supports Ubuntu 26.04, 24.04, or 22.04. Detected codename: %s\n' "${VERSION_CODENAME:-unknown}" >&2
    ;;
esac

A generated Ubuntu 26.04 source file looks like this:

Types: deb
URIs: https://apt.postgresql.org/pub/repos/apt
Suites: resolute-pgdg
Components: main
Architectures: amd64
Signed-By: /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc

Refresh APT and confirm that PostgreSQL 18 now resolves from PGDG. Relevant output includes the current candidate and the PGDG source line:

sudo apt update
apt-cache policy postgresql-18 postgresql-client-18
postgresql-18:
  Installed: (none)
  Candidate: 18.4-1.pgdg26.04+1
  Version table:
     18.4-1.pgdg26.04+1 500
        500 https://apt.postgresql.org/pub/repos/apt resolute-pgdg/main amd64 Packages
     18.3-1 500
        500 http://archive.ubuntu.com/ubuntu resolute/main amd64 Packages
postgresql-client-18:
  Installed: (none)
  Candidate: 18.4-1.pgdg26.04+1
  Version table:
     18.4-1.pgdg26.04+1 500
        500 https://apt.postgresql.org/pub/repos/apt resolute-pgdg/main amd64 Packages
     18.3-1 500
        500 http://archive.ubuntu.com/ubuntu resolute/main amd64 Packages

Ubuntu 24.04 shows 18.4-1.pgdg24.04+1 from noble-pgdg, and Ubuntu 22.04 shows 18.4-1.pgdg22.04+1 from jammy-pgdg. Install the server and client packages after the candidate points at the source you intend to use.

sudo apt install -y postgresql-18 postgresql-client-18

Install Only PostgreSQL 18 Client Tools

Install only the client package on admin workstations, CI runners, backup hosts, or application servers that connect to a separate PostgreSQL server. On Ubuntu 26.04, choose either Ubuntu main or PGDG first; on Ubuntu 24.04 or 22.04, add PGDG first so the 18 client package exists.

sudo apt install -y postgresql-client-18
psql --version
psql (PostgreSQL) 18.4 (Ubuntu 18.4-1.pgdg26.04+1)

Ubuntu main on 26.04 may show an Ubuntu-owned package suffix instead, but the important part is the psql major version: it should begin with 18.

Verify PostgreSQL 18 on Ubuntu

PostgreSQL packages create a postgres system account, initialize the 18/main cluster, and enable local peer authentication plus host scram-sha-256 authentication during installation.

Check the PostgreSQL 18 Service and Cluster

Confirm that the wrapper service, versioned cluster unit, and cluster registry all agree that PostgreSQL 18 is running.

systemctl is-active postgresql
systemctl is-enabled postgresql
systemctl is-active postgresql@18-main
pg_lsclusters
active
enabled
active
Ver Cluster Port Status Owner    Data directory              Log file
18  main    5432 online postgres /var/lib/postgresql/18/main /var/log/postgresql/postgresql-18-main.log

The generic postgresql.service unit is a wrapper. The running cluster appears as postgresql@18-main.service, which is useful when you troubleshoot one PostgreSQL major on a host that has more than one cluster.

Confirm the Installed PostgreSQL 18 Version

Query the server itself instead of relying only on package metadata. Example output from a current PGDG install on Ubuntu 26.04 looks like this:

sudo -u postgres psql -c "SELECT version();"
                                                           version
------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 18.4 (Ubuntu 18.4-1.pgdg26.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 15.2.0-16ubuntu1) 15.2.0, 64-bit
(1 row)

PGDG installs include a source-specific suffix such as 18.4-1.pgdg26.04+1, 18.4-1.pgdg24.04+1, or 18.4-1.pgdg22.04+1. Ubuntu main on 26.04 may show an older Ubuntu-owned suffix until the archive publishes a newer minor update, but the server major should remain 18.

Check PostgreSQL 18 Defaults on Ubuntu

New PostgreSQL 18 clusters enable page checksums by default and use SCRAM for host password authentication. These checks also show the packaged configuration file paths used by service and remote-access steps.

sudo -u postgres psql -Atc "SHOW data_checksums; SHOW password_encryption; SHOW config_file; SHOW hba_file;"
on
scram-sha-256
/etc/postgresql/18/main/postgresql.conf
/etc/postgresql/18/main/pg_hba.conf

You can also run a tiny PostgreSQL 18 feature check by asking the server for a UUIDv7 value:

sudo -u postgres psql -Atc "SELECT uuidv7() IS NOT NULL AS uuidv7_available;"
t

Manage PostgreSQL 18 Service on Ubuntu

Use systemd for routine service control. The generic postgresql wrapper affects every PostgreSQL cluster on the host, so use it only when that is intentional. Reload is enough for authentication file changes; restart the versioned cluster after changing startup-time settings such as listen_addresses, shared_buffers, or max_connections.

sudo systemctl status --no-pager postgresql
sudo systemctl reload postgresql
sudo systemctl restart postgresql
sudo systemctl stop postgresql
sudo systemctl start postgresql

On hosts with more than one PostgreSQL major, target only the PostgreSQL 18 cluster with the versioned unit name:

sudo systemctl reload postgresql@18-main
sudo systemctl restart postgresql@18-main
systemctl status --no-pager postgresql@18-main

Create a PostgreSQL Role and Database on Ubuntu

The postgres role is for administration. Create a separate login role and database for your application so day-to-day connections do not need superuser rights. The password prompt keeps the new role password out of your shell history and process list.

sudo -u postgres createuser --login --pwprompt appuser
sudo -u postgres createdb -O appuser appdb

Use the password you just entered to test a normal password-authenticated local TCP connection:

psql -h 127.0.0.1 -U appuser -d appdb -c "SELECT current_user, current_database();"
 current_user | current_database
--------------+------------------
 appuser      | appdb
(1 row)

Verify that the new database belongs to the intended role:

sudo -u postgres psql -Atc "SELECT datname || ':' || pg_catalog.pg_get_userbyid(datdba) FROM pg_database WHERE datname = 'appdb';"
appdb:appuser

Use a real password manager value when the prompt asks for the role password. For local administration, keep using sudo -u postgres psql; connecting as postgres with psql -U postgres from a normal Linux account usually fails because the default local rule uses peer authentication.

Enable Remote Access to PostgreSQL 18 on Ubuntu

Local applications can stay on the Unix socket and skip remote access. If another host needs PostgreSQL over TCP, change the listening address, add a matching pg_hba.conf rule, restart the cluster, and open the firewall only for trusted clients.

Set the PostgreSQL 18 Listening Address

Open the main configuration file:

sudo nano /etc/postgresql/18/main/postgresql.conf

Use a specific private address when only one interface should accept connections:

listen_addresses = 'localhost, 192.168.1.50'

Use all interfaces only when the firewall and pg_hba.conf rules are already narrow:

listen_addresses = '*'

Add a PostgreSQL 18 Client Rule

Open the host-based authentication file:

sudo nano /etc/postgresql/18/main/pg_hba.conf

A tight single-client rule is safer than a broad subnet rule when you know the exact application host:

# TYPE  DATABASE        USER            ADDRESS                 METHOD
host    appdb           appuser         192.168.1.100/32        scram-sha-256

Use a subnet only for a trusted network segment that you control:

host    appdb           appuser         192.168.1.0/24          scram-sha-256

Restart PostgreSQL 18 and Verify Port 5432

Restart the cluster after changing listen_addresses, then use the grep command to isolate the PostgreSQL listener from the rest of the socket list.

sudo systemctl restart postgresql@18-main
ss -tln | grep ':5432'
LISTEN 0      200       127.0.0.1:5432        0.0.0.0:*
LISTEN 0      200     192.168.1.50:5432       0.0.0.0:*

If the output still shows only 127.0.0.1:5432, PostgreSQL is still local-only. Recheck listen_addresses, restart the cluster again, and then review the PostgreSQL log path shown by pg_lsclusters.

Configure UFW for PostgreSQL 18 on Ubuntu

If PostgreSQL accepts remote connections, restrict port 5432 with UFW instead of opening it to every source. The broader firewall setup is covered in the guide to configure UFW firewall on Ubuntu.

Allow SSH before enabling UFW on a remote server. Otherwise the firewall can cut off your current session and leave you dependent on a VM console or cloud recovery shell.

Install UFW when it is not already present, allow SSH, and enable the firewall:

sudo apt install -y ufw
sudo ufw allow 22/tcp
sudo ufw enable

Allow one PostgreSQL client host with an explicit TCP rule:

sudo ufw allow proto tcp from 192.168.1.100 to any port 5432

For a trusted subnet, use the subnet address instead:

sudo ufw allow proto tcp from 192.168.1.0/24 to any port 5432

Review numbered rules after adding the PostgreSQL entry:

sudo ufw status numbered
Status: active

     To                         Action      From
     --                         ------      ----
[ 1] 22/tcp                     ALLOW IN    Anywhere
[ 2] 5432/tcp                   ALLOW IN    192.168.1.0/24
[ 3] 22/tcp (v6)                ALLOW IN    Anywhere (v6)

Delete the PostgreSQL rule by number when it is no longer needed:

sudo ufw delete 2

Troubleshoot PostgreSQL 18 on Ubuntu

Most PostgreSQL 18 problems on Ubuntu fall into one of three groups: the package source is missing, local authentication is being used from the wrong Linux account, or remote access has not been opened at every required layer.

Fix PostgreSQL 18 Package Not Found

On Ubuntu 26.04, postgresql-18 should resolve from Ubuntu main when PGDG is absent, or from resolute-pgdg when PGDG is enabled. On Ubuntu 24.04 and 22.04, the same command should resolve from PGDG. If APT cannot find the package, inspect the source file and refresh metadata.

cat /etc/apt/sources.list.d/pgdg.sources
sudo apt update
apt-cache policy postgresql-18

The Suites: line should match your release codename with -pgdg appended, such as resolute-pgdg, noble-pgdg, or jammy-pgdg. If the file names the wrong suite, recreate it from the PGDG section.

Fix Peer Authentication Failed for User postgres

Ubuntu’s default local PostgreSQL setup maps the Linux postgres account to the PostgreSQL postgres role. Running psql -U postgres from your normal user commonly fails with this message:

psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  Peer authentication failed for user "postgres"

Use the packaged administrative path instead:

sudo -u postgres psql
psql (18.4)
Type "help" for help.

postgres=#

Fix PostgreSQL 18 Cluster Not Active

If the wrapper service exists but the 18 cluster is not online, check the cluster registry, start the 18 cluster directly, and read the versioned unit log.

pg_lsclusters
sudo pg_ctlcluster 18 main start
systemctl status --no-pager postgresql@18-main
journalctl -u postgresql@18-main -n 40 --no-pager

Configuration syntax errors, port conflicts, and permission problems normally appear in the service log or in the log file path printed by pg_lsclusters.

Fix Remote Connection Refused Errors

A remote refusal usually means PostgreSQL is still bound to localhost, the cluster is down, UFW does not allow the client source, or pg_hba.conf lacks a matching rule. Use the tail command in Linux when you only need the newest PostgreSQL log lines.

systemctl is-active postgresql@18-main
ss -tln | grep ':5432'
sudo ufw status numbered
sudo tail -n 40 /var/log/postgresql/postgresql-18-main.log

Fix the first layer that fails before changing the next one. Opening UFW will not help if PostgreSQL is still listening only on 127.0.0.1, and changing listen_addresses will not help if pg_hba.conf does not allow the client address.

Update PostgreSQL 18 on Ubuntu

Minor PostgreSQL 18 releases arrive through the same source used for installation: Ubuntu main on 26.04, or PGDG on 26.04, 24.04, and 22.04. PostgreSQL’s minor release policy treats current minors as the safer target, but you should still read the current 18.x release notes when an update calls out extra steps.

sudo apt update
sudo apt install --only-upgrade postgresql-18 postgresql-client-18

Confirm the server version after the update. Example output from a current PGDG install on Ubuntu 26.04 looks like this:

sudo -u postgres psql -c "SELECT version();"
                                                           version
------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 18.4 (Ubuntu 18.4-1.pgdg26.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 15.2.0-16ubuntu1) 15.2.0, 64-bit
(1 row)

Major upgrades from PostgreSQL 14, 16, or 17 to 18 are a different workflow. Installing postgresql-18 creates a PostgreSQL 18 cluster; moving existing data from an older major requires dump and restore, logical replication, or pg_upgrade planning.

Remove PostgreSQL 18 from Ubuntu

Choose the removal path based on whether you want to keep the PostgreSQL 18 data directory for a later reinstall. Back up important databases first with pg_dump, pg_dumpall, filesystem snapshots, or your normal backup tooling.

Remove PostgreSQL 18 Packages but Keep Data

This path removes the binaries while leaving the cluster data and configuration on disk.

sudo apt remove postgresql-18 postgresql-client-18

Confirm the PostgreSQL 18 server and client packages are no longer installed:

dpkg -l postgresql-18 postgresql-client-18 2>/dev/null | grep '^ii' || echo "PostgreSQL 18 packages are not installed"
PostgreSQL 18 packages are not installed

Preview dependency cleanup before approving it, especially on servers that host more than one PostgreSQL major:

sudo apt autoremove --purge --dry-run

If the preview lists only PostgreSQL support packages you no longer need, run the cleanup interactively:

sudo apt autoremove --purge

Remove PostgreSQL 18 Completely

This path deletes the PostgreSQL 18 cluster before removing the packages. Do not run it until your backups are complete and tested.

sudo pg_dropcluster --stop 18 main
sudo apt purge postgresql-18 postgresql-client-18

Confirm that no PostgreSQL 18 cluster remains:

pg_lsclusters | grep '^18[[:space:]]' || echo "No PostgreSQL 18 clusters remain"
No PostgreSQL 18 clusters remain

Check the package state after the purge:

dpkg -l postgresql-18 postgresql-client-18 2>/dev/null | grep '^ii' || echo "PostgreSQL 18 packages are not installed"
PostgreSQL 18 packages are not installed

Preview dependency cleanup and confirm the package list is limited to unused PostgreSQL support packages:

sudo apt autoremove --purge --dry-run

If the preview is limited to PostgreSQL support packages you no longer need, run the cleanup interactively:

sudo apt autoremove --purge

Remove the PGDG Repository from Ubuntu

Remove PGDG only if you no longer need PostgreSQL packages from that repository. Skip this cleanup when the same host still uses another PostgreSQL major, extension package, or client tool from PGDG. The key cleanup checks remaining APT source files before deleting the shared PGDG key path.

sudo rm -f /etc/apt/sources.list.d/pgdg.sources

pgdg_key='/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc'
source_paths=()
[ -f /etc/apt/sources.list ] && source_paths+=(/etc/apt/sources.list)
while IFS= read -r -d '' source_file; do
  source_paths+=("$source_file")
done < <(find /etc/apt/sources.list.d -type f -print0)

if ((${#source_paths[@]} == 0)) || ! grep -Fq "$pgdg_key" "${source_paths[@]}"; then
  sudo rm -f "$pgdg_key"
  sudo rmdir /usr/share/postgresql-common/pgdg 2>/dev/null || true
fi

sudo apt update

On Ubuntu 24.04 and 22.04, apt-cache policy postgresql-18 has no candidate after PGDG is removed. On Ubuntu 26.04, the Ubuntu archive candidate remains because PostgreSQL 18 is part of the default release.

Conclusion

PostgreSQL 18 is installed on Ubuntu through the package source that matches your release and update preference: Ubuntu main on 26.04, or PGDG on 26.04, 24.04, and 22.04. Keep local administration on the postgres account, create separate roles for applications, and expose port 5432 only to trusted clients. On long-running servers, configure unattended upgrades on Ubuntu so security updates keep flowing between planned database maintenance windows.

Share this guide

Help another Linux user troubleshoot faster

Share this guide with someone troubleshooting Linux systems or saving it for later.

Follow LinuxCapable

Want more LinuxCapable guides in Google?

Add LinuxCapable as a preferred source so Google can show our tutorials more often in Top Stories and mark them as preferred in AI Mode and AI Overviews when relevant.

Add LinuxCapable as a preferred source on Google
Search LinuxCapable

Need another guide?

Search LinuxCapable for package installs, commands, troubleshooting, and follow-up guides related to what you just read.

Found this guide useful?

Support LinuxCapable to keep tutorials free and up to date.

Buy me a coffeeBuy me a coffee
Before commenting, please review our Comments Policy.
Formatting tips for your comment

You can use basic HTML to format your comment. Useful tags currently allowed in published comments:

You type Result
<code>command</code> command
<strong>bold</strong> bold
<em>italic</em> italic
<a href="https://example.com">link</a> link
<blockquote>quote</blockquote> quote block

Add to the discussion

Questions, fixes, command output, and version notes help keep this guide current.

Verify before posting: