Occasionally, during software installation, interruptions or incomplete downloads can occur, leaving you with partial files and empty directories. In such cases, you may need to completely uninstall recently installed software. This tutorial provides a step-by-step guide on safely removing PostgreSQL from an Ubuntu 16.04 server.

Important: Backup First

Before initiating the uninstallation process, it is crucial to back up any databases you may need to prevent data loss.

How to Remove PostgreSQL

  1. Step 1: List the PostgreSQL Packages

    Use the dpkg tool to list the packages associated with the PostgreSQL setup:

    dpkg -l | grep postgres

    This command will display all PostgreSQL-related packages installed on your system.

  2. Step 2: Uninstall PostgreSQL Packages

    Building on the package list obtained in Step 1, you can efficiently remove all associated software packages with a single command. Utilize the apt-get --purge remove command followed by the names of each identified package separated by a space.

    For example, if you have the following installed packages:

    • pgdg-keyring
    • postgresql-10
    • postgresql-client-10
    • postgresql-client-common
    • postgresql-common

    The purge remove command would appear as follows:

    sudo apt-get --purge remove pgdg-keyring postgresql-10 postgresql-client-10 postgresql-client-common postgresql-common
  3. Step 3: Verify PostgreSQL Deletion

    After executing the removal command, attempting to access the PostgreSQL environment should be unsuccessful. Confirm this by rerunning the dpkg -l | grep postgres command and searching for 'postgres.'

    If the uninstallation was successful, there should be no output from the command, indicating that PostgreSQL has been completely removed.