How to uninstall PostgreSQL from Ubuntu

In this article, we will see how to uninstall PostgreSQL from Ubuntu.

Important Note: I suggest you to take backup of your important PostgreSQL databases. Once we uninstall, all your PostgreSQL data will be lost.

1. Stop PostgreSQL server using below command.

sudo systemctl stop postgresql

2. Lets verify PostgreSQL packages by using dpkg.

dpkg -l | grep postgres

How to uninstall PostgreSQL from Ubuntu

3. Remove all PostgreSQL related packages completely using below command.

sudo apt-get --purge remove postgres*

Press Y to continue.

Use tab and select Yes then press Enter.

4. Use the rm command with the -rf options to recursively remove all of the directories and data for the postgresql packages:

sudo rm -rf /var/lib/postgresql/ sudo rm -rf /etc/postgresql/ sudo rm -rf /var/log/postgresql/

5. After complete rm commands, execute the dpkg -l | grep postgres command one more time to verify that all of the packages have been removed.

dpkg -l | grep postgres

So, we have successfully uninstalled PostgreSQL from Ubuntu Linux machine.