How to Remove Postgres From my Installation on Ubuntu?

PostgreSQL is the open-source database management tool that is well known for its security flexibility and reliability. Postgres is equipped with a large set of dependencies and packages that are embedded at the time of installation. Whenever the uninstallation of Postgres is required, it is recommended to completely remove the Postgres alongside its dependencies and configurations. 

This post will address the step-by-step procedure to remove Postgres from the Ubuntu system.

How to Completely Remove Postgres From Ubuntu?

To remove PostgreSQL from your system, go through the following steps:

Step 1: List the Postgres Packages 

When the user installs Postgres, its multiple packages are installed in the operating system.  List down the Debian PostgreSQL package list via the following command: 

$ dpkg -l | grep postgres

Step 2: Remove All Listed Packages

Remove all the above packages from your computer, you can uninstall them one by one or in one command:

$ sudo apt --purge remove postgresql postgresql-15 postgresql-client-15 postgresql-client-common postgresql-common postgresql-contrib

Verify the packages list again, there will be no package to display:

$ dpkg -l | grep postgres

Step 3: Remove Directories

The next step is to remove the directories of PostgreSQL, use the following “rm” command to remove the directories of PostgreSQL. As these directories are not part of the installation, it is created after the installation by calling “initdb” (initializing database):

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

The directories have been removed.

Step 4: Delete the Postgres User

The final step is to remove the postgres user as well, for doing this, run the following command:

$ sudo deluser postgres

The postgres user has been deleted.

Remove Postgres on Fedora/RHEL/CentOS

If you are using other Linux distributions such as Fedora, RHEL, and CentOS, use the below-mentioned commands to uninstall PostgreSQ:.

$ yum remove postgresql
$ yum remove postgres\*
$ rm /var/lib/pgsql$ yum list installed | grep postgres$ yum remove [PostgreSQL_Packge Name]

Note: The above commands need sudo(root) privileges in order to uninstall the Postgres.

Conclusion

To remove the PostgreSQL from Ubuntu, list the packages and remove them from the system. After that remove the directories and PostgreSQL users as well. All these processes are demonstrated in a step-by-step procedure in this guide.