How to Install Postgresql on Ubuntu 22.04 LTS

In this article We are going to perform How to install Postgresql on Ubuntu 22.04 LTS and How we can use PostgreSQL, create database, How to create user in PostgreSQL and How to see file location in PostgreSQL Database.

What is PostgreSQL ?

PostgreSQL (often referred to as “Postgres”) is an open-source object-relational database management system (DBMS). It is a powerful and highly extensible database system that provides robust data storage, retrieval, and management capabilities.

PostgreSQL is known for its compliance with SQL standards and its ability to handle complex workloads while maintaining data integrity and reliability. It supports various features of modern relational databases, such as transactions, views, foreign keys, and joins, and it also includes advanced functionalities like stored procedures, triggers, and user-defined types.

One of the notable strengths of PostgreSQL is its extensibility. It allows users to define their own data types, operators, and functions, which can be used to create custom solutions tailored to specific application requirements. PostgreSQL also offers support for various programming languages, including Python, Java, C/C++, and many others, making it flexible and adaptable to different development environments.

Additionally, PostgreSQL provides advanced features like built-in replication, high availability, and asynchronous and synchronous commit capabilities, making it suitable for enterprise-level applications that require scalability and fault tolerance.

Overall, PostgreSQL is a feature-rich and reliable DBMS that is widely used in both small-scale and large-scale applications across a range of industries. Its open-source nature encourages community-driven development and continuous improvement, making it a popular choice for developers and organizations seeking a robust and flexible database solution.

Step #1:Enable PostgreSQL Package Repository

Postgresql package is not available in the default package repository, so enable its official package repository using create the file repository configuration

sudo sh -c 'echo "deb $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

Import the repository signing key using following command

wget --quiet -O - | sudo apt-key add -

Update all system packages

sudo apt-get update

Step #2:Install PostgreSQL on Ubuntu 22.04 LTS

Install latest version of postgresql 

sudo apt-get -y install postgresql

if you want to install specific version the use ‘postgresql-12’.

After the installation completed the enable and start the postgresql service using following command

sudo systemctl enable postgresql
sudo systemctl start postgresql

also you check the status of postgresql using below command

sudo systemctl status postgresql


How to Install Postgresql on Ubuntu 22.04 LTS 1

To check postgresql installed version

psql –-version


How to Install Postgresql on Ubuntu 22.04 LTS 2

We have covered Install PostgreSQL on Ubuntu 22.04 LTS.

Step #3:To connect postgresql account using command line in Ubuntu

  1. Login to PostgreSQL database on Ubuntu using command line
sudo -u postgres psql

To exit from postgresql using



How to Install Postgresql on Ubuntu 22.04 LTS 3

2. Access using default user account

sudo -i -u postgres

To connect postgresql


To exit from postgresql using



How to Install Postgresql on Ubuntu 22.04 LTS 4

Step #4:To set the password in PostgreSQL

By default, we can connect to the Postgresql server without using any password. In this step we will see how to set the password.

ALTER USER postgres PASSWORD ‘enter the password’;

To check password has been set successfully, for that exit from your current session and try again to log in.

psql -h localhost -U postgres


How to Install Postgresql on Ubuntu 22.04 LTS 5

The password has been set successfully.

Step #5:To create new database in PostgreSQL

For create new database use below query

create database db_name;

To create table inside the newly created database using below query

create table t_name(add data types);

To insert data in the table

insert into t_name (add data);

To see the the table or information in that table

select * from t_name;
How to Install Postgresql on Ubuntu 22.04 LTS 6

Step #6:To create users and check the list of users in PostgreSQL

See the list of users using below command


Now let’s create new user

create user devops with password ‘dev@123’;

 now again you can see list of users



How to Install Postgresql on Ubuntu 22.04 LTS 7

Step #7:Find Configuration file in PostgreSQL

If you want to find configuration file information using

show config_file;

authentication file location

show hba_file;

to find large or data directory

show data_directory;

to find log directory

show log_directory;


In this article we have covered how to install Postgresql on Ubuntu 22.04 LTS and how we can use postgresql, create database, how to create user and see file location.

Related Articles:

How to Install Apache Kafka on Ubuntu 22.04 LTS


Postgresql installation official page

FOSS TechNix

FOSS TechNix (Free,Open Source Software's and Technology Nix*) founded in 2019 is a community platform where you can find How-to Guides, articles for DevOps Tools,Linux and Databases.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap