Skip to main content

Install PostgreSQL on Ubuntu 26.04: The Complete Guide

Are you ready to discover how to install PostgreSQL on Ubuntu 26.04? You are in the right place! PostgreSQL, often referred to simply as Postgres, is a powerful open-source relational database management system (RDBMS), known for its reliability, robustness, and rich feature set. Whether you are developing a new web application, managing large amounts of data, or simply exploring the world of databases, this guide will take you through it step by step.

In this article, we will see together how to install the latest version of PostgreSQL on Ubuntu 26.04 LTS, configure basic access, and take your first steps with this extraordinary database.

Why Choose PostgreSQL?

PostgreSQL stands out for several reasons:

  • Open Source and Free: No licensing costs.
  • Extensibility: Supports custom data types, functions, operators, and aggregates.
  • SQL Standard Compliance: High adherence to SQL standards.
  • Reliability and Stability: Ideal for mission-critical applications thanks to its robust architecture.
  • Active Community: Extensive documentation and community support.
  • Advanced Features: Support for JSON, ACID transactions, replication, and much more.

If you are looking for a powerful and flexible database solution for your Ubuntu 26.04 server, PostgreSQL is an excellent choice.

Prerequisites

Before starting, make sure you have:

Step 1: Update the Ubuntu System

It is always a good practice to update the package list and installed packages before adding new software. To ensure that your business growth is not hindered, we recommend our design of highly scalable, intrinsically secure, and perfectly optimized IT infrastructures for your database. Open the terminal and type:

sudo apt update
sudo apt upgrade -y

This command will update the list of available packages and subsequently update all installed packages to the latest version.

Step 2: Install PostgreSQL on Ubuntu 26.04

Ubuntu 26.04 includes a recent version of PostgreSQL in its official repositories. Currently, the recommended default version is PostgreSQL 18. To install it, along with the postgresql-contrib package which provides additional utilities and features, run:

sudo apt install postgresql postgresql-contrib -y

Verify the Installation

Once the installation is complete, the PostgreSQL service should start automatically. You can check its status with the following command:

sudo systemctl status postgresql.service

If everything went well, you should see an output indicating that the service is active (running). Press q to exit the status view.

You can also verify the installed version with:

psql --version

It should return something like psql (PostgreSQL) 18.x (Ubuntu 18.x-...).

Step 3: Access PostgreSQL and Configure the postgres User

During installation, PostgreSQL creates an operating system-level user called postgres. This user has the default superuser role within the database.

Connection via the postgres user

To connect to the PostgreSQL prompt (called psql), you must first switch to the operating system's postgres user:

sudo -i -u postgres

Once you become the postgres user, you can access the psql prompt:

psql

Now you should be in the PostgreSQL prompt, indicated by postgres=#. To exit the psql prompt, type \q and press Enter. To return to your normal user, type exit and press Enter.

Set a Password for the postgres User

For security reasons, it is crucial to set a password for the database's postgres user. Within the psql prompt (after connecting as described above), type:

ALTER USER postgres PASSWORD 'secure-password';

Replace 'secure-password' with a strong password. Remember that this password is for the postgres database user, not the operating system user.

Step 4: Create a New User and a New Database (Recommended)

For most applications, it is not recommended to use the postgres user directly. It is a good practice to create dedicated users with specific privileges for your databases.

Create a New Role User

Still from the psql prompt (connected as the postgres user), you can create a new role (user). For example, to create a user named user:

CREATE USER new_user WITH ENCRYPTED PASSWORD 'user-password';

Create a New Database

Now, let's create a database. For example, a database named database owned by the newly created new_user:

CREATE DATABASE database OWNER new_user;

Grant Privileges to the New User on the Database

Even though user is the owner of the database, you might want to explicitly grant all privileges:

GRANT ALL PRIVILEGES ON DATABASE database TO new_user;

Exit the psql prompt (\q) and the postgres user (exit).

Verify the Connection with the New User

Now you can try connecting to your new database with your new user directly from your regular Linux user (without sudo -u postgres), if you have configured md5 or scram-sha-256 authentication (usually default for local connections after setting a password).

psql -U new_user -d database -h localhost

You will be prompted for the password you set for user. If the connection is successful, you will see the database=> prompt.

Note: The default authentication method for local connections (host or local) is often peer for the postgres user and md5 or scram-sha-256 for other users after they have been assigned a password. If you encounter authentication issues, you may need to modify the pg_hba.conf file. For Ubuntu 26.04 with PostgreSQL 18, it is usually located in /etc/postgresql/18/main/pg_hba.conf.

Useful Commands for PostgreSQL Management

Here are some useful systemctl commands to manage the PostgreSQL service:

  • Start PostgreSQL:

    sudo systemctl start postgresql
  • Stop PostgreSQL:

    sudo systemctl stop postgresql
  • Restart PostgreSQL:

    sudo systemctl restart postgresql
  • Enable automatic start of PostgreSQL on boot:

    sudo systemctl enable postgresql
  • Disable automatic start of PostgreSQL on boot:

    sudo systemctl disable postgresql

Frequently Asked Questions (FAQ) on PostgreSQL Ubuntu 26.04

  • Q: Which version of PostgreSQL is installed on Ubuntu 26.04 with apt?
    A: Ubuntu 26.04 includes PostgreSQL 18 in its default repositories.
  • Q: Where are the PostgreSQL configuration files located?
    A: The main configuration files (postgresql.conf and pg_hba.conf) are usually located in /etc/postgresql/18/main/ (the version number may vary if you install a different version).
  • Q: How can I enable remote connections to PostgreSQL?
    A: You need to modify two files:

    1. postgresql.conf: set listen_addresses = '*'.
    2. pg_hba.conf: add a line to allow connections from the desired network, for example host all all 0.0.0.0/0 md5 (for IPv4) or host all all ::/0 md5 (for IPv6). Warning: 0.0.0.0/0 allows connections from any IP, carefully evaluate the security implications.

    Remember to restart the PostgreSQL service after making changes.

  • Q: What is a popular GUI tool for managing PostgreSQL?
    A: pgAdmin is the most popular open-source administration and development tool for PostgreSQL. It is available as a web or desktop application.
  • Q: How do you back up a PostgreSQL database?
    A: You can use the pg_dump utility. For example: pg_dump -U new_user -d database -f backup.sql.

Conclusion

Congratulations! You now have a perfectly functioning PostgreSQL server on your Ubuntu 26.04 installation. You have learned how to install PostgreSQL, secure the administrator user, create new users and databases, and manage the service. If transitioning to this new environment brings a paralyzing fear of service interruptions, rely on our management of guaranteed "Zero-Downtime" migrations from On-Premise to Cloud environments.

PostgreSQL offers a world of possibilities. I encourage you to explore the official PostgreSQL documentation to dive deeper into its many features.

Scale Your Database on Ubuntu

Is the server consuming too much CPU? Are queries timing out, or do you need to configure an advanced connection pooling system? We secure and tune your infrastructure, handling the heaviest loads so you can return to focusing solely on the code.

Speak with a Systems Engineer (30 min. free)

Add new comment

Comment

  • Allowed HTML tags: <br> <p> <code class="language-*"> <pre>
  • Lines and paragraphs break automatically.
  • Only images hosted on this site may be used in <img> tags.