How to Uninstall PostgreSQL From Ubuntu

PostgreSQL is among the most popular open-source relational databases that provide a wide range of advantages, including security, stability, extensibility, etc. Users can safely store vast amount of complex data using Postgres. However, when PostgreSQL is no longer needed, you can uninstall it from your Ubuntu operating system.

This blog post will demonstrate how to uninstall Postgres completely from the ubuntu operating system. So, let’s get started!

How to Uninstall Postgres From Ubuntu?

PostgreSQL may need to be uninstalled from a system at some point for various reasons, such as cleaning up the disk space, not needing it anymore, etc. So it's crucial to understand how to uninstall the PostgreSQL database completely(along with its dependent packages) from your system.

Uninstalling Postgres from Ubuntu can be done using various ways. In this write-up, we are going to discuss a couple of them!

Contact us today for all your Postgres and Open Source consulting and support needs.

Method 1: Using ‘sudo apt remove postgresql’

This section describes the simplest way of uninstalling Postgres from ubuntu. For that purpose, follow the steps provided below:

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.

Method 2: Using ‘purge remove postgresql’

If the above method is not working for some reasons, then nothing to worry about! We have explained another very convenient method to uninstall Postgres from your Ubuntu operating system.

Step 1: List Postgres Packages

Run the below-provided command to see the list of Postgres packages currently installed on your ubuntu operating system:

dpkg -l |grep postgres;
img

The above snippet shows the list of Postgres packages installed on our machine.

Step 2: Uninstall Postgres

Remove Postgres from your system by running the remove purge command followed by the name of all the packages related to Postgres:

sudo apt-get –purge remove postgresql postgresql-14 postgresql-client-common postgresql-common postgresql-contrib
img

Type “y” and hit the “Enter” button to continue the uninstallation process of Postgres:

img

The following window will appear during the uninstallation, asking you to remove Postgres directories when the package is purged:

img

Hit the “Yes” button, and it will take a few minutes to complete the uninstallation.

Step 3: Verify Uninstallation

Let’s execute the below statement one more time to verify the uninstallation of Postgres from ubuntu:

dpkg -l | grep postgres
img

The above snippet proves that postgres has been uninstalled completely from Ubuntu.

That's all from this Postgres guide!

Conclusion

To uninstall Postgres from your Ubuntu operating system, open the terminal and run the “sudo apt remove postgresql postgresql-contrib” command. To uninstall the dependent packages from the ubuntu operating system, use the “sudo apt autoremove” command. Another way of removing postgres from ubuntu is by using the “sudo apt-get –purge remove” command followed by the name of packages to be removed. Through practical demonstration, this Postgres blog has explained how to uninstall PostgreSQL from the ubuntu operating system.