How to Install PostgreSQL on Ubuntu 26.04

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:
- Have access to an Ubuntu 26.04 server instance as a non-root user with sudo privileges.
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.
Update the APT package index.
console$ sudo apt update
Install the
postgresql-commonpackage, which contains shared utilities and the repository setup script.console$ sudo apt install -y postgresql-common
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.Install the PostgreSQL server package.
console$ sudo apt install -y postgresql
Start the PostgreSQL service.
console$ sudo systemctl start postgresql
Enable PostgreSQL to start automatically at boot time.
console$ sudo systemctl enable postgresql
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.
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)Log in to the PostgreSQL console as the
postgressuperuser.console$ sudo -u postgres psql
Set a strong password for the
postgresaccount. Replacestrong_passwordwith a secure password.psqlpostgres=# ALTER USER postgres WITH ENCRYPTED PASSWORD 'strong_password';
Create a new database user named
example_adminwith an encrypted password. Replacestrong_passwordwith a secure password.psqlpostgres=# CREATE USER example_admin ENCRYPTED PASSWORD 'strong_password';
Exit the PostgreSQL console.
psqlpostgres=# EXIT;
Switch from peer authentication to
scram-sha-256password authentication by updating thepg_hba.conffile. Replace17with 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
Restart PostgreSQL to apply the authentication changes.
console$ sudo systemctl restart postgresql
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.
Create a new database named
example_storeand assign ownership to theexample_adminuser.console$ sudo -u postgres createdb example_store -O example_admin
Enter the
postgresuser password when prompted.Log in to the
example_storedatabase as theexample_adminuser.console$ sudo -u postgres psql -U example_admin -d example_store
Enter the
example_adminpassword and press Enter to access the database.Create a new table named
productsto store sample records.psqlexample_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_idis aPRIMARY KEYthat uniquely identifies each record.SERIALauto-generates a newproduct_idfor each inserted row.product_nameandcategorystore text values describing the product.pricestores the product price as a numeric value with two decimal places.
Insert sample records into the
productstable.psqlexample_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);
Query the
productstable to verify the inserted data.psqlexample_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)Exit the PostgreSQL console.
psqlexample_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.