Home → Articles → How to Install PostgreSQL Database Server on Ubuntu 26.04

How to Install PostgreSQL Database Server on Ubuntu 26.04

Published: Last updated:

Introduction

PostgreSQL is a free, open-source relational database management system that offers advanced features and supports many modern programming languages like Python, Java, C++, PHP, Golang, and Node.js. The database server handles flexible data types and supports different operating systems. These features make PostgreSQL a powerful and versatile choice for developers and businesses.

This guide shows you how to install PostgreSQL on Ubuntu 26.04.

Prerequisites

Before you start:

Install PostgreSQL

The PostgreSQL package is available by default on the Ubuntu repositories. To install the package, follow these steps.

  • Refresh your system's package list to ensure you get the latest available versions.

    console
    $ sudo apt update
    
  • Install the PostgreSQL package and the required dependencies.

    console
    $ sudo apt install -y postgresql postgresql-contrib
    

    Output:

    Reading package lists... Done
    Building dependency tree... Done
    Reading state information... Done
    The following additional packages will be installed:
      libcommon-sense-perl libjson-perl libjson-xs-perl libllvm19 libpq5
      libtypes-serialiser-perl postgresql-18 postgresql-client-18
      postgresql-client-common postgresql-common
    Suggested packages:
      postgresql-doc
    The following NEW packages will be installed:
      libcommon-sense-perl libjson-perl libjson-xs-perl libllvm19 libpq5
      libtypes-serialiser-perl postgresql postgresql-18 postgresql-client-18
      postgresql-client-common postgresql-contrib postgresql-common
    0 upgraded, 12 newly installed, 0 to remove and 0 not upgraded.
    Need to get 41.2 MB of archives.
    After this operation, 231 MB of additional disk space will be used.
    
  • Verify the PostgreSQL version using the psql client, an interactive command‑line tool that ships with PostgreSQL.

    console
    $ psql --version
    

    Output:

    psql (PostgreSQL) 18.3 (Ubuntu 18.3-1)
    

Manage PostgreSQL Service

PostgreSQL runs as a system service on Ubuntu under the name postgresql. You can manage this service by using specific commands to start, stop, restart, and check the PostgreSQL status. These commands ensure your PostgreSQL server runs smoothly and make maintenance easier on your system.

Check PostgreSQL Status

console
$ sudo systemctl status postgresql

Output:

● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: enabled)
     Active: active (exited) since Tue 2026-03-10 10:15:23 UTC; 5min 12s ago
    Process: 1143 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 1143 (code=exited, status=0/SUCCESS)
        CPU: 2ms

Press Ctrl + C to return to the shell prompt

Enable PostgreSQL to Start on Boot

console
$ sudo systemctl enable postgresql

Output:

Synchronizing state of postgresql.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable postgresql

Start PostgreSQL Service

console
$ sudo systemctl start postgresql

Stop PostgreSQL Service

console
$ sudo systemctl stop postgresql

Restart PostgreSQL Service

console
$ sudo systemctl restart postgresql

Secure PostgreSQL Database Server

By default, you can log in to your PostgreSQL server using the postgres user account without a password on your localhost. To secure the postgres user account with a password, follow the steps below.

  • Log in to the PostgreSQL server.

    console
    $ sudo -u postgres psql
    

    Output:

    psql (18.2 (Ubuntu 18.2-1ubuntu1))
    Type "help" for help.
    
    postgres=#
    
  • Change the password for the postgres user.

    postgresql
    postgres=# \password postgres
    

    Output:

    Enter new password: your_password_here
    Enter it again: your_password_here
    
  • Log out from the PostgreSQL server.

    postgresql
    postgres=# \q
    

    Output:

    $
    
  • Open the main PostgreSQL client authentication configuration file.

    console
    $ sudo nano /etc/postgresql/18/main/pg_hba.conf
    
  • Locate the administrative login directive.

    INI
    # Database administrative login by Unix domain socket
    local   all             postgres                                peer
    
    # TYPE  DATABASE        USER            ADDRESS                 METHOD
    
  • Change peer to md5.

    INI
    # Database administrative login by Unix domain socket
    local   all             postgres                                md5
    
    # TYPE  DATABASE        USER            ADDRESS                 METHOD
    
  • Save and close the /etc/postgresql/18/main/pg_hba.conf file by pressing Ctrl + X, Y then Enter.

  • Restart the PostgreSQL service to load the new security changes.

    console
    $ sudo systemctl restart postgresql
    

Test the PostgreSQL Database Server

In this section, you'll test PostgreSQL by logging into the database server, creating a sample database and table, inserting some data, and querying the table to ensure everything functions correctly.

  • Log in to the PostgreSQL server as postgres user and enter the password you defined.

    console
    $ sudo -u postgres psql
    

    Output:

    Password: your_password_here
    psql (18.2 (Ubuntu 18.2-1ubuntu1))
    Type "help" for help.
    
    postgres=#
    
  • Create a new company_db database.

    postgresql
    postgres=# CREATE DATABASE company_db;
    

    Output:

    CREATE DATABASE
    
  • List all databases and ensure company_db appears in the list.

    postgresql
    postgres=# \l
    

    Output:

    postgresql
                                      List of databases
       Name    |  Owner   | Encoding | Locale Provider | Collate |  Ctype  | Access privileges
    -----------+----------+----------+-----------------+---------+---------+-------------------
     company_db | postgres | UTF8     | libc            | C.UTF-8 | C.UTF-8 |
     postgres  | postgres | UTF8     | libc            | C.UTF-8 | C.UTF-8 |
     template0 | postgres | UTF8     | libc            | C.UTF-8 | C.UTF-8 | =c/postgres       +
               |          |          |                 |         |         | postgres=CTc/postgres
     template1 | postgres | UTF8     | libc            | C.UTF-8 | C.UTF-8 | =c/postgres       +
               |          |          |                 |         |         | postgres=CTc/postgres
    (4 rows)
    
  • Press Q to exit from the list.

  • Connect to the new company_db.

    postgresql
    postgres=# \c company_db;
    

    Output:

    You are now connected to database "company_db" as user "postgres".
    company_db=#
    
  • Create a sample employees table.

    postgresql
    company_db=# CREATE TABLE employees (
        employee_id SERIAL PRIMARY KEY,
        full_name VARCHAR(100),
        email VARCHAR(255)
    );
    

    Output:

    CREATE TABLE
    
  • Populate the employees table with sample records.

    postgresql
    company_db=# INSERT INTO employees(full_name, email) 
                 VALUES 
                     ('MARY ROE', 'mary_roe@example.com'),
                     ('JAY SMITH', 'jay_smith@example.com'),
                     ('ANNA KIM', 'anna_kim@example.com');
    

    Output:

    INSERT 0 3
    
  • Query the employees table to verify the records.

    postgresql
    company_db=# SELECT
        employee_id,
        full_name,
        email
    FROM employees;
    

    Output:

    postgresql
    employee_id | full_name |         email
    ------------+-----------+-----------------------
              1 | MARY ROE  | mary_roe@example.com
              2 | JAY SMITH | jay_smith@example.com
              3 | ANNA KIM  | anna_kim@example.com
    (3 rows)
    
  • Exit from the PostgreSQL prompt.

    postgresql
    company_db=# \q
    

Your PostgreSQL database server is working as expected.

Conclusion

In this guide, you have installed PostgreSQL on Ubuntu 26.04 using apt, secured the database server with a password, and created a sample database with tables and records. Now that you have PostgreSQL running, consider integrating the database with a programming language like Python using psycopg2 or with Node.js using the pg package to build data-driven applications.