[MacOS] How to Uninstall All Versions of PostgreSQL ~ Homebrew Edition

Tadashi Shigeoka ·  Sat, September 16, 2017

I’ll introduce how to uninstall all versions of PostgreSQL installed with Homebrew on MacOS.

Background for Wanting to Uninstall PostgreSQL

When I tried to start new development with Ruby on Rails, I hadn’t used PostgreSQL in my MacOS development environment for a while, so I got a PG::ConnectionBad error and couldn’t connect to PostgreSQL even when running rails server.

$ brew services restart postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql

Even trying to restart PostgreSQL with the brew services restart command didn’t work, so I decided to uninstall everything and install PostgreSQL from scratch.

Checking Installed PostgreSQL Versions

First, let’s check the installed PostgreSQL versions before uninstalling:

$ ls /usr/local/Cellar/postgresql
9.3.5_1 9.6.3

By the way, the current latest PostgreSQL version is 9.6.5.

$ brew info postgresql
postgresql: stable 9.6.5 (bottled), HEAD
Object-relational database system
https://www.postgresql.org/

Uninstall All Versions with brew uninstall --force

By using brew uninstall —force postgresql with the —force option, you can uninstall all versions.

$ brew uninstall --force postgresql
Uninstalling postgresql... (2,936 files, 32.1MB)

By the way, if you uninstall without the —force option, it will tell you about the existence of the —force option:

$ brew uninstall postgresql
Uninstalling /usr/local/Cellar/postgresql/9.6.3... (3,259 files, 36.6MB)
postgresql 9.3.5_1 1 is still installed.
Remove all versions with `brew uninstall --force postgresql`.

Delete PostgreSQL Configuration Files, Logs, Data, etc.

There should be various files in the path specified by initdb, so don’t forget to delete those as well:

$ ls -l /usr/local/var/postgres
total 37608
-rw-------   1 codenote  admin     4B 11 25  2014 PG_VERSION
drwx------   5 codenote  admin   170B 11 25  2014 base
drwx------  41 codenote  admin   1.4K  1 30  2015 global
drwx------   3 codenote  admin   102B 11 25  2014 pg_clog
-rw-------   1 codenote  admin   4.4K 11 25  2014 pg_hba.conf
-rw-------   1 codenote  admin   1.6K 11 25  2014 pg_ident.conf
drwx------   4 codenote  admin   136B 11 25  2014 pg_multixact
drwx------   3 codenote  admin   102B  6 28 07:26 pg_notify
drwx------   2 codenote  admin    68B 11 25  2014 pg_serial
drwx------   2 codenote  admin    68B 11 25  2014 pg_snapshots
drwx------   3 codenote  admin   102B  6 30 03:13 pg_stat
drwx------   2 codenote  admin    68B  6 30 03:13 pg_stat_tmp
drwx------   3 codenote  admin   102B 11 25  2014 pg_subtrans
drwx------   2 codenote  admin    68B 11 25  2014 pg_tblspc
drwx------   2 codenote  admin    68B 11 25  2014 pg_twophase
drwx------   4 codenote  admin   136B 11 25  2014 pg_xlog
-rw-------   1 codenote  admin    20K 11 25  2014 postgresql.conf
-rw-------   1 codenote  admin   123B  6 28 07:26 postmaster.opts
-rw-r--r--   1 codenote  admin    18M  6 30 03:13 server.log
rm -rf /usr/local/var/postgres

If you don’t delete these, even after reinstalling PostgreSQL, you’ll encounter the problem of not being able to connect to PostgreSQL with this error:

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

Finally, Reinstall PostgreSQL

Once everything is uninstalled and clean, install PostgreSQL again.

I’ve written a separate article about PostgreSQL installation, so please check it out.

That’s all from the Gemba.