How to uninstall specific versions of Postgres?

On Ubuntu 14.04 I have done this to get the latest postgres:

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get upgrade

Then I installed version 9.4:

sudo apt-get install postgresql-9.4

However it seems I have now three versions:

sudo service postgresql stop

 * Stopping PostgreSQL 9.3 database server  [ OK ]
 * Stopping PostgreSQL 9.4 database server  [ OK ]
 * Stopping PostgreSQL 9.5 database server  [ OK ]

How can I keep only 9.4 and uninstall the other ones? Thanks

UPDATE:

As suggested in comments I have uninstalled 9.3 and 9.5.

But when I do this:

sudo service postgresql status
9.3/main (port 5432): down
9.4/main (port 5434): online
9.5/main (port 5433): down

So far so good, but when I switch to postgres:

sudo su postgres

and do a psql, I get an error:

psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

Different versions of PostgreSQL are located in the packages postgresql-9.*.

  1. So you should remove your two packages:

    sudo apt-get purge postgresql-9.3 postgresql-9.5
    
  2. Then you should remove unnecessary config folders:

    rm -r /etc/postgresql/9.3/ /etc/postgresql/9.5
    
  3. And from /var/lib/ folder if you have:

    /var/lib/postgresql/*
    

I hope it will help you.


Adding answer very late but might help people here.

Short answer: Error indicates postgres configured on wrong(not default) port. Use the right port

Long answer: When you installed multiple version of Postgresql, all of them started running postgres clusters on different port. In your output, following postgres versions are running: 9.3 --> 5432, 9.4 -->5434, 9.5 --> 5433.

sudo service postgresql status
9.3/main (port 5432): down
9.4/main (port 5434): online
9.5/main (port 5433): down

You can uninstall the other versions using sudo apt-get purge postgresql-9.x where 9.x is your version however in your case the version(9.4) which was present on your system is configured to run on port 5434. Thus the error Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"? perfectly indicates that psql by default trying to connect to port 5432 which no longer has any postgres attached.

Two solutions here:

  • Either use psql -p 5434 i.e port option(-p/--port) in psql command.
  • Or configure your postgres server port configuration value in /etc/postgresql/9.5/main/postgresql.conf to serve on 5432. Don't forget to restart postgres after change.