How to completely remove PostgreSQL Installation on Ubuntu Linux

Posted on

This is an article used where the subject of the writing is clearly specified in the title. There is a special needs for removing the old PostgreSQL database server instalation so that it can be replaced with the new one. Below are the steps taken in order to accomplish the task :

  1. Execute the following command :
apt-get remove postgresql*

The ‘*’ sign will automatically add any pattern which has the string of ‘postgresql’ followed with any characters. Below is the actual command execution :

root@hostname:/tmp# apt-get remove postgresql*
Reading package lists... Done
Building dependency tree       
Reading state information... Done
...
Note, selecting 'postgresql-server-dev-9.1' for glob 'postgresql*'
Note, selecting 'postgresql-server-dev-9.2' for glob 'postgresql*'
Note, selecting 'postgresql-server-dev-9.3' for glob 'postgresql*'
Note, selecting 'postgresql-server-dev-9.4' for glob 'postgresql*'
Note, selecting 'postgresql-server-dev-9.5' for glob 'postgresql*'
Note, selecting 'postgresql-server-dev-9.6' for glob 'postgresql*'
...
Package 'postgresql-server' is not installed, so not removed
...
Package 'postgresql-all' is not installed, so not removed
Package 'postgresql-client' is not installed, so not removed
Package 'postgresql-client-10' is not installed, so not removed
Package 'postgresql-client-9.1' is not installed, so not removed
Package 'postgresql-client-9.2' is not installed, so not removed
Package 'postgresql-client-9.3' is not installed, so not removed
Package 'postgresql-client-9.4' is not installed, so not removed
Package 'postgresql-client-9.6' is not installed, so not removed
...
Package 'postgresql-server-dev-10' is not installed, so not removed
Package 'postgresql-server-dev-9.1' is not installed, so not removed
Package 'postgresql-server-dev-9.2' is not installed, so not removed
Package 'postgresql-server-dev-9.3' is not installed, so not removed
Package 'postgresql-server-dev-9.4' is not installed, so not removed
Package 'postgresql-server-dev-9.5' is not installed, so not removed
Package 'postgresql-server-dev-9.6' is not installed, so not removed
Package 'postgresql-server-dev-all' is not installed, so not removed
The following packages will be REMOVED:
  postgresql postgresql-9.5 postgresql-client-9.5 postgresql-client-common postgresql-common postgresql-contrib postgresql-contrib-9.5
0 upgraded, 0 newly installed, 7 to remove and 21 not upgraded.
After this operation, 18,4 MB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 404291 files and directories currently installed.)
Removing postgresql (9.5+173ubuntu0.1) ...
Removing postgresql-contrib (9.5+173ubuntu0.1) ...
Removing postgresql-contrib-9.5 (9.5.12-0ubuntu0.16.04) ...
update-alternatives: warning: forcing reinstallation of alternative /usr/share/postgresql/9.5/man/man1/postmaster.1.gz because link group postmaster.1.gz is broken
Removing postgresql-9.5 (9.5.12-0ubuntu0.16.04) ...
Removing postgresql-client-9.5 (9.5.12-0ubuntu0.16.04) ...
Removing postgresql-common (173ubuntu0.1) ...
Removing 'diversion of /usr/bin/pg_config to /usr/bin/pg_config.libpq-dev by postgresql-common'
Removing postgresql-client-common (173ubuntu0.1) ...
Processing triggers for man-db (2.7.5-1) ...
root@hostname:/tmp# ls

This is another one which is executed to make sure all of the associated packages are fully removed :

root@hostname:/tmp# apt-get --purge remove postgresql postgresql-doc postgresql-common
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package 'postgresql' is not installed, so not removed
Package 'postgresql-doc' is not installed, so not removed
The following packages will be REMOVED:
  postgresql-common*
0 upgraded, 0 newly installed, 1 to remove and 21 not upgraded.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] Y
(Reading database ... 403631 files and directories currently installed.)
Removing postgresql-common (173ubuntu0.1) ...
Purging configuration files for postgresql-common (173ubuntu0.1) ...
dpkg: warning: while removing postgresql-common, directory '/var/log/postgresql' not empty so not removed
dpkg: warning: while removing postgresql-common, directory '/var/lib/postgresql' not empty so not removed
dpkg: warning: while removing postgresql-common, directory '/etc/postgresql' not empty so not removed
root@hostname:/tmp# dpkg -l | grep postgres
rc  postgresql-9.5                                9.5.12-0ubuntu0.16.04                        amd64        object-relational SQL database, version 9.5 server
rc  postgresql-client-common                      173ubuntu0.1                                 all          manager for multiple PostgreSQL client versions
root@hostname:/~#

2. This is another step for the command to be executed :

root@hostname:/~# apt-get --purge remove postgresql-9.5 postgresql-client-common
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  postgresql-9.5* postgresql-client-common*
0 upgraded, 0 newly installed, 2 to remove and 21 not upgraded.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] Y
(Reading database ... 403625 files and directories currently installed.)
Removing postgresql-9.5 (9.5.12-0ubuntu0.16.04) ...
Purging configuration files for postgresql-9.5 (9.5.12-0ubuntu0.16.04) ...
Dropping cluster main...
Removing postgresql-client-common (173ubuntu0.1) ...
Purging configuration files for postgresql-client-common (173ubuntu0.1) ...
root@hostname:~#
  1. Remove the remaining previous installation files which is the library files in the /var/lib/postgresql  :
root@hostname:~# rm -rf /var/lib/postgresql/
  1. Remove the remaining previous installation files which is the log files in the /var/log/postgresl :
root@hostname:/tmp# rm -rf /var/log/postgresql/
  1. Remove the remaining previous installation files which is located in /etc/postgresql mainly contains configuration files :
root@hostname:/tmp# rm -rf /etc/postgresql/

Leave a Reply