How to Uninstall PostgreSQL On Ubuntu
In some cases, on a single server, we have deployed multiple databases. here suppose Mysql and Postgresql database. After some days we have decided to separate both databases because of some performance issues. So, we have deployed two Azure Vm and separated both databases into individual VM.
But in this situation, we need to clean postgresql completely in one server. because we have deployed VM from Snapshot of an existing server in which we have both databases MySQL and postgresql installed.
So let see how we can Remove/Uninstall Postgresql database on the Ubuntu server.
Step 1 Stop the PostgreSQL service
Before going to uninstall we are going to stop the service of PostgreSQL using below command.
# systemctl stop postgresql ## check status # systemctl status postgresql
Step 2 Remove the PostgreSQL packages
Once the postgresql service stopped we will remove the postgresql package from the system.
# apt-get remove postgresql
root@LinuxFork:~# apt-get remove postgresql Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: grub-pc-bin linux-headers-4.15.0-106 Use 'sudo apt autoremove' to remove them. The following packages will be REMOVED: postgresql 0 upgraded, 0 newly installed, 1 to remove and 27 not upgraded. After this operation, 63.5 kB disk space will be freed. Do you want to continue? [Y/n] y (Reading database ... 77416 files and directories currently installed.) Removing postgresql (10+190ubuntu0.1) ... root@LinuxFork:~#
Now, we will perform below command to remove all the configuration files and other dependent packages because the above command can only remove the package.
# apt-get purge postgresql*
Step 3 Remove data Directory of PostgreSQL
By default storing all the data in /var/log/postgresql/ directory. we will delete it using the below command.
# rm -rf /var/lib/postgresql
Step 4 Remove Conf and log file
Now, we need to remove conf and log file of PostgreSQL using below command.
# rm -rf /etc/postgresql/ # rm -rf /var/log/postgresql/
Step 5 Remove Postgres User
Now, we need to remove the postgres user from the system because the postgres user not removed by uninstalling it.
# userdel -r postgres
root@LinuxFork:~# userdel -r postgres userdel: postgres mail spool (/var/mail/postgres) not found userdel: postgres home directory (/var/lib/postgresql) not found root@LinuxFork:~#
That’s all. In this post we have learned how to remove PostgreSQL from the Ubuntu system, if you are facing any issue please use our comment section.
I am a professional Cloud Engineer and founder of LinuxFork. I am working on Infrastructure Deployment and focusing on the integration of open source technologies with the Cloud. I am an avid Linux lover and open source enthusiast. mostly, I am working with Redhat and Ubuntu Linux in the production environment. apart from this I always believe in knowledge sharing.