How to Install PostgreSQL on Ubuntu 26.04

PostgreSQL is an open-source, object-relational database management system known for its reliability, feature set, and standards compliance. It is widely used in production environments ranging from small applications to large-scale data warehouses.
This guide explains how to install PostgreSQL 18 on Ubuntu 26.04, set up roles and authentication, and configure remote access.
Prerequisites
To follow this guide, you need to be logged in as a user with sudo privileges .
Installing PostgreSQL on Ubuntu 26.04
The default Ubuntu 26.04 repositories include PostgreSQL 18.
Start by updating the local package index:
sudo apt updateInstall the PostgreSQL server and the contrib package, which provides several additional features:
sudo apt install postgresql postgresql-contribOnce the installation is completed, the PostgreSQL service starts automatically. Use the psql tool to verify the installation by connecting to the PostgreSQL server and printing its version
:
sudo -u postgres psql -c "SELECT version();" version
----------------------------------------------------------------------------------------------------------------------------------
PostgreSQL 18.3 (Ubuntu 18.3-1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 15.2.0-14ubuntu1) 15.2.0, 64-bit
(1 row)PostgreSQL has been installed and is ready to use.
PostgreSQL Roles and Authentication
Database access in PostgreSQL is managed through roles. A role can represent a database user or a group of database users.
PostgreSQL supports several authentication methods . The most commonly used are:
- Trust lets a role connect without a password, as long as the conditions in
pg_hba.confare met. - Password lets a role connect by providing a password. Passwords can be stored as
scram-sha-256ormd5. - Ident is supported on TCP/IP connections. It obtains the client’s operating system username, with optional mapping.
- Peer works like Ident, but for local connections only.
PostgreSQL client authentication is defined in pg_hba.conf. By default, PostgreSQL uses the peer authentication method for local connections.
The postgres user is created automatically during installation. It is the superuser for the PostgreSQL instance, equivalent to the MySQL root user.
To log in to the PostgreSQL server as the postgres user, use the sudo
command:
sudo -u postgres psqlYou can also switch to the user first:
sudo su - postgres
psqlTo exit the PostgreSQL shell, type:
\qCreating a Role and Database
Only superusers and roles with the CREATEROLE privilege can create new roles.
The following example creates a role named john, sets a password for the role, and creates a database named johndb owned by that role:
Create a new PostgreSQL role:
Terminalsudo -u postgres createuser johnSet a password for the role:
Terminalsudo -u postgres psqlsqlALTER ROLE john WITH ENCRYPTED PASSWORD 'strong_password';Use a real password here. Do not store it in shell history, scripts, or version control.
Create a new database owned by that role:
Terminalsudo -u postgres createdb johndb --owner=john
Because john owns the database, it can create objects in the default public schema without additional grants.
Enabling Remote Access
By default, PostgreSQL only listens on localhost (127.0.0.1).
To allow remote connections, open the PostgreSQL configuration file:
sudo nano /etc/postgresql/18/main/postgresql.confFind the listen_addresses line in the CONNECTIONS AND AUTHENTICATION section and set it to '*' to listen on all interfaces:
listen_addresses = '*'Save the file and restart the PostgreSQL service:
sudo systemctl restart postgresqlVerify that PostgreSQL is now listening on all interfaces:
ss -nlt | grep 5432LISTEN 0 244 0.0.0.0:5432 0.0.0.0:*
LISTEN 0 244 [::]:5432 [::]:*The next step is to configure which hosts are allowed to connect by editing pg_hba.conf:
sudo nano /etc/postgresql/18/main/pg_hba.confAdd rules at the bottom of the file. Some examples:
# TYPE DATABASE USER ADDRESS METHOD
# Allow jane to connect to all databases from anywhere using a password
host all jane 0.0.0.0/0 scram-sha-256
# Allow jane to connect only to janedb from anywhere
host janedb jane 0.0.0.0/0 scram-sha-256
# Allow jane to connect from a trusted host without a password
host all jane 192.168.1.134 trustReload PostgreSQL to apply the changes:
sudo systemctl reload postgresqlFinally, open port 5432 in the firewall. To allow access from a specific subnet:
sudo ufw allow proto tcp from 192.168.1.0/24 to any port 5432Conclusion
We have shown you how to install PostgreSQL 18 on Ubuntu 26.04 and covered the basics of roles, authentication, and remote access. For more details, consult the PostgreSQL 18 documentation .
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page