How to thoroughly purge and reinstall postgresql on ubuntu?

243,394

Solution 1

Option A

If your install isn't already damaged, you can drop unwanted PostgreSQL servers ("clusters") using pg_dropcluster. Use that in preference to a full purge and reinstall if you just want to restart with a fresh PostgreSQL instance.

$ pg_lsclusters
Ver Cluster Port Status Owner    Data directory              Log file
11  main    5432 online postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11-main.log
$ sudo systemctl stop postgresql@11-main
$ sudo pg_dropcluster --stop 11 main
$ sudo pg_createcluster --start 11 main

Option B

If you really need to do a full purge and reinstall, first make sure PostgreSQL isn't running. ps -C postgres should show no results.

Now run:

apt-get --purge remove postgresql\*

to remove everything PostgreSQL from your system. Just purging the postgres package isn't enough since it's just an empty meta-package.

Once all PostgreSQL packages have been removed, run:

rm -r /etc/postgresql/
rm -r /etc/postgresql-common/
rm -r /var/lib/postgresql/
userdel -r postgres
groupdel postgres

You should now be able to:

apt-get install postgresql

or for a complete install:

apt-get install postgresql-8.4 postgresql-contrib-8.4 postgresql-doc-8.4

Solution 2

I had a similar situation: I needed to purge postgresql 9.1 on a debian wheezy ( I had previously migrated from 8.4 and I was getting errors ).

What I did:

First, I deleted config and database

$ sudo pg_dropcluster --stop 9.1 main

Then removed postgresql

$ sudo apt-get remove --purge postgresql postgresql-9.1 

and then reinstalled

$ sudo apt-get install postgresql postgresql-9.1

In my case I noticed /etc/postgresql/9.1 was empty, and running service postgresql start returned nothing

So, after more googling I got to this command:

$ sudo pg_createcluster 9.1 main

With that I could start the server, but now I was getting log-related errors. After more searching, I ended up changing permissions to the /var/log/postgresql directory

$ sudo chown root.postgres /var/log/postgresql
$ sudo chmod g+wx /var/log/postgresql

That fixed the issue, Hope this helps

Solution 3

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/
    

Solution 4

apt-get purge postgresql*

is enough.

Solution 5

I know an answer has already been provided, but dselect didn't work for me. Here is what worked to find the packages to remove:

# search postgr  | grep ^i
i   postgresql                      - object-relational SQL database (supported 
i A postgresql-8.4                  - object-relational SQL database, version 8.
i A postgresql-client-8.4           - front-end programs for PostgreSQL 8.4     
i A postgresql-client-common        - manager for multiple PostgreSQL client ver
i A postgresql-common               - PostgreSQL database-cluster manager       

# aptitude purge postgresql-8.4 postgresql-client-8.4 postgresql-client-common postgresql-common postgresql

rm -r /etc/postgresql/
rm -r /etc/postgresql-common/
rm -r /var/lib/postgresql/

Finally, editing /etc/passwd and /etc/group

Share:
243,394

Related videos on Youtube

John Mee
Author by

John Mee

I am mee, and no-one else.

Updated on December 28, 2020

Comments

  • John Mee
    John Mee over 3 years

    Somehow I've managed to completely bugger the install of postgresql on Ubuntu karmic. I want to start over from scratch, but when I "purge" the package with apt-get it still leaves traces behind such that the reinstall configuration doesn't run properly.

    After I've done:

    apt-get purge postgresql
    apt-get install postgresql
    

    It said

    Setting up postgresql-8.4 (8.4.3-0ubuntu9.10.1) ...
    Configuring already existing cluster (configuration: /etc/postgresql/8.4/main, data: /var/lib/postgresql/8.4/main, owner: 108:112)
    Error: move_conffile: required configuration file     /var/lib/postgresql/8.4/main/postgresql.conf does not exist
    Error: could not create default cluster. Please create it manually with
    
      pg_createcluster 8.4 main --start
    
    or a similar command (see 'man pg_createcluster').
    update-alternatives: using /usr/share/postgresql/8.4/man/man1/postmaster.1.gz to provide /usr/share/man/man1/postmaster.1.gz (postmaster.1.gz) in auto mode.
    
    Setting up postgresql (8.4.3-0ubuntu9.10.1) ...
    

    I have a "/etc/postgresql" with nothing in it and "/etc/postgresql-common/" has a 'pg_upgradecluser.d' directory and root.crt and user_clusters files.

    The /etc/passwd has a postgres user; the purge script doesn't appear to touch it. There's been a bunch of symptoms which I work through only to expose the next.

    Right this second, when I run that command "pg_createcluster..." it complains that '/var/lib/postgresql/8.4/main/postgresql.conf does not exist', so I'll go find one of those but I'm sure that won't be the end of it.

    Is there not some easy one-liner (or two) which will burn it completely and let me start over?

    • Alexandre Leites
      Alexandre Leites about 14 years
      Try one-click installer, it's more simple, its directories are not scattered in /etc/ and /var. They are all stored in one directory
    • Andrew
      Andrew about 5 years
      This is not a general computing question, and postgresql is standard stackoverflow topic. Question should be reopened.
  • John Mee
    John Mee over 12 years
    In deleting the line you removed the entire "ssl-cert" group, which could create havoc with ssl. Instead, to remove postgres from the ssl-cert group, delete "postgres" from after the colon.
  • Evgeny
    Evgeny over 12 years
    These instructions worked on debian squeeze/sid
  • Nick
    Nick almost 12 years
    Removing ALL Postgres related packages worked for me. I had some 8.4 & 9.1 packages mixed up. That combined with removing everything allowed me to finally reinstall 9.1 and have everything working with it.
  • Craig Ringer
    Craig Ringer almost 12 years
    I've reworked the answer to remove advice to hand edit /etc/passwd and /etc/group in favour of safer userdel and groupdel. Also, rather than using dselect for hand-selection of packages, use an apt wildcard to match them reliably.
  • Craig Ringer
    Craig Ringer almost 12 years
    The original instructions were very mistaken in advising you to hand-edit /etc/passwd. never do that. Use the userdel and groupdel commands and you won't have that problem in future.
  • Craig Ringer
    Craig Ringer almost 12 years
    Much easier with a wildcard: apt-get --purge remove postgresql\*
  • Craig Ringer
    Craig Ringer almost 12 years
    It's way easier to use a wildcard; see the updated 1st answer.
  • Anuj Gupta
    Anuj Gupta over 11 years
    +1 for pg_dropcluster!! (Example Usage: pg_dropcluster --stop 9.1 main)
  • Rogerio Chaves
    Rogerio Chaves almost 10 years
    add libpq5 and libpq-dev to that purge list, that lib gave me A LOT of problems, until I reinstalled it on the right version
  • johndpope
    johndpope almost 9 years
    apt-get autoremove postgresql*
  • Admin
    Admin over 8 years
    Works on Debian 8 with PostgreSQL 9.4 too.
  • Gregory Arenius
    Gregory Arenius almost 7 years
    I would also add rm -r /var/log/postgresql.
  • dipenparmar12
    dipenparmar12 about 3 years
    That was a perfect solution for me. thanks