HomeDATABASEHow to Install PostgreSQL on Ubuntu 24.04

How to Install PostgreSQL on Ubuntu 24.04

In this guide, we shall learn How to Install PostgreSQL on Ubuntu 24.04. The open-source database PostgreSQL, sometimes spelled as “Post-GRES,” is well-known for its dependability, adaptability, and endorsement of open technological standards. PostgreSQL (link outside ibm.com) supports both relational and non-relational data types, in contrast to other RDMBS (Relational Database Management Systems). This places it among the most stable, advanced, and compliant relational databases on the market right now.

Why use PostgreSQL? Considering how quickly newer technologies are developing, it is imperative to maintain dynamic database systems in today’s digital environment. PostgreSQL’s rich extension ecosystem, which supports anything from time-series data types to geospatial analytics, enables it to swiftly handle a wide range of specific use cases.

How to Install PostgreSQL on Ubuntu 24.04

To install Postgres on Ubuntu 24.04, follow these detailed installation instructions:

Step 1. Run System Update

Before installing Postgres, let’s update the system packages using the command provided below:

sudo apt update -y && sudo apt upgrade -y

Step 2. Install PostgreSQL on Ubuntu 24.04

Once the system packages are up-to-date, you’re now ready to install PostgreSQL by running the command below. Be aware that the given command will install the default PostgreSQL 16.

sudo apt install postgresql postgresql-contrib -y

It will take a few, minutes to finish the installation.

Following installation, the PostgreSQL service is launched automatically.

$ sudo systemctl status postgresql.service
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: enabled)
     Active: active (exited) since Thu 2024-05-23 08:35:00 EAT; 1min 30s ago
   Main PID: 6381 (code=exited, status=0/SUCCESS)
        CPU: 3ms

To verify the version installed run:

$ sudo -u postgres psql -c "SELECT version();"
                                                         version                                                         
-------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 16.2 (Ubuntu 16.2-1ubuntu4) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 13.2.0-23ubuntu3) 13.2.0, 64-bit
(1 row)

Step 3. Create an Administrative User

Establishing an administrative user to oversee other users and databases is a smart idea. Initially, use the following command to log into the PostgreSQL shell:

$  sudo -u postgres psql
psql (16.2 (Ubuntu 16.2-1ubuntu4))
Type "help" for help.

postgres=# 

Then, run the following command to create an admin user and establish a password:

postgres=# CREATE ROLE root WITH LOGIN SUPERUSER CREATEDB CREATEROLE PASSWORD 'password';
CREATE ROLE

Next, execute the following command to confirm the newly formed user:

postgres=# \du
                             List of roles
 Role name |                         Attributes                         
-----------+------------------------------------------------------------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS
 root      | Superuser, Create role, Create DB

Step 4. Create a Database and User in PostgreSQL

We will demonstrate how to establish a database and user in PostgreSQL in this section. Run the following command in PostgreSQL to create a database called Postgresdb:

postgres=# create database Postgresdb;
CREATE DATABASE

In PostgreSQL, type the following command to create a user called testuser:

postgres=# create user testuser with encrypted password 'password';
CREATE ROLE

Now use the following command to give the Postgresdb database full access:

postgres=#  grant all privileges on database Postgresdb to testuser;
GRANT

With the following command, you can list all databases:

postgres=# \l

  Name    |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
------------+----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 postgres   | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 postgresdb | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =Tc/postgres         +
            |          |          |                 |             |             |            |           | postgres=CTc/postgres+
            |          |          |                 |             |             |            |           | testuser=CTc/postgres
 template0  | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
            |          |          |                 |             |             |            |           | postgres=CTc/postgres
 template1  | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
            |          |          |                 |             |             |            |           | postgres=CTc/postgres
(4 rows)

:

Conclusion

Well done! On Ubuntu 24.04, the PostgreSQL server has been successfully installed and configured. Outstanding performance and data integrity can now be achieved in a production setting by using PostgreSQL.

You can explore more on PostgreSQL documentation pages.

Check also:

- Advertisment -

Recent posts

LEAVE A REPLY

Please enter your comment!
Please enter your name here