How to Install PostgreSQL on Ubuntu 26.04

Updated on 24 April, 2026
Install and configure PostgreSQL relational database on an Ubuntu 26.04 with user management, database creation, and access configuration.
How to Install PostgreSQL on Ubuntu 26.04 header image

PostgreSQL is an open-source relational database management system known for its extensibility, standards compliance, and support for advanced features such as complex queries, JSON data types, and full-text search. It handles workloads ranging from single-server applications to large-scale data warehouses, making it a preferred choice for analytics platforms, geographic information systems, and dynamic web applications.

This article explains how to install PostgreSQL on an Ubuntu 26.04 server. It covers adding the official PostgreSQL APT repository, enabling the database service, configuring password-based authentication, and creating a sample database with test records.

Prerequisites

Before you begin, you need to:

Install PostgreSQL

The postgresql-common package provides shared utilities and a setup script that adds the official PostgreSQL APT repository to your system. After adding the repository, install the server package and configure the service to start automatically on boot.

  1. Update the APT package index.

    console
    $ sudo apt update
    
  2. Install the postgresql-common package, which contains shared utilities and the repository setup script.

    console
    $ sudo apt install -y postgresql-common
    
  3. Run the setup script to add the official PostgreSQL APT repository.

    console
    $ sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
    

    The script displays the repository details. Press Enter to confirm and proceed:

    This script will enable the PostgreSQL APT repository on apt.postgresql.org on
    your system. The distribution codename used will be questing-pgdg.
    
    Press Enter to continue, or Ctrl-C to abort.
  4. Install the PostgreSQL server package.

    console
    $ sudo apt install -y postgresql
    
  5. Start the PostgreSQL service.

    console
    $ sudo systemctl start postgresql
    
  6. Enable PostgreSQL to start automatically at boot time.

    console
    $ sudo systemctl enable postgresql
    
  7. Verify that the PostgreSQL service is active.

    console
    $ sudo systemctl status postgresql
    

    Your output should be similar to the one below:

    ● postgresql.service - PostgreSQL RDBMS
         Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: enabled)
         Active: active (exited) since Wed 2026-03-25 18:08:31 UTC; 58s ago
    ......

Secure the PostgreSQL Database Server

PostgreSQL ships with a default superuser account named postgres that manages all administrative operations. Ubuntu configures peer authentication by default, which allows local system users to connect without a password. The following steps switch to password-based authentication using scram-sha-256, set a password for the postgres account, and create a dedicated database user.

  1. Check the installed PostgreSQL version.

    console
    $ psql --version
    

    Your output should be similar to the one below:

    psql (PostgreSQL) 18.3 (Ubuntu 18.3-1.pgdg26.04+1)
  2. Log in to the PostgreSQL console as the postgres superuser.

    console
    $ sudo -u postgres psql
    
  3. Set a strong password for the postgres account. Replace strong_password with a secure password.

    psql
    postgres=# ALTER USER postgres WITH ENCRYPTED PASSWORD 'strong_password';
    
  4. Create a new database user named example_admin with an encrypted password. Replace strong_password with a secure password.

    psql
    postgres=# CREATE USER example_admin ENCRYPTED PASSWORD 'strong_password';
    
  5. Exit the PostgreSQL console.

    psql
    postgres=# EXIT;
    
  6. Switch from peer authentication to scram-sha-256 password authentication by updating the pg_hba.conf file. Replace 17 with your installed PostgreSQL major version if it differs.

    console
    $ sudo sed -i '/^local/s/peer/scram-sha-256/' /etc/postgresql/18/main/pg_hba.conf
    
  7. Restart PostgreSQL to apply the authentication changes.

    console
    $ sudo systemctl restart postgresql
    
  8. Verify that the PostgreSQL service is active after the restart.

    console
    $ sudo systemctl status postgresql
    

    Your output should be similar to the one below:

    ● postgresql.service - PostgreSQL RDBMS
         Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: enabled)
         Active: active (exited) since Wed 2026-03-25 18:12:13 UTC; 11s ago
     Invocation: 9954ca43ab5646529cc34efe141b6aab
    ...

Access the PostgreSQL Database Server

The psql command-line utility ships with the PostgreSQL server package and provides direct access to the database console. The following steps create a sample database owned by the example_admin user, build a table, and populate it with test records.

  1. Create a new database named example_store and assign ownership to the example_admin user.

    console
    $ sudo -u postgres createdb example_store -O example_admin
    

    Enter the postgres user password when prompted.

  2. Log in to the example_store database as the example_admin user.

    console
    $ sudo -u postgres psql -U example_admin -d example_store
    

    Enter the example_admin password and press Enter to access the database.

  3. Create a new table named products to store sample records.

    psql
    example_store=> CREATE TABLE products (
               product_id SERIAL PRIMARY KEY,
               product_name VARCHAR(100),
               category VARCHAR(50),
               price NUMERIC(10,2)
          );
    

    The SQL statement creates a table with the following columns:

    • product_id is a PRIMARY KEY that uniquely identifies each record.
    • SERIAL auto-generates a new product_id for each inserted row.
    • product_name and category store text values describing the product.
    • price stores the product price as a numeric value with two decimal places.
  4. Insert sample records into the products table.

    psql
    example_store=> INSERT INTO products
           (product_name, category, price)
           VALUES
           ('Wireless Mouse', 'Accessories', 29.99),
           ('Mechanical Keyboard', 'Accessories', 79.50),
           ('USB-C Monitor', 'Displays', 349.00);
    
  5. Query the products table to verify the inserted data.

    psql
    example_store=> SELECT * FROM products;
    

    Your output should be similar to the one below:

     product_id |    product_name     |  category   | price
    ------------+---------------------+-------------+--------
              1 | Wireless Mouse      | Accessories |  29.99
              2 | Mechanical Keyboard | Accessories |  79.50
              3 | USB-C Monitor       | Displays    | 349.00
    (3 rows)
  6. Exit the PostgreSQL console.

    psql
    example_store=> EXIT;
    

Conclusion

You have installed PostgreSQL on an Ubuntu 26.04 server, enabled password-based authentication, and created a dedicated user with a sample database. PostgreSQL integrates with a wide range of application frameworks and supports replication for high-availability deployments. For more information, refer to the official PostgreSQL documentation.

Comments