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

Install PostgreSQL 17 on Ubuntu 26.04, 24.04, or 22.04 with the official PGDG repository. Includes remote access, firewall setup and tips.

Last updatedAuthorJoshua JamesRead time8 minGuide typeUbuntu

PostgreSQL 17 is not the default on any current Ubuntu LTS release. Ubuntu 26.04 already moved to PostgreSQL 18, while Ubuntu 24.04 and 22.04 stay on older branches. If you need to install PostgreSQL 17 on Ubuntu for application compatibility or staged upgrades, the official PGDG repository is the clean path that keeps the 17.x series available on all three releases.

The PGDG repository is a better fit than downloading individual .deb files because APT keeps the server and client tools on the 17.x update track. PostgreSQL 17 was released on September 26, 2024 and stays supported through November 8, 2029, so the rest of the guide covers local access, remote connections, firewall rules, troubleshooting, and clean removal.

Install PostgreSQL 17 on Ubuntu

Ubuntu’s default repositories no longer provide PostgreSQL 17 on any current LTS release, so add PGDG first and then install the packages you need.

Compare Default PostgreSQL Versions on Ubuntu

The distro default changes by release, which is why PostgreSQL 17 needs the upstream repository everywhere in this guide.

Ubuntu ReleaseDefault PostgreSQLNeed PGDG for PostgreSQL 17?
Ubuntu 26.04PostgreSQL 18.xYes
Ubuntu 24.04PostgreSQL 16.xYes
Ubuntu 22.04PostgreSQL 14.xYes

If your application can stay on Ubuntu 24.04’s default branch, use our guide to install PostgreSQL 16 on Ubuntu. If you need the older 15 series instead, install PostgreSQL 15 on Ubuntu with the same PGDG repository workflow.

Update Ubuntu Before Installing PostgreSQL 17

Refresh package metadata and apply pending upgrades before you add another repository. Review the upgrade list before you approve it, especially on production servers.

sudo apt update && sudo apt upgrade

Commands that need root privileges use sudo. If your account does not have sudo access yet, follow the guide to add a new user to sudoers on Ubuntu.

Install Required Packages for the PostgreSQL Repository on Ubuntu

Install the packages that fetch the repository key over HTTPS. Desktop installs often already have them, but cloud and minimal Ubuntu images may not. The curl command downloads the signing key, and ca-certificates lets APT validate TLS certificates when it contacts PGDG.

sudo apt install -y curl ca-certificates

Store the PostgreSQL APT Signing Key on Ubuntu

PGDG signs its packages, so store the official ASCII-armored key in the path used by the PostgreSQL Ubuntu download instructions.

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

The DEB822 source file references that key with Signed-By, so PGDG packages are trusted only through this repository-specific key path.

Add the PostgreSQL PGDG Repository on Ubuntu

Create a DEB822 source file for PGDG. This form is easier to audit than the older one-line .list format, and the command detects your Ubuntu codename and system architecture automatically.

. /etc/os-release
printf '%s\n' \
'Types: deb' \
'URIs: https://apt.postgresql.org/pub/repos/apt' \
"Suites: ${VERSION_CODENAME}-pgdg" \
'Components: main' \
"Architectures: $(dpkg --print-architecture)" \
'Signed-By: /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc' \
| sudo tee /etc/apt/sources.list.d/pgdg.sources > /dev/null

The tee command writes the file with root privileges because a normal shell redirection would still run as your current user. The . /etc/os-release line provides VERSION_CODENAME, so the same command writes resolute-pgdg, noble-pgdg, or jammy-pgdg as needed.

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 so PostgreSQL 17 becomes available:

sudo apt update

Verify the PostgreSQL 17 Package Source on Ubuntu

Before you install anything, confirm that APT sees the PostgreSQL 17 package from PGDG.

apt-cache policy postgresql-17
postgresql-17:
  Installed: (none)
  Candidate: 17.9-1.pgdg26.04+2
  Version table:
     17.9-1.pgdg26.04+2 500
        500 https://apt.postgresql.org/pub/repos/apt resolute-pgdg/main amd64 Packages

On Ubuntu 24.04 and 22.04, the candidate line changes to 17.9-1.pgdg24.04+1 or 17.9-1.pgdg22.04+1. The important part is the source line from https://apt.postgresql.org/pub/repos/apt and the versioned package name postgresql-17; after PGDG is enabled, the unversioned postgresql package can point at PostgreSQL 18 instead.

Install PostgreSQL 17 Server on Ubuntu

Install both packages on any host that will run the database locally. The client package also provides psql, pg_dump, and pg_restore.

sudo apt install -y postgresql-17 postgresql-client-17

Ubuntu creates the postgres system account, initializes the 17/main cluster, and enables local peer plus host scram-sha-256 authentication during the install.

Confirm that the wrapper service is active, enabled, and managing the PostgreSQL 17 cluster:

systemctl is-active postgresql
systemctl is-enabled postgresql
systemctl is-active postgresql@17-main
active
enabled
active

The main postgresql.service unit is a wrapper. The version-specific cluster runs as postgresql@17-main.service underneath it.

Verify the installed server version:

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

On Ubuntu 24.04 and 22.04, the package suffix changes to pgdg24.04+1 or pgdg22.04+1, but the PostgreSQL major and minor release stays on the same 17.9 branch.

If the service did not start automatically, enable it explicitly:

sudo systemctl enable postgresql --now

Install Only the PostgreSQL 17 Client Tools on Ubuntu

If the server lives elsewhere and you only need the admin utilities on this machine, install the client package on its own.

sudo apt install -y postgresql-client-17

Verify that the client binaries are on your PATH:

command -v psql pg_dump pg_restore
/usr/bin/psql
/usr/bin/pg_dump
/usr/bin/pg_restore

This client-only path fits admin workstations, CI runners, and backup hosts that connect to a different PostgreSQL server.

Manage PostgreSQL 17 Service on Ubuntu

Ubuntu manages PostgreSQL through systemd. These are the service commands you will use most often.

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

Use reload for changes such as pg_hba.conf updates that PostgreSQL can apply without dropping active sessions. Use restart after startup-only changes such as listen_addresses, shared_buffers, or max_connections.

Configure PostgreSQL 17 on Ubuntu

A fresh install gives you the postgres superuser and the default 17/main cluster. The next steps cover the common first-run admin tasks.

Open the PostgreSQL Shell on Ubuntu

Use the postgres system account for local admin access.

sudo -u postgres psql

Inside psql, confirm how you are connected:

\conninfo
You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432".

Exit the shell when you are finished:

\q

Create a PostgreSQL User on Ubuntu

Create a dedicated application role instead of using the postgres superuser from your app.

sudo -u postgres createuser --interactive <username>
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) n
Shall the new role be allowed to create more new roles? (y/n) n

Answer n to all three prompts for a typical application account. Then set a password for that role.

sudo -u postgres psql -c "ALTER USER <username> WITH PASSWORD 'your_secure_password';"
ALTER ROLE

Create a PostgreSQL Database on Ubuntu

Create the database with the application role as its owner. Ownership avoids a common first-run problem where a role can connect to a database but cannot create tables in the default schema.

sudo -u postgres createdb --owner=<username> <database_name>

Use that role for your application connections so the postgres account stays reserved for administration. If you need a shared database where the application role is not the owner, grant database and schema privileges deliberately instead of using the superuser role from the app.

Enable Remote Access to PostgreSQL 17 on Ubuntu

Local applications can stay on the Unix socket and skip this section. If another host needs PostgreSQL over TCP, change the listening address, add a matching pg_hba.conf rule, and then verify that port 5432 is open on the correct interface.

Edit postgresql.conf for PostgreSQL 17 on Ubuntu

Open the main server configuration file:

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

Use a specific 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 will narrow access:

listen_addresses = '*'

Edit pg_hba.conf for PostgreSQL 17 on Ubuntu

Now add a host rule that matches the clients you want to allow.

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

A common subnet rule looks like this:

# TYPE  DATABASE        USER            ADDRESS                 METHOD
host    all             all             192.168.1.0/24          scram-sha-256

The all values allow any database and role to match this line, while 192.168.1.0/24 limits access to that subnet. Tighten those fields when you know the exact database, user, or client address that should connect.

Restart PostgreSQL 17 and Verify Port 5432 on Ubuntu

Restart PostgreSQL after a listen_addresses change so the socket binding is rebuilt.

sudo systemctl restart postgresql

Use the grep command to isolate the PostgreSQL listener from the rest of the socket list:

ss -tlnp | grep 5432
LISTEN 0      200          0.0.0.0:5432      0.0.0.0:*
LISTEN 0      200             [::]:5432         [::]:*

If you set a single IPv4 address, ss shows that address instead of 0.0.0.0. If the output still shows only 127.0.0.1:5432, PostgreSQL is still restricted to local access.

Configure UFW for PostgreSQL 17 on Ubuntu

If PostgreSQL will accept remote connections, tighten access with UFW rather than opening port 5432 broadly. The broader firewall workflow is covered in our guide to configure UFW firewall on Ubuntu.

Allow SSH before you enable UFW on a remote Ubuntu system. Otherwise the firewall can cut off the current session and leave you at the VM or cloud console.

If UFW is not installed yet, add it, allow SSH, and then enable the firewall. Ubuntu asks for confirmation the first time UFW starts.

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

Allow one client host with a single-IP rule:

sudo ufw allow from 192.168.1.100 to any port 5432

For a trusted subnet, use a CIDR range instead:

sudo ufw allow from 192.168.1.0/24 to any port 5432

Check the numbered rules after you add them:

sudo ufw status numbered
Status: active

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

If you later remove the PostgreSQL rule, use its number from that list:

sudo ufw delete 2

Troubleshoot PostgreSQL 17 on Ubuntu

These are the failure modes readers hit most often after the package install, especially when they are switching from local-only access to remote connections.

Fix Peer Authentication Failed for User postgres on Ubuntu

Ubuntu’s default local PostgreSQL setup uses peer authentication for the postgres role. Running psql -U postgres from your own Linux account usually fails with this error:

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

Use the postgres system account for local admin work, or connect over TCP with a password-enabled role instead.

sudo -u postgres psql -c "SELECT current_user;"
 current_user
--------------
 postgres
(1 row)

Fix Connection Refused Errors for PostgreSQL 17 on Ubuntu

If a remote client cannot reach the server, PostgreSQL usually returns a plain connection refusal.

psql: error: connection to server at "192.168.1.50", port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?

This usually means PostgreSQL is still bound to localhost, the service is down, or UFW never opened port 5432 for the client address. Re-check the service state, the listening socket, the firewall rules, and the matching host line in pg_hba.conf. If ss still shows only 127.0.0.1:5432, return to the remote-access section and fix listen_addresses before you restart the service again.

Fix postgresql.service Not Found on Ubuntu

This message appears when the server package or the shared PostgreSQL service helpers are no longer installed.

Unit postgresql.service could not be found.

Re-add the PGDG repository if needed, then reinstall the server packages and check the wrapper unit again:

sudo apt install -y postgresql-17 postgresql-client-17
systemctl is-active postgresql
active

Update PostgreSQL 17 on Ubuntu

Minor PostgreSQL 17 releases arrive through the same PGDG repository. Use --only-upgrade so APT updates the package only when it is already installed.

sudo apt update
sudo apt install --only-upgrade -y postgresql-17 postgresql-client-17

Verify the running server version after the update:

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

Remove PostgreSQL 17 from Ubuntu

Pick the removal path that matches whether you want to keep or delete the 17/main cluster.

Remove PostgreSQL 17 Packages but Keep Data on Ubuntu

This path removes the server and client binaries but keeps the PostgreSQL 17 data and configuration on disk for a later reinstall.

sudo apt remove postgresql-17 postgresql-client-17

The existing cluster stays in /var/lib/postgresql/17/main, the config stays in /etc/postgresql/17/main, and the version-specific log file stays in /var/log/postgresql/postgresql-17-main.log. Preview dependency cleanup before removing shared helpers:

sudo apt autoremove --dry-run

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

sudo apt autoremove

Remove PostgreSQL 17 Completely on Ubuntu

This path deletes the PostgreSQL 17 cluster itself. Back up anything important first with pg_dump or pg_dumpall, because the data files do not come back after the cluster is dropped.

Drop the 17/main cluster before you purge the packages:

sudo pg_dropcluster --stop 17 main

Confirm that no PostgreSQL 17 cluster is left behind:

pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file

Once the cluster is gone, purge the server and client packages:

sudo apt purge postgresql-17 postgresql-client-17

This sequence removes the version-specific cluster directories instead of asking you to clean up /var/lib/postgresql/17, /etc/postgresql/17, and the matching log file by hand. Preview dependency cleanup after the purge:

sudo apt autoremove --dry-run

If the preview only lists unused PostgreSQL support packages, remove them:

sudo apt autoremove

Remove the PGDG Repository from Ubuntu

If you no longer want PGDG packages on this machine, remove the source file and signing key, then refresh APT. The command also removes the older source and keyring paths used by previous instructions when they exist.

sudo rm -f /etc/apt/sources.list.d/pgdg.sources /etc/apt/sources.list.d/postgresql.sources
sudo rm -f /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc /usr/share/keyrings/postgresql.gpg
sudo rmdir /usr/share/postgresql-common/pgdg 2>/dev/null || true
sudo apt update

Check that Ubuntu now falls back to its distro-default PostgreSQL branch:

apt-cache policy postgresql
postgresql:
  Installed: (none)
  Candidate: 16+257build1.1
  Version table:
     16+257build1.1 500
        500 http://au.archive.ubuntu.com/ubuntu noble-updates/main amd64 Packages
     16+257build1 500
        500 http://au.archive.ubuntu.com/ubuntu noble/main amd64 Packages

On Ubuntu 26.04 the candidate becomes PostgreSQL 18 again, and on Ubuntu 22.04 it becomes PostgreSQL 14. If you query apt-cache policy postgresql-17 after removing PGDG, Ubuntu prints no package output because that major is no longer available from the configured sources.

Conclusion

PostgreSQL 17 is installed on Ubuntu through the PGDG repository, so the 17.x branch stays available on 26.04, 24.04, and 22.04 through normal APT updates. If you plan to expose it beyond localhost, configure UFW firewall on Ubuntu next, or install PostgreSQL 16 on Ubuntu if your stack can stay on the distro default on 24.04.

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 more of our fresh Linux tutorials in Top Stories and From your sources 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
<blockquote>quote</blockquote> quote block

Got a Question or Feedback?

We read and reply to every comment - let us know how we can help or improve this guide.

Verify before posting: