How to Install PostgreSQL 15 on Ubuntu

PostgreSQL 15 is a robust, open-source relational database management system that continues PostgreSQLโ€™s tradition of delivering advanced features, reliability, and high performance. This version introduces significant improvements including enhanced logical replication capabilities, better compression for backups using LZ4 and Zstandard algorithms, improved sort performance, and new SQL standard features like the MERGE command. As a result, PostgreSQL 15 is particularly well-suited for applications requiring complex data operations, from small development projects to large-scale enterprise databases.

This guide walks you through installing PostgreSQL 15 on Ubuntu using the official PostgreSQL APT repository maintained by the PostgreSQL Global Development Group (PGDG). While Ubuntuโ€™s default repositories include PostgreSQL (version 14 on 22.04 and version 16 on 24.04), the PGDG repository provides access to PostgreSQL 15 specifically, along with the latest patch releases and security updates directly from the PostgreSQL team. Additionally, you will learn how to configure the database service, set up remote access, configure firewall rules, and manage the installation over time.

These steps cover Ubuntu 22.04 LTS (Jammy) and 24.04 LTS (Noble). The PostgreSQL APT repository does not yet have a branch for Ubuntu 26.04 LTS (Resolute). Users on Ubuntu 26.04 should use the default Ubuntu repository, which includes PostgreSQL 17. This guide will be updated once PostgreSQL adds support for Ubuntu 26.04.

Compare PostgreSQL Versions for Ubuntu

Before proceeding, use the comparison below to decide whether PostgreSQL 15 fits your requirements or if another version better matches your stability or feature needs.

PostgreSQL VersionAvailability on UbuntuChoose It WhenTrade-offs
PostgreSQL 15PGDG repository on 22.04/24.04You need MERGE command, improved logical replication, or LZ4/Zstandard compressionRequires external repository; not the default on any Ubuntu LTS
Install PostgreSQL 16 on UbuntuUbuntu 24.04 default, PGDG on 22.04General-purpose production with Canonical-managed security through April 2029Missing some PostgreSQL 17 features; PPA required on 22.04

Therefore, choose PostgreSQL 15 when your application specifically requires its feature set. For most production environments on Ubuntu 24.04, the default PostgreSQL 16 offers Canonical-managed security updates without additional repository configuration.

Import the PostgreSQL APT Repository

Update Your System

Before adding any external repository, first update your package index to ensure you have the latest package information. This step also upgrades any outdated packages on your system:

sudo apt update && sudo apt upgrade

Install Required Packages

Next, install the packages needed to securely download and verify the PostgreSQL repository. Specifically, the curl utility fetches the GPG key, gpg processes it, and lsb-release identifies your Ubuntu version for repository configuration:

sudo apt install curl ca-certificates gpg lsb-release -y

Import the PostgreSQL GPG Key

As a security measure, the PostgreSQL team cryptographically signs all packages from their repository. To verify their authenticity, import the official signing key into your systemโ€™s keyring:

curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo gpg --dearmor -o /usr/share/keyrings/postgresql.gpg

This command downloads the ASCII-armored key, converts it to binary format using gpg --dearmor, and saves it to the keyrings directory where APT references it securely.

Add the PostgreSQL Repository

With the GPG key in place, now add the official PostgreSQL repository to your system. The following command creates a repository configuration file in the modern DEB822 format, which offers better readability than the legacy single-line format:

echo "Types: deb
URIs: https://apt.postgresql.org/pub/repos/apt
Suites: $(lsb_release -cs)-pgdg
Components: main
Architectures: $(dpkg --print-architecture)
Signed-By: /usr/share/keyrings/postgresql.gpg" | sudo tee /etc/apt/sources.list.d/postgresql.sources

The command outputs the file contents, confirming successful creation:

Types: deb
URIs: https://apt.postgresql.org/pub/repos/apt
Suites: noble-pgdg
Components: main
Architectures: amd64
Signed-By: /usr/share/keyrings/postgresql.gpg

The $(lsb_release -cs) command automatically detects your Ubuntu codename (such as jammy for 22.04 or noble for 24.04), ensuring the correct repository branch configuration. After adding the repository, refresh the package index:

sudo apt update

The output should include lines showing APT fetching from the PostgreSQL repository:

Get:1 https://apt.postgresql.org/pub/repos/apt noble-pgdg InRelease [107 kB]
Get:2 https://apt.postgresql.org/pub/repos/apt noble-pgdg/main amd64 Packages [575 kB]
...
Fetched 682 kB in 1s (734 kB/s)

Verify Package Source

Nevertheless, before installing, confirm that the PostgreSQL 15 package comes from the PGDG repository:

apt-cache policy postgresql-15
postgresql-15:
  Installed: (none)
  Candidate: 15.15-1.pgdg24.04+1
  Version table:
     15.15-1.pgdg24.04+1 500
        500 https://apt.postgresql.org/pub/repos/apt noble-pgdg/main amd64 Packages

This output confirms the package will install from the PostgreSQL APT repository (apt.postgresql.org) rather than Ubuntuโ€™s default repositories.

Install PostgreSQL 15

Once you configure the repository, install PostgreSQL 15 along with its client utilities. The postgresql-15 package includes the database server, while postgresql-client-15 provides command-line tools like psql for interacting with databases:

sudo apt install postgresql-15 postgresql-client-15

APT downloads and installs PostgreSQL 15 along with all required dependencies. During installation, the package automatically creates a postgres system user, initializes a default database cluster, and configures the PostgreSQL service to start automatically on boot.

Verify the Installation

After installation completes, verify that the PostgreSQL service runs properly:

systemctl status postgresql

In response, you should see output indicating the service is active:

โ— postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: enabled)
     Active: active (exited) since Mon 2025-01-01 10:00:00 UTC; 1min ago
   Main PID: 12345 (code=exited, status=0/SUCCESS)
        CPU: 15ms

The active (exited) status is normal for PostgreSQL because the main service unit acts as a wrapper that manages individual database clusters. The actual database server runs as a separate process. To confirm the database accepts connections, check the version:

sudo -u postgres psql -c "SELECT version();"
                                                   version                                                   
-------------------------------------------------------------------------------------------------------------
 PostgreSQL 15.15 (Ubuntu 15.15-1.pgdg24.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0, 64-bit
(1 row)

On Ubuntu 22.04, the package version string shows 15.15-1.pgdg22.04+1 instead. Both versions install the same PostgreSQL 15.15 release.

If PostgreSQL does not start automatically, enable and start it with the following command:

sudo systemctl enable postgresql --now

Manage the PostgreSQL Service

PostgreSQL runs as a systemd service, which you can manage using standard systemctl commands. Below are the most commonly used operations:

Stop the PostgreSQL server:

sudo systemctl stop postgresql

Start the PostgreSQL server:

sudo systemctl start postgresql

Restart the PostgreSQL server (required after configuration changes):

sudo systemctl restart postgresql

Reload configuration without restarting (for changes that support hot reload):

sudo systemctl reload postgresql

Check the service status:

systemctl status postgresql

Configure PostgreSQL 15

Access the PostgreSQL Shell

By design, PostgreSQL uses role-based authentication. During installation, the system creates a superuser role named postgres along with a corresponding Linux system account. To access the PostgreSQL interactive shell, first switch to the postgres user:

sudo -i -u postgres

Once you log in as the postgres user, start the interactive SQL shell:

psql

Your terminal prompt changes to postgres=#, indicating you now connect to the PostgreSQL database:

psql (15.15 (Ubuntu 15.15-1.pgdg24.04+1))
Type "help" for help.

postgres=#

To exit the shell, simply type:

\q

Afterward, type exit to return to your regular user account.

Alternatively, you can also access the PostgreSQL shell directly without switching users by combining the commands:

sudo -u postgres psql

This approach saves time for quick database operations and eliminates the need to manage multiple shell sessions.

Create a New Database User

For security best practices, avoid using the postgres superuser for application connections. Instead, create dedicated users with appropriate privileges. Specifically, run the following command, replacing <username> with your desired username:

sudo -u postgres createuser --interactive <username>

The --interactive flag prompts you to specify whether the new user should have superuser, database creation, or role creation privileges:

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

Generally, for a standard application user, answer โ€œnoโ€ to all prompts to follow the principle of least privilege.

Subsequently, to set a password for the new user, connect to the database and run:

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

Create a New Database

Once you create a user, you can then create a database and grant the user access to it. First, create the database:

sudo -u postgres createdb <database_name>

Next, grant the new user full privileges on this database:

sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE <database_name> TO <username>;"
GRANT

At this point, you can verify the grant succeeded by listing all databases and their access privileges:

sudo -u postgres psql -c "\l"
                                                   List of databases
   Name    |  Owner   | Encoding | Collate |  Ctype  | ICU Locale | Locale Provider |   Access privileges   
-----------+----------+----------+---------+---------+------------+-----------------+-----------------------
 postgres  | postgres | UTF8     | C.UTF-8 | C.UTF-8 |            | libc            | 
 template0 | postgres | UTF8     | C.UTF-8 | C.UTF-8 |            | libc            | =c/postgres          +
           |          |          |         |         |            |                 | postgres=CTc/postgres
 template1 | postgres | UTF8     | C.UTF-8 | C.UTF-8 |            | libc            | =c/postgres          +
           |          |          |         |         |            |                 | postgres=CTc/postgres
 testdb    | postgres | UTF8     | C.UTF-8 | C.UTF-8 |            | libc            | =Tc/postgres         +
           |          |          |         |         |            |                 | postgres=CTc/postgres+
           |          |          |         |         |            |                 | testuser=CTc/postgres
(4 rows)

The Access privileges column shows that testuser has connect (c), temporary table (T), and create (C) privileges on the testdb database.

Configure UFW Firewall for PostgreSQL

By default, PostgreSQL listens only on localhost (127.0.0.1), which means no firewall configuration is needed for local-only access. However, if you plan to allow remote connections, you must configure your firewall to permit traffic on PostgreSQLโ€™s default port (5432). For a comprehensive guide to firewall management, see our UFW firewall guide for Ubuntu.

To begin, ensure UFW is installed and enabled:

sudo apt install ufw -y
sudo ufw enable

Warning: Before enabling UFW, ensure you allow SSH access with sudo ufw allow ssh if you connect remotely. Enabling the firewall without an SSH rule locks you out of your server.

To allow PostgreSQL connections from a specific IP address (recommended for security):

sudo ufw allow from 192.168.1.100 to any port 5432

Similarly, to allow connections from an entire subnet:

sudo ufw allow from 192.168.1.0/24 to any port 5432

After adding rules, verify your firewall configuration:

sudo ufw status numbered
Status: active

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

Likewise, to remove a rule, use the rule number shown in the output:

sudo ufw delete 2

Remember to replace the IP addresses and subnet examples with your actual network configuration. Avoid opening port 5432 to the entire internet (ufw allow 5432) unless you implement additional security measures like SSL/TLS encryption and strong authentication.

Enable Remote Access

By default, PostgreSQL only accepts connections from the local machine. To allow remote connections, you must modify two configuration files: postgresql.conf to change the listening address, and pg_hba.conf to configure client authentication.

Configure the Listening Address

First, identify the PostgreSQL configuration directory for your installation:

ls /etc/postgresql/
15

As expected, this output confirms PostgreSQL 15 installation. Next, open the main configuration file:

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

Locate the listen_addresses setting (around line 60). By default, the system sets it to 'localhost'. Change it to allow connections from specific addresses or all interfaces:

For a specific IP address (more secure):

listen_addresses = 'localhost, 192.168.1.50'

For all network interfaces (use with caution):

listen_addresses = '*'

Finally, save and close the file (Ctrl+O, Enter, Ctrl+X in nano).

Configure Client Authentication

Next, edit the host-based authentication file to specify which clients can connect and how they authenticate:

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

Then, add a line at the end of the file to allow remote connections. The format follows this pattern:

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

In essence, this configuration allows all users to connect to all databases from the 192.168.1.0/24 subnet using password authentication with the secure SCRAM-SHA-256 method. Adjust the database, user, and address fields to match your security requirements.

Apply the Configuration Changes

After modifying both configuration files, restart PostgreSQL to apply the changes:

sudo systemctl restart postgresql

Then verify that PostgreSQL now listens on the configured addresses:

ss -tlnp | grep 5432
LISTEN 0      244          0.0.0.0:5432      0.0.0.0:*    users:(("postgres",pid=12345,fd=7))

Specifically, the 0.0.0.0:5432 output indicates PostgreSQL listens on all interfaces. If you configure a specific IP, you would see that address instead.

Troubleshoot Common Issues

Connection Refused Errors

If you receive โ€œconnection refusedโ€ when trying to connect remotely, the error typically looks like:

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?

To diagnose this issue, verify these items in order:

  1. Check PostgreSQL status: Run systemctl status postgresql
  2. Verify listening address: Run ss -tlnp | grep 5432
  3. Check firewall rules: Run sudo ufw status
  4. Review pg_hba.conf: Ensure the file allows your client IP

Authentication Failed

If you see a password authentication error, it typically appears as:

psql: error: connection to server at "192.168.1.50", port 5432 failed: FATAL:  password authentication failed for user "testuser"

Generally, the issue involves one of these causes:

  • Incorrect password: Reset it with ALTER USER username WITH PASSWORD 'newpassword';
  • Wrong authentication method: Check that pg_hba.conf matches your connection method
  • User does not exist: Verify with \du in psql

Repository Issues

If apt update shows errors about the PostgreSQL repository, you may see:

E: The repository 'https://apt.postgresql.org/pub/repos/apt noble-pgdg Release' does not have a Release file.

To resolve this, verify your configuration:

cat /etc/apt/sources.list.d/postgresql.sources

Ensure the Suites line contains your correct Ubuntu codename followed by -pgdg (e.g., noble-pgdg for Ubuntu 24.04 or jammy-pgdg for Ubuntu 22.04). If you run Ubuntu 26.04 and see โ€œRelease file not foundโ€, this is expected; the PostgreSQL APT repository does not yet have a branch for Ubuntu 26.04.

View PostgreSQL Logs

For detailed error information, review the PostgreSQL logs. The tail command displays the most recent entries:

sudo tail -50 /var/log/postgresql/postgresql-15-main.log

Update PostgreSQL 15

Since PostgreSQL 15 comes from the official PGDG repository, you receive updates through standard APT package management. To update PostgreSQL to the latest patch release:

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

Importantly, the --only-upgrade flag ensures the command only updates the packages if they already exist, preventing accidental installation on systems where PostgreSQL 15 might have been removed. For related PostgreSQL versions, see our guide on installing PostgreSQL 16 on Ubuntu.

Remove PostgreSQL 15

When you need to uninstall PostgreSQL 15, you can remove the packages while preserving or deleting your data.

Remove Packages Only (Preserve Data)

To remove PostgreSQL while keeping your databases and configuration files:

sudo apt remove postgresql-15 postgresql-client-15

As a benefit, this approach allows you to reinstall later without losing data.

Complete Removal Including Configuration

To remove PostgreSQL and its configuration files (but not databases):

sudo apt purge postgresql-15 postgresql-client-15
sudo apt autoremove

Furthermore, the autoremove command cleans up dependencies that were installed with PostgreSQL but are no longer needed.

Remove Everything Including Databases

Warning: The following commands permanently delete all PostgreSQL databases and data. You cannot undo this action. Back up any important data first using pg_dump.

sudo apt purge postgresql-15 postgresql-client-15 postgresql-common
sudo apt autoremove
sudo rm -rf /var/lib/postgresql/
sudo rm -rf /etc/postgresql/

Remove the PostgreSQL Repository

Finally, if you no longer need the PostgreSQL APT repository, remove it along with the GPG key:

sudo rm /etc/apt/sources.list.d/postgresql.sources
sudo rm /usr/share/keyrings/postgresql.gpg
sudo apt update

After running apt update, verify the repository removal succeeded:

apt-cache policy postgresql-15
N: Unable to locate package postgresql-15

As shown above, this output confirms APT no longer has access to the PostgreSQL repository.

Conclusion

You have successfully installed PostgreSQL 15 on Ubuntu using the official PostgreSQL APT repository. This setup provides the latest PostgreSQL 15 patch releases and security updates directly from the PostgreSQL Global Development Group. The guide covered the complete installation process, basic configuration including user and database creation, firewall setup for remote access, and maintenance procedures for updates and removal.

Moving forward, for production environments, consider implementing additional security measures such as SSL/TLS encryption for client connections, regular automated backups using pg_dump or continuous archiving, and monitoring solutions to track database performance and health. The official PostgreSQL documentation provides comprehensive guidance on these advanced topics.

Additional Resources

For more information about PostgreSQL 15 and ongoing maintenance, refer to these official resources:

5 thoughts on “How to Install PostgreSQL 15 on Ubuntu”

  1. Hi, i have a problem with installing on WSL 20.04, This is the error when trying to use the command:
    “sudo apt install postgresql-client-15 postgresql-15”
    => postgresql-15 : Depends: postgresql-common (>= 241~) but 214ubuntu0.1 is to be installed
    Depends: libpq5 (>= 14~beta2) but 12.22-0ubuntu0.20.04.1 is to be installed
    Recommends: sysstat but it is not going to be installed
    postgresql-client-15 : Depends: libpq5 (>= 15.12) but 12.22-0ubuntu0.20.04.1 is to be installed.

    Reply
    • Thanks for reporting this, Alex. The dependency errors you are seeing occur because Ubuntu 20.04’s base libraries are too old for the current PostgreSQL 15 packages. Specifically, your system has libpq5 version 12.22 and postgresql-common version 214, but PostgreSQL 15 now requires libpq5 15.12+ and postgresql-common 241+.

      Ubuntu 20.04 LTS reached end of standard support in April 2025, and this guide covers Ubuntu 22.04 and 24.04 LTS. The PostgreSQL APT repository still has packages for 20.04 (focal-pgdg), but dependency conflicts with the aging base system libraries make installation unreliable.

      Your best option is to upgrade your WSL Ubuntu installation to 22.04 or 24.04. In WSL, you can either install a fresh Ubuntu 22.04/24.04 distribution from the Microsoft Store, or attempt an in-place upgrade with sudo do-release-upgrade. After upgrading, the PostgreSQL 15 installation will work without dependency issues.

      Reply
    • Hello,

      Yes, the commands in this guide are designed specifically for Ubuntu and its versions, not Linux Mint or other Ubuntu-based distributions.

      If you’re using Linux Mint, Pop!_OS, or any other Ubuntu derivative, you’ll need to modify this part of the command:

      echo deb [arch=amd64,arm64,ppc64el signed-by=/usr/share/keyrings/postgresql.gpg] http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main | sudo tee /etc/apt/sources.list.d/postgresql.list

      The segment $(lsb_release -cs)-pgdg dynamically fetches the codename of your Ubuntu version. For example, if you’re using Linux Mint 22 (which is based on Ubuntu 24.04), you would replace that section with noble-pgdg.

      This ensures the command matches the right version for your system.

      Feel free to reach out if you have any further questions!

      Reply

Leave a Comment

Let us know you are human: