How to Uninstall PostgreSQL on CentOS?

The purpose of uninstalling PostgreSQL on CentOS is to remove the PostgreSQL software and all associated components from the system. There could be several reasons to uninstall PostgreSQL, such as upgrading or downgrading with different versions, reinstalling, releasing disk space, and many more.

This article will teach the step-by-step procedure to uninstall PostgreSQL from CentOS.

- How to Uninstall PostgreSQL on CentOS?
- Stop the PostgreSQL Service
- Remove the PostgreSQL Package
- Remove the PostgreSQL Data Directory
- Remove the PostgreSQL User

How to Uninstall PostgreSQL on CentOS?

Uninstalling PostgreSQL on CentOS refers to the process of removing PostgreSQL software and all associated components, such as the data directory, configuration files, and user accounts, from a CentOS system.

To uninstall PostgreSQL on CentOS, follow the below steps:

Step 1: Stop the PostgreSQL Service

Before users uninstall PostgreSQL, users need to stop the PostgreSQL service. For this, run the following command:

$ systemctl stop postgresql
img

Step 2: Remove the PostgreSQL Package

Once the service has stopped, remove the PostgreSQL package using the “yum” package manager. To do this, run the following command:

$ sudo yum remove postgresql-server postgresql-contrib
img

img

This command removes the PostgreSQL server and the contrib package.

Step 3: Remove the PostgreSQL Data Directory

After removing the package, users need to remove the PostgreSQL data directory. By default, the data directory is located at “/var/lib/pgsql/data”. To remove this directory and its contents execute the following command:

$ sudo rm -rf /var/lib/pgsql/data
img

Step 4: Remove the PostgreSQL User

Finally, users need to remove the PostgreSQL user. For this, run the below command as the root user:

# userdel postgres
img

This command removes the PostgreSQL user from the system.

Conclusion

To uninstall PostgreSQL from CentOS, execute the “yum remove postgresql-server postgresql-contrib” command. It removes all the associated packages with the dependencies. Users can also remove the PostgreSQL data directory by running the “sudo rm -rf /var/lib/pgsql/data” command in the terminal. This article has presented step-by-step guidelines to uninstall PostgreSQL from CentOS.