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 17 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 17.
Start by updating the local package index:
Terminal
sudo apt update
Install the PostgreSQL server and the contrib package, which provides several additional features:
Terminal
sudo apt install postgresql postgresql-contrib
Once 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
:
Terminal
sudo -u postgres psql -c "SELECT version();"
version
----------------------------------------------------------------------------------------------------------------------------------
PostgreSQL 17.6 (Ubuntu 17.6-1build1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 13.3.0-6ubuntu2) 13.3.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:
Terminal
sudo -u postgres psql
You can also switch to the user
first:
Terminal
sudo su - postgres
psql
To exit the PostgreSQL shell, type:
Terminal
q
Creating 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 john -
Set a password for the role:
Terminalsudo -u postgres psqlsqlALTER ROLE john WITH ENCRYPTED PASSWORD 'strong_password';
WarningUse a real password here, and 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
johnowns the database, it can create objects in the defaultpublicschema 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:
Terminalsudo nano /etc/postgresql/17/main/postgresql.confFind the
listen_addressesline in theCONNECTIONS AND AUTHENTICATIONsection and set it to'*'to listen on all interfaces:/etc/postgresql/17/main/postgresql.confcfglisten_addresses = '*'Save the file and restart the PostgreSQL service:
Terminalsudo systemctl restart postgresqlVerify that PostgreSQL is now listening
on all interfaces:
Terminalss -nlt | grep 5432outputLISTEN 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:
Terminalsudo nano /etc/postgresql/17/main/pg_hba.confAdd rules at the bottom of the file. Some examples:
/etc/postgresql/17/main/pg_hba.confconf# 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:
Terminalsudo systemctl reload postgresqlFinally, open port
5432in the firewall. To allow access from a specific subnet:
Terminalsudo ufw allow proto tcp from 192.168.1.0/24 to any port 5432
WarningMake sure your firewall is configured to accept connections only from trusted IP addresses.Conclusion
We have shown you how to install PostgreSQL 17 on Ubuntu 26.04 and covered the basics of roles, authentication, and remote access. For more details, consult the PostgreSQL 17 documentation
. -




