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](/media/images/image_eH7BpP7.width-1200.png)
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](/media/images/image_JM2OlIM.width-1200.png)
Type “Y” and hit the “Enter” button to continue the installation:
![img](/media/images/image_Y42U5zc.width-1200.png)
It will take some time to complete the installation process:
![img](/media/images/image_lX7rctq.width-1200.png)
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](/media/images/image_QaF219c.width-1200.png)
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](/media/images/image_X1vEK3q.width-1200.png)
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](/media/images/image_UbXHMC2.width-1200.png)
Step 3: List Databases
Now, execute the “\l” command to see the list of available databases:
\l
![img](/media/images/image_wxH9HXf.width-1200.png)
Step 4: Create New Database
Let’s create a new database named “example_db” using the following command:
CREATE DATABASE example_db;
![img](/media/images/image_NQr60Z1.width-1200.png)
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](/media/images/image_Vg18N8W.width-1200.png)
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](/media/images/image_dR2xBd1.width-1200.png)
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](/media/images/image_SGI5QMx.width-1200.png)
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](/media/images/image_oyJxQrK.width-1200.png)
Type “y” and hit the “Enter” button to remove Postgres from Ubuntu:
![img](/media/images/image_LZ4lJZ3.width-1200.png)
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.