Completely Uninstall and Reinstall PSQL on OSX

bita djaghouri
Feb 17, 2017 · 2 min read

ONLY do this if you need to completely wipe out PSQL and do not care about your PSQL version or any databases that you’ve made. If you do care, back up your databases before following any of these steps!! SERIOUSLY! I CANNOT STRESS THIS ENOUGH!

This post assumes that you are using Homebrew. I suggest that you should absolutely be running Homebrew, but I’ll save the “why” of that for another post.

When PSQL fails on you, it really fails on you.

You go in to your terminal and type psql in hopes of launching psql in terminal and doing awesome database stuff.

But this happens…

Service `postgresql` already started, use `brew services restart postgresql` to restart.
  Is the server running locally and accepting connections on Unix   domain socket “/tmp/.s.PGSQL.5432”?
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”?

No problem. You just run a quick brew services restart postgresql and you should be golden.

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

Great! You run psql again. But this happens…

Service `postgresql` already started, use `brew services restart postgresql` to restart.
   Is the server running locally and accepting connections on Unix   domain socket “/tmp/.s.PGSQL.5432”?
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”?

Error-ception. After much Googling and mostly incomplete solutions you will find that the only thing you can do is completely uninstall and reinstall PSQL.

Here is how:

Make sure brew is happy:

brew doctor
brew update

Uninstall PostgreSQL using Homebrew:

brew uninstall postgresql

Remove all local PostgreSQL files:

rm -rf rm -rf /usr/local/var/postgres
rm -rf .psql_history .psqlrc .psql.local .pgpass .psqlrc.local

Confirm that PostgreSQL is actually uninstalled:

brew list

Update Homebrew for posterity and then reinstall PostgreSQL (Homebrew will probably offer some advice here. It’s good advice. Take it.):

brew update
brew install postgres

Start PostgreSQL services with Homebrew. If you restart PostgreSQL using Homebrew, you will not have to restart Postgres services every time you want to run psql:

brew services start postgresql

Create your default user database:

createdb `whoami`

Run psql to find out if the PostgreSQL gods have smiled on you today. You should hopefully see:

psql (9.6.1)
Type “help” for help.myusername=#

Happy databasing.

bita djaghouri

Written by

Engineer, Dog-Lover, Photographer, Shoe-Hoarder, Parallel Parker, Hamburger, ISFP