How to Install PostgreSQL Database on Ubuntu

One of the most popular open-source relational databases is PostgreSQL which offers numerous features such as security, reliability, extensibility, etc. Postgres assists the developers in building the applications, administrators in protecting data integrity, builts fault tolerance environments, etc. It allows users to store immense and sophisticated data securely. Postgres is becoming increasingly popular due to its enormous features. However, it must be installed on your system in order to access any of its functionality/features.

This blog will teach you how to install, use and uninstall the Postgres database from Ubuntu. So, let's begin.

How to Install PostgreSQL Database on Ubuntu?

Firstly, open the terminal and perform the following steps to install Postgres on Ubuntu.

Step1: Update Packages

Let’s update the system packages via the below command:

sudo apt update
img

The above snippet shows that the system packages are being updated.

Step 2: Install Postgres

To start Postgres installation on ubuntu, run the below “sudo apt install” command:

sudo apt install postgresql postgresql-contrib
img

Type “Y” and hit the “Enter” button to continue the installation:

img

It will take some time to complete the installation process:

img

Finally, Postgres has been installed on our system successfully.

Step 3: Start the PostgreSQL Services

Once Postgres is installed on your ubuntu operating system, execute the below command to start the Postgres Services:

sudo systemctl start postgresql.service
img

Specify the password and hit the “enter” button to start the Postgres services.

Getting Started With Postgres

Installing Postgres will automatically install the SQL Shell on your system. So, you can perform any Postgres task via SQL Shell.

Step 1: Switch to Postgres Account

Firstly, you need to switch to the Postgres account. To do so, run the below command:

sudo -i -u postgres
img

Specify the password and hit the “Enter” button to switch the account.

Step 2: Switch to SQL Shell

To use any Postgres query/command, you need to switch to SQL Shell(psql).

psql
img

Step 3: List Databases

Now, execute the “\l” command to see the list of available databases:

\l
img

Step 4: Create New Database

Let’s create a new database named “example_db” using the following command:

CREATE DATABASE example_db;
img

The output indicates that a database named “example_db” has been created.

Step 5: Verify Database Creation

You can verify the database’s creation through the following command:

\l
img

The database list proves that the database “example_db” has been created.

Step 6: Access New Database

To establish a connection with the newly created database, you can utilize the “\c” command as follows:

\c example_db
img

The output verifies that the connection has been established with the new database successfully.

This way, you can install Postgres and perform all the Postgres tasks on ubuntu.

How to Uninstall Postgres From Ubuntu?

Uninstalling Postgres from Ubuntu can be done using various ways. We are going to discuss one of them in this write-up.

Step 1: Uninstall Postgres

To uninstall Postgres from your Ubuntu operating system, open the terminal and run the command below:

sudo apt remove postgresql postgresql-contrib
img

The above snippet demonstrates that Postgres has been removed from Ubuntu.

Step 2: Uninstall Dependent Packages

Some additional(dependent) packages are automatically installed on Ubuntu when you install Postgres. However, when you remove Postgres from ubuntu, these dependent/additional packages are no longer needed. So, to uninstall these dependent packages, the below-mentioned command is used in ubuntu:

sudo apt autoremove
img

Type “y” and hit the “Enter” button to remove Postgres from Ubuntu:

img

The whole process will take some time to remove Postgres and its dependent packages completely from ubuntu.

That's all from this Postgres guide!

Conclusion

To install Postgres on your Ubuntu operating system, run the “sudo apt install postgresql postgresql -contrib” command. Installing Postgres will automatically install the SQL Shell on your system. So, use the SQL Shell to perform any Postgres task. This blog post explained how to install, use, and uninstall the Postgres database on Ubuntu via practical demonstration.