How to install PostgreSQL 13 on Ubuntu 22.04 LTS Linux

Steps to install the PostgreSQL 13 on Ubuntu 22.04 LTS Jammy JellyFish by adding its repository using the command terminal.

Postgres is a platform-independent object-relational database management system (ORDBMS). In many Linux distributions, the database management system is part of the basic equipment. It can also be used on Windows and macOS systems. Due to the object relationality, the DBMS is suitable for data warehouse databases. It differs from relational database management systems such as MySQL in that even complex data objects can be stored relationally in the database. Postgres works on the client-server model. The server is responsible for managing the databases and processing and answering client requests.

In addition to the server and a command line-based client, many Linux distributions also supply a client program with a graphical user interface. The communication between client and server takes place in a distributed architecture via TCP/IP connection. Few features of Postgres are: distributed under an open source license, platform-independent; can be expanded in many ways with functions, self-defined data types or operators; no size limit; high reliability; stores data objects in the relational database schema, and more…

Steps to install PostgreSQL 13 on Ubuntu 22.04 LTS

The need for doing this article is, that the default version of PostgreSQL in the Ubuntu 22.04 Jammy repository is v14. Hence, to get the previous version still in use by some popular applications such as SonarQube, we have to go through this tutorial. 

1. Update Ubuntu 22.04

Let’s run the system update command before moving further in the tutorial. For that go to your command terminal and execute the given syntax:

sudo apt update && sudo apt upgrade

This will not only install the latest available updates but also rebuild the APT package index cache.  

2. Add a GPG key

If we are going to add a new repository on Ubuntu 22.04, then we also need a GPG key used to sign the packages available through it. Therefore, before adding the official repository of PostgreSQL on Ubuntu 22.04 add its public GPG key.

curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/apt.postgresql.org.gpg >/dev/null

3. Add PostgreSQL repository on Ubuntu 22.04

After adding the GPG key, let’s add the repository we need to install version 13 for this database server on Ubuntu 22.04. Remember, we are doing this because the official default system package repository of Ubuntu 22.04 doesn’t offer this version.

echo "deb [arch=amd64] http://apt.postgresql.org/pub/repos/apt/ jammy-pgdg main" | sudo tee /etc/apt/sources.list.d/postgresql.list

Run system update to rebuild APT index cache:

sudo apt update

4. Install PostgreSQL 13 on Ubuntu 22.04

Once you have followed the above steps without producing any error, your system is eligible to install version 13 of PostgreSQL on Ubuntu 22.04 LTS. Let’s do that with the help of the APT package manager.

sudo apt install postgresql-13
Install PostgreSQL 13 on Ubuntu 22.04

5. Check the status

Once the installation is completed to check and confirm the database server is running without any error, run the following command on your Terminal.

systemctl status postgresql --no-pager -l
check the PostgreSQL service status

6. Set Password for Admin User

To secure the default admin user of PostgresSQL, let’s set a password for it. To do that use the given command:

sudo passwd postgres

7. How to create a New Database

If you are new to PostgreSQL and looking for commands to create a new database on PostgreSQL, then you can learn that in this step.

1. Switch to postgres admin user. Use the password you have set for that in the previous step.

su - postgres

2. Now, create a  new user that will access the database we will create for Sonarqube.

createuser youruser

Note: Change youruser in the above command with whatever you want to use.

3. Switch to the PostgreSQL shell.

psql

4. To secure a newly created user, set a password for the same using the below syntax:

ALTER USER youruser WITH ENCRYPTED password 'yourpassword';

Note: change the bold items with whatever you want to use.

5. Create a new database on  PostgreSQL by running:

CREATE DATABASE yourdb OWNER youruser;

Note: You can use the yourdb name as per your choice and also don’t forget to replace the youruser in the above command with the one, you have created.

6. Exit from the psql shell:

\q

7. Get back to your system user

exit

8. Command to stop, restart and disable the Database server

Well, Postgres runs as a background service on our system, therefore, we can use systemctl command to stop, restart, and disable it.

To stop:

sudo systemctl stop postgresql

To restart:

sudo systemctl restart postgresql

To Disable: 

To disable the service from starting automatically with system boot.

sudo systemctl disable postgresql

To Enable

To enable the service and start automatically with system boot.

sudo systemctl enable postgresql

8. Uninstall or Remove PostgreSQL from Ubuntu 22.04

Maybe the project for what you are using the PostgreSQL is over and you don’t want it anymore or due to any reason you don’t want to continue with this database server then here is the command to completely remove it from your Ubuntu 22.04 Linux.

sudo apt autoremove --purge postgresql-13

To remove the Repository and GPG key:

sudo rm /etc/apt/sources.list.d/postgresql.list /etc/apt/trusted.gpg.d/apt.postgresql.org.gpg

FAQ

What is PostgreSQL used for?

Postgres works according to the client-server model. The server is responsible for managing the databases and processing and responding to client requests. Just like MySQL, the Postgres database management system is used to store data generated by web, mobile, geospatial, and analytics applications.

Why is PostgreSQL so popular?

PostgreSQL is a so-called object-relational database management system (ORDBMS). It differs from relational database management systems such as MySQL in that even complex data objects can be stored relationally in the database.

Few Features of Postgres that make it popular:

# Freely available under an open source license,
# Can be used platform-independently,
# Versatile expandable with functions, self-defined data types or operators,
# Stores data objects in the relational database schema,
# Large community available on the net,
# Complex queries possible,
# No size limit,
# ACID compliance
# High reliability through synchronous replication.

What language does PostgreSQL use?

SQL is the query language used by PostgreSQL and most other relational databases because it is portable and easy to learn.

What is the default Postgres password?

There is no need to use a password to log in default Postgres user. However, to secure it we can add a password using the command – sudo passwd postgres

Other Articles:

Install Postgresql 13 on AWS Ec2 Amazon Linux 2
Install PostgreSQL and pgAdmin in AlmaLinux / Rocky 8
How to install Pgadmin 4 on Ubuntu 20.04 LTS Linux

Leave a Comment

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