General

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();"

output
 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.conf are met.
  • Password lets a role connect by providing a password. Passwords can be stored as scram-sha-256 or md5.
  • 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:

  1. Create a new PostgreSQL role:


    Terminal
    sudo -u postgres createuser john
  2. Set a password for the role:


    Terminal
    sudo -u postgres psql

    sql
    ALTER ROLE john WITH ENCRYPTED PASSWORD 'strong_password';

    Warning
    Use a real password here, and do not store it in shell history, scripts, or version control.
    
    1. Create a new database owned by that role:


      Terminal
      sudo -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:


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

    Find the listen_addresses line in the CONNECTIONS AND AUTHENTICATION section and set it to '*' to listen on all interfaces:

    /etc/postgresql/17/main/postgresql.confcfg
    listen_addresses = '*'

    Save the file and restart the PostgreSQL service:


    Terminal
    sudo systemctl restart postgresql

    Verify that PostgreSQL is now listening
    on all interfaces:


    Terminal
    ss -nlt | grep 5432

    output
    LISTEN 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:


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

    Add 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 trust

    Reload PostgreSQL to apply the changes:


    Terminal
    sudo systemctl reload postgresql

    Finally, open port 5432 in the firewall. To allow access from a specific subnet:


    Terminal
    sudo ufw allow proto tcp from 192.168.1.0/24 to any port 5432

    Warning
    Make 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
    .

AI Assistant
Context loaded: How to Install PostgreSQL on Ubuntu 26.04