PostgreSQL 15 is available on Ubuntu 22.04, 24.04, and 26.04 through the official PGDG APT repository. Ubuntu’s default repositories ship different PostgreSQL versions (14 on 22.04, 16 on 24.04, and 18 on 26.04), so the PGDG repository is the recommended way to install PostgreSQL 15 on Ubuntu with the latest patch releases and security updates directly from the PostgreSQL team.
PostgreSQL 15 introduces the SQL-standard MERGE command, LZ4 and Zstandard backup compression, enhanced logical replication, and improved sort performance. These features make it a strong fit for workloads that depend on complex data operations, from development databases to large-scale production deployments. You finish with PostgreSQL 15 installed, configured for local and remote access, and secured behind UFW firewall rules.
Import the PostgreSQL APT Repository on Ubuntu
Compare PostgreSQL Versions for Ubuntu
Before proceeding, review the comparison below to decide whether PostgreSQL 15 fits your requirements or if another version better matches your stability or feature needs.
| PostgreSQL Version | Availability on Ubuntu | Choose It When | Trade-offs |
|---|---|---|---|
| PostgreSQL 15 | PGDG repository on 22.04/24.04/26.04 | You need MERGE command, improved logical replication, or LZ4/Zstandard compression | Requires external repository; not the default on any Ubuntu LTS |
| PostgreSQL 16 | Ubuntu 24.04 default, PGDG on 22.04/26.04 | General-purpose production with Canonical-managed security through April 2029 | Missing some PostgreSQL 17 features; PGDG required on 22.04 and 26.04 |
| PostgreSQL 17 | PGDG on 22.04/24.04/26.04 | Incremental backup and improved JSON support | Not the default on any current Ubuntu LTS; requires PGDG repository |
| PostgreSQL 18 | Ubuntu 26.04 default, PGDG on 22.04/24.04 | Latest features on the newest Ubuntu LTS with Canonical-managed updates | Newest release; ecosystem tooling may take time to catch up |
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.
Update Ubuntu Before PostgreSQL Installation
Before adding any external repository, update your package index and upgrade any outdated packages:
sudo apt update && sudo apt upgrade
If you do not have sudo configured, see our guide on adding a user to sudoers on Ubuntu.
Install Required Packages for PostgreSQL
Install the packages needed to securely download and verify the PostgreSQL repository. The curl command fetches the GPG key, gpg processes it, and lsb-release identifies your Ubuntu codename for repository configuration:
sudo apt install curl ca-certificates gpg lsb-release -y
Import the PostgreSQL GPG Key
The PostgreSQL team cryptographically signs all packages from their repository. Import the official signing key into your system’s keyring to verify their authenticity:
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 APT Repository
With the GPG key in place, add the official PostgreSQL repository. The following command creates a DEB822-format configuration file, which is the modern standard for APT sources:
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 echo output pipes through sudo tee to write the file with root privileges, since standard shell redirection does not inherit sudo. The two command substitutions run automatically when pasted: $(lsb_release -cs) detects your Ubuntu codename (jammy for 22.04, noble for 24.04, or resolute for 26.04) and $(dpkg --print-architecture) detects your CPU architecture (usually amd64). 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 the PostgreSQL 15 Package Source
Before installing, confirm that the PostgreSQL 15 package comes from the PGDG repository:
apt-cache policy postgresql-15
postgresql-15:
Installed: (none)
Candidate: 15.16-1.pgdg24.04+1
Version table:
15.16-1.pgdg24.04+1 500
500 https://apt.postgresql.org/pub/repos/apt noble-pgdg/main amd64 Packages
This output confirms the package installs from the PostgreSQL APT repository (apt.postgresql.org) rather than Ubuntu’s default repositories.
Install PostgreSQL 15 on Ubuntu
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 PostgreSQL 15 Installation
After installation completes, verify that the PostgreSQL service runs properly:
systemctl status postgresql
The output should show 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.16 (Ubuntu 15.16-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.16-1.pgdg22.04+1 and on Ubuntu 26.04 it shows 15.16-1.pgdg26.04+1. All three install the same PostgreSQL 15.16 release.
If PostgreSQL does not start automatically, enable and start it with the following command:
sudo systemctl enable postgresql --now
Manage the PostgreSQL 15 Service on Ubuntu
PostgreSQL runs as a systemd service. Use the commands below to control the database server:
| Action | Command |
|---|---|
| Stop PostgreSQL | sudo systemctl stop postgresql |
| Start PostgreSQL | sudo systemctl start postgresql |
| Restart PostgreSQL (required after config changes) | sudo systemctl restart postgresql |
| Reload configuration without restart | sudo systemctl reload postgresql |
| Check service status | systemctl status postgresql |
Configure PostgreSQL 15 on Ubuntu
Access the PostgreSQL 15 Shell
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, switch to the postgres user:
sudo -i -u postgres
Once logged in as the postgres user, start the interactive SQL shell:
psql
The terminal prompt changes to postgres=#, confirming the database connection:
psql (15.16 (Ubuntu 15.16-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, access the PostgreSQL shell directly without switching users:
sudo -u postgres psql
This is faster for quick database operations when you do not need a persistent postgres session.
Create a PostgreSQL Database User
For security best practices, avoid using the postgres superuser for application connections. Create dedicated users with appropriate privileges instead, 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
For a standard application user, answer “no” to all prompts to follow the principle of least privilege.
Set a password for the new user by connecting to the database and running:
sudo -u postgres psql -c "ALTER USER <username> WITH PASSWORD 'your_secure_password';"
ALTER ROLE
Create a PostgreSQL Database
Once you create a user, create a database and grant access. First, create the database:
sudo -u postgres createdb <database_name>
Grant the new user full privileges on this database:
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE <database_name> TO <username>;"
GRANT
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 on Ubuntu
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 more details, see our UFW firewall guide for Ubuntu.
Ensure UFW is installed and enabled:
sudo apt install ufw -y
sudo ufw enable
Before enabling UFW, allow SSH access with
sudo ufw allow sshif 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):
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
To remove a rule, use the rule number from the output above:
sudo ufw delete 2
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 for PostgreSQL 15 on Ubuntu
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 PostgreSQL Listening Address
First, identify the PostgreSQL configuration directory for your installation:
ls /etc/postgresql/
15
This output confirms PostgreSQL 15 installation. 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 PostgreSQL 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
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 PostgreSQL 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))
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 PostgreSQL Issues on Ubuntu
Fix PostgreSQL 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:
- Check PostgreSQL status: Run
systemctl status postgresql - Verify listening address: Run
ss -tlnp | grep 5432 - Check firewall rules: Run
sudo ufw status - Review pg_hba.conf: Ensure the file allows your client IP
Fix PostgreSQL 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"
The issue typically 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
\duin psql
Fix PostgreSQL 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, jammy-pgdg for Ubuntu 22.04, or resolute-pgdg for Ubuntu 26.04).
View PostgreSQL 15 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 on Ubuntu
The PGDG repository delivers updates through standard APT package management. To update PostgreSQL 15 to the latest patch release:
sudo apt update
sudo apt install --only-upgrade postgresql-15 postgresql-client-15
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 guides, see installing PostgreSQL 16 on Ubuntu or installing PostgreSQL 17 on Ubuntu.
Remove PostgreSQL 15 from Ubuntu
When you need to uninstall PostgreSQL 15, you can remove the packages while preserving or deleting your data.
Remove PostgreSQL 15 Packages Only (Preserve Data)
To remove PostgreSQL while keeping your databases and configuration files:
sudo apt remove postgresql-15 postgresql-client-15
This approach allows reinstallation later without losing data.
Purge PostgreSQL 15 Including Configuration
To remove PostgreSQL and its configuration files (but not databases):
sudo apt purge postgresql-15 postgresql-client-15
sudo apt autoremove
The autoremove command cleans up dependencies that were installed with PostgreSQL but are no longer needed.
Remove PostgreSQL 15 Including Databases
The following commands permanently delete all PostgreSQL databases and data. 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 APT Repository
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
This output confirms APT no longer has access to the PostgreSQL repository.
Frequently Asked Questions about PostgreSQL 15 on Ubuntu
Ubuntu 22.04 ships PostgreSQL 14, Ubuntu 24.04 ships PostgreSQL 16, and Ubuntu 26.04 ships PostgreSQL 18. PostgreSQL 15 is not the default on any Ubuntu LTS and requires the PGDG repository.
Install the postgresql-client-15 package with sudo apt install postgresql-client-15. This gives you psql and other command-line utilities without the database server, which is useful for remote administration.
This error means the PGDG repository is not configured. Import the PostgreSQL GPG key and add the DEB822 sources file as shown in the Import the PostgreSQL APT Repository section above, then run sudo apt update before installing.
PostgreSQL 15 reaches end of life on November 11, 2027. After that date, the PostgreSQL team will stop releasing security patches and bug fixes. Plan a migration to a newer major version before that deadline.
Conclusion
PostgreSQL 15 is installed and configured on Ubuntu through the PGDG APT repository with the database service running, user authentication in place, and firewall rules applied. Ongoing maintenance requires only apt update and --only-upgrade commands for patch releases. For production hardening, the PostgreSQL documentation, PostgreSQL 15 release notes, and PostgreSQL community cover SSL/TLS encryption, automated backups, and performance monitoring.
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.
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
libpq5version 12.22 andpostgresql-commonversion 214, but PostgreSQL 15 now requireslibpq515.12+ andpostgresql-common241+.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.You must use jammy-pgdg as virginia-pgdg is not present
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.listThe 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!
I love this app.