Learn how to completely uninstall (and reinstall) PostgreSQL with Homebrew on Mac.
If you’re having problems starting up your PostgreSQL databases running on Homebrew, such as the classic:
could not connect to server: No such file or directory
You can try restarting PostgreSQL services:
brew services restart postgresql
And run psql:
But that often doesn’t work.
Sometimes the fastest way to solve the problem by uninstalling PostgreSQL entirely, and reinstalling a fresh version.
This will show you how to completely remove PostgreSQL with Homebrew.
Warning: If you have any important projects on your machine that run on PostgreSQL databases, now is the time to back them up, because the following will wipe out PostgreSQL completely!
First Open your Terminal, and run the following command to make sure that PostgreSQL is actually installed with Homebrew:
If it doesn’t show up, you’re in the wrong tutorial!
Before you move on, run the following two commands to make sure that your Homebrew installation is healthy, and up to date:
brew doctor brew update
brew doctor is Homebrew's self-diagnosis tool.
Now uninstall PostgreSQL with Homebrew with this command:
brew uninstall postgresql
When the uninstaller is finished, remove all your local PostgreSQL files with these two commands:
rm -rf /usr/local/var/postgres rm -rf .psql_history .psqlrc .psql.local .pgpass .psqlrc.local
PostgreSQL is now completely gone from your machine.
If you want to reinstall PostgreSQL with Homebrew again, run another update on Homebrew (for good measure):
I know you just updated Homebrew, but sometimes I have to do it twice to make it work (I don’t know why).
And then install PostgreSQL:
brew install postgres
Now you can start PostgreSQL services with this command:
brew services start postgresql
To test that it works, we can create the default database:
Connect to PostgreSQL by running this command:
If everything went well your terminal will output the following (or similar):
$ psql psql (12.0) Type "help" for help. yourusername=#