How to upgrade PostgreSQL with Homebrew

Updated

TL;DR

If you’ve just upgraded Postgres with Homebrew, and Postgres won’t start, as long as you don’t care about any data stored locally, remove all versions of Postgres:

brew remove --force postgresql

You might have to specify a version if you used one of the @ version numbers, such as Postgres 14:

brew remove --force postgresql@14

Delete the Postgres folders:

rm -rf /usr/local/var/postgres/
rm -rf /usr/local/var/postgresql@14/

If you’re on an Apple Silicon Mac, delete these folders:

rm -rf /opt/homebrew/var/postgres
rm -rf /opt/homebrew/var/postgresql@14

Reinstall Postgres:

brew install postgresql@14

Fire it up:

brew services start postgresql@14

Verify that it’s running:

brew services list

The Long Version

I’m currently doing some Rails consulting for a company that uses Postgres in their app. After cloning the app to my computer, I wanted to make sure I had the latest version of Postgres.

Before installing or upgrading anything with Homebrew, I always run these two commands first:

$ brew update
$ brew doctor

Once I know everything is up to date and firing on all cylinders, I can get my installs on. In this case, I wanted to upgrade everything I had ever installed with Homebrew. Mainly because I hadn’t used my iMac for a few months while I was in San Francisco for the Code for America fellowship.

$ brew upgrade

After everything was up to date, I tried to install the DB in the Rails app:

$ rake db:create

That failed with this error message:

could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?

could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?

could not connect to server: Connection refused
Is the server running on host "localhost" (fe80::1) and accepting TCP/IP connections on port 5432?

The next step was to read through Homebrew’s notes about Postgres:

$ brew info postgres

I noticed the following:

If builds of PostgreSQL 9 are failing and you have version 8.x installed, you may need
to remove the previous version first.
See: https://github.com/mxcl/homebrew/issues/issue/2510

Although I didn’t have any 8.x versions installed, I decided to remove Postgres and uninstall it since I didn’t have any local data that I cared about:

$ brew remove postgresql

This only uninstalled 9.3.2 (the latest version), but I knew I had three other versions by looking in /usr/local/Cellar/postgresql/. I wanted to remove them all to start with a clean slate, and Homebrew was helpful enough to let me know how to do it after I tried to run the previous command a second time:

$ brew remove postgresql
Error: postgresql has multiple installed versions
Use `brew remove --force postgresql` to remove all versions

I followed those instructions and successfully uninstalled all versions of Postgres:

$ brew install postgresql

I still couldn’t launch Postgres :(

After some digging around, I came across this comment in one of the Homebrew issues on GitHub. This pointed me in the right direction by mentioning the /usr/local/var/postgres folder. After uninstalling Postgres one last time, I simply deleted the /usr/local/var/postgres folder since I didn’t have any Postgres data worth preserving.

Then I reinstalled Postgres and was finally able to launch it like so:

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

I didn’t want to see anyone else waste time trying to figure this out, so I wrote this up for you.