Tag Archives: how to remove postgresql and delete data directory

Uninstall PostgreSQL 17 on Linux (RPM Method)

Uninstall PostgreSQL 17 on Linux (Installed Using RPM Method)

Table of Contents



0. Important Caution Before Proceeding

** Before uninstalling PostgreSQL PostgreSQL 17, first take a logical or physical backup. **

If PostgreSQL was installed using RPM packages, removing the package does not automatically remove:

Database files
WAL files
Backup directories
Custom configuration files

1. Check Installed PostgreSQL RPM Packages

[root@pgdb02 ~]# rpm -qa | grep postgres
postgresql17-libs-17.6-1PGDG.rhel9.x86_64
postgresql17-17.6-1PGDG.rhel9.x86_64
postgresql17-server-17.6-1PGDG.rhel9.x86_64
postgresql17-contrib-17.6-1PGDG.rhel9.x86_64
[root@pgdb02 ~]#

2. Stop PostgreSQL Service

[root@pgdb02 ~]# systemctl stop postgresql-17
[root@pgdb02 ~]# systemctl status postgresql-17
○ postgresql-17.service - PostgreSQL 17 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-17.service; enabled; preset: disabled)
Active: inactive (dead)
Docs: https://www.postgresql.org/docs/17/static/
[root@pgdb02 ~]#

3. Disable PostgreSQL Service

[root@pgdb02 ~]# systemctl disable postgresql-17
Removed "/etc/systemd/system/multi-user.target.wants/postgresql-17.service".
[root@pgdb02 ~]#

4. Remove and Verify PostgreSQL RPM Packages

WARNING: This permanently deletes all binaries, libraries, and data. Ensure your data is backed up before this step.

[root@pgdb02 ~]# rpm -qa | grep postgres
postgresql17-libs-17.6-1PGDG.rhel9.x86_64
postgresql17-17.6-1PGDG.rhel9.x86_64
postgresql17-server-17.6-1PGDG.rhel9.x86_64
postgresql17-contrib-17.6-1PGDG.rhel9.x86_64
[root@pgdb02 ~]#
[root@pgdb02 ~]# rpm -e postgresql17-contrib-17.6-1PGDG.rhel9.x86_64
[root@pgdb02 ~]# rpm -e postgresql17-server-17.6-1PGDG.rhel9.x86_64
[root@pgdb02 ~]# rpm -e postgresql17-17.6-1PGDG.rhel9.x86_64
[root@pgdb02 ~]# rpm -e postgresql17-libs-17.6-1PGDG.rhel9.x86_64
[root@pgdb02 ~]#

5. Verify Packages Are Removed

[root@pgdb02 ~]# rpm -qa | grep postgres
[root@pgdb02 ~]#

6. Remove PostgreSQL System User (Optional)

[root@pgdb02 ~]# id postgres
uid=26(postgres) gid=26(postgres) groups=26(postgres)
[root@pgdb02 ~]# userdel postgres
[root@pgdb02 ~]#
[root@pgdb02 ~]# groupdel postgres
groupdel: group 'postgres' does not exist
[root@pgdb02 ~]#
[root@pgdb02 ~]# id postgres
id: ‘postgres’: no such user
[root@pgdb02 ~]#

7. Remove PostgreSQL Configuration Files

# It's already removed in step no. 3

[root@pgdb02 ~]# ls -l /usr/lib/systemd/system/postgresql-17.service
ls: cannot access '/usr/lib/systemd/system/postgresql-17.service': No such file or directory
[root@pgdb02 ~]# 
[root@pgdb02 ~]# ls -l /etc/systemd/system/postgresql-17.service
-rw-r--r--. 1 root root 588 Jun 19 21:20 /etc/systemd/system/postgresql-17.service
[root@pgdb02 ~]#
[root@pgdb02 ~]# rm -f /etc/systemd/system/postgresql-17.service
[root@pgdb02 ~]#

8. Verify Complete Removal

[root@pgdb02 ~]# id postgres
id: ‘postgres’: no such user
[root@pgdb02 ~]#
[root@pgdb02 ~]# rpm -qa | grep postgres
[root@pgdb02 ~]#
[root@pgdb02 ~]# systemctl status postgresql-17
Unit postgresql-17.service could not be found.
[root@pgdb02 ~]#

Caution: Your use of any information or materials on this website is entirely at your own risk. It is provided for educational purposes only. It has been tested internally, however, we do not guarantee that it will work for you. Ensure that you run it in your test environment before using.

Thank you,
Rajasekhar Amudala
Email: br8dba@gmail.com
Linkedin: https://www.linkedin.com/in/rajasekhar-amudala/