Install PostgreSQL on Linux


The steps will be of how to install and set up PostgreSQL on Ubuntu 20.04 LTS.

1. Go to the official website of PostgreSQL, i.e., https://www.postgresql.org/

Click on the download button present on the screen. Click on this button and you will be redirected to the download page of PostgreSQL.

2. Now, select the Operating System for which you want to install PostgreSQL.

We’ll select Linux here because we are doing it for Ubuntu, which itself is a distribution of Linux.

3. After clicking on Linux, you can see various Linux distribution which supports PostgreSQL.

For our installation, we’ll choose Ubuntu.

4. After clicking on Ubuntu, a list of commands will be displayed. We have to run those commands on the Ubuntu terminal for successful installation of the PostgreSQL.

The file repository configuration will be created:
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
# The signing key for the repository will be added:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
# Now we need to update the list of packages for the apt-get package manager:
sudo apt-get update
# Now we’ll proceed to the installation of the latest version of PostgreSQL.
# Use 'postgresql-12' or similar command instead of 'postgresql', if you want to install a specific version:
sudo apt-get -y install postgresql

Once you run all these commands, you are all set to use PostgreSQL by CLI on your Linux machine.

Now to start PostgreSQL, we need to run some commands and those are:

  • sudo -i -u postgres
  • psql

After successful execution of the above two commands, we are all set to use the PostgreSQL database.

This is how a PostgreSQL CLI looks like, now we can run various PostgreSQL queries here.

Let us begin with creating our own database, for that the command to be executed is,

CREATE DATABASE dbname;

Where, dbname will be the name of our database.

The “CREATE DATABASE” message is displayed once the database is created successfully.

The ‘\l’ or ‘\list’ command will display the list of all the databases that are present in the system.

This command will list all the databases that are present and we can confirm the creation of our database by running this command and observing the output.

To use a particular database from the available databases, we have to use the ‘\connect’ or ‘\c’ command.

After the successful selection of a particular database, the message is displayed.

So, with the help of this article, we are able to learn what is PostgreSQL and its major features and how to install PostgreSQL.