164

I have ubuntu 9.10(karmic), and when I attempted to install a new program, the postgres was installed as a dependency of that program, no problem at all if the installation occurs with success, but there was an error, and the Postgres was not installed and the application is not working. I had tryed to update the Postgres and nothing, only the message "there was an error and your postgres can not be installed"

Now I want to remove completely the Postgres from my machine, how can I do that, I do not want to kill the process every boot. I just want to remove Postgres.

What is the command line?

Thanks people.

5 Answers 5

282

The simplest way to do this is to open a terminal and type:

sudo apt-get --purge remove postgresql postgresql-*

This will also prompt you to remove that software that depends on Postgres, which in this case it appears you would like to do.
I do not personally run 9.10 or Postgres, so it is possible that Postgres installs itself in several parts. In that case, a simple:

dpkg -l | grep postgres

Will get you the list of those packages that Postgres installed. Then, just use the same "apt-get --purge remove ...." command but instead of just postgresql, type each package name, separated by spaces, like:

sudo apt-get --purge remove postgresql postgresql-doc postgresql-common

This is dependent on the list of packages installed, of course.

5
  • 1
    I run the command line --> sudo apt-get --purge remove postgresql and looks like it was removed from my ubuntu.. I'm waiting the update, always on update postgres show up the error. thanks anyway. Apr 8, 2011 at 23:34
  • I thought it was right. But today I got the same warning again, a crash system, something try to update the postgres, but it not happen, then the crash report has showed itself.. some suggestions? thanks again. Apr 12, 2011 at 18:00
  • 4
    It should be dpkg -l | grep postgres Mar 8, 2012 at 13:53
  • you can also use sudo apt-get --purge remove postgresql postgresql-* Nov 15, 2021 at 12:50
  • You just saved my life! Jan 24 at 22:50
164

Steps that worked for me on Ubuntu 8.04.2 to remove postgres 8.3

  1. List All Postgres related packages

    dpkg -l | grep postgres
    
    ii  postgresql                            8.3.17-0ubuntu0.8.04.1           object-relational SQL database (latest versi
    ii  postgresql-8.3                        8.3.9-0ubuntu8.04                object-relational SQL database, version 8.3
    ii  postgresql-client                     8.3.9-0ubuntu8.04                front-end programs for PostgreSQL (latest ve
    ii  postgresql-client-8.3                 8.3.9-0ubuntu8.04                front-end programs for PostgreSQL 8.3
    ii  postgresql-client-common              87ubuntu2                        manager for multiple PostgreSQL client versi
    ii  postgresql-common                     87ubuntu2                        PostgreSQL database-cluster manager
    ii  postgresql-contrib                    8.3.9-0ubuntu8.04                additional facilities for PostgreSQL (latest
    ii  postgresql-contrib-8.3                8.3.9-0ubuntu8.04                additional facilities for PostgreSQL
    
  2. Remove all above listed

    sudo apt-get --purge remove postgresql postgresql-8.3  postgresql-client  postgresql-client-8.3 postgresql-client-common postgresql-common  postgresql-contrib postgresql-contrib-8.3
    
  3. Remove the following folders

    sudo rm -rf /var/lib/postgresql/
    sudo rm -rf /var/log/postgresql/
    sudo rm -rf /etc/postgresql/
    
  4. Remove the postgres user:

    sudo deluser postgres
    
4
  • Thanks! but I'm not using ubuntu anymore. Now I'm on opensuse and centos. Thanks anyway, if I have troubles again with postgres your answer will be useful. Mar 8, 2012 at 16:22
  • 4
    Step 3 can be combined into one line if using bash shell sudo rm -rf {/var/{lib,log},etc}/postgresql/
    – smac89
    Oct 5, 2017 at 16:22
  • 1
    +1 for step 3 here. That was key for me. Tried removing the packages several times but there was cruft left behind in /var/lib/postgresql and /etc/postgresql that was causing subsequent operations after reinstall to fail. Jan 29, 2018 at 23:14
  • you can also use sudo apt-get --purge remove postgresql postgresql-* Nov 15, 2021 at 12:52
56

One command to completely remove postgresql in terminal is sudo apt-get --purge remove postgresql\*. Please note that this command will remove postgresql and all it's compenents.

4
  • 10
    I used sudo apt-get purge postgresql* on 14.04 Aug 6, 2016 at 22:39
  • This should be the accepted answer! Apr 29, 2019 at 16:28
  • Answer above worked for me - This didn't remove everything as --purge states
    – Ricky
    Oct 8, 2019 at 5:57
  • Thanks! This works for me! Jun 28, 2021 at 5:57
19

Follow the commands:

  • sudo apt-get --purge remove postgresql

List all postgres related packages:

  • dpkg -l | grep postgres

remove all the above listed packages using the command :

  • apt-get --purge remove package1 package2 ..

Confirm all the files and folders related to postgres/postgresql are deleted using the command :

  • whereis postgres
  • whereis postgresql

Remove all the files and folders listed using rm command.

Delete the user postgres using the command :

  • userdel -f postgres

happy coding :)

1
  • Removing the postgres user is a good extra step.
    – ndemarco
    Jun 10, 2020 at 15:42
1

Thanks to Code Friendly, & I want to share how I solved my problem.

When I updated postgresql from the Synaptic application, it is the postgresql 10 version that was installed. So I had two versions of postgresql (10 & 9.6) installed in my debian machine. Postgresql 9.6 listening on port 5432. Postgresql 10 listening on port 5433 instead of 5432.

When I run the comand msfconsole in a terminal, although metasploit connects to the msf database on port 5432, the following error message appears:


root@kali:~# msfconsole
[-] Failed to connect to the database: could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?

I unistall the 9.6 postgresql version from the Synaptic application, but the problem persists (postgresql 9.6 no longer appears in synaptic's installed software list);

But by executing the command that follows we see that the version postgresql 9.6 was not completely unistalled:


root@kali:~# dpkg -l | grep postgresql 
ii  postgresql                     10+187             all          object-relational SQL database (supported version)
ii  postgresql-9.6                 **                 all          object-relational SQL database, version 9.6 server
ii  postgresql-10                  10.0-1+b1          amd64        object-relational SQL database, version 10 server
ii  postgresql-client-10           10.0-1+b1          amd64        front-end programs for PostgreSQL 10
ii  postgresql-client-common       187                all          manager for multiple PostgreSQL client versions
ii  postgresql-common              187                all          PostgreSQL database cluster manager
ii  postgresql-contrib             10+187             all          additional facilities for PostgreSQL (supported version)

With the following command, postgresql-9.6 uninstalled completely:
root@kali:~# sudo apt-get --purge remove postgresql-9.6

Then I edited the file /etc/postgresql/10/main/postgresql.conf, changed the port number to 5432 and the problem is solved.


Another error message appears:

Creating initial database schema /usr/local/bin/bundle:22:in ``load': cannot load such file -- /usr/lib/ruby/exe/bundle (LoadError)     from /usr/local/bin/bundle:22:in `<main>

I found that the cause was that two (2) versions of Ruby are installed in my debian machine, versions (2.2 & 2.3).

Same thing, I uninstall the ruby 2.2 version from the synaptic application and the problem is solved, metasploit starts without problems.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .