How-to Uninstall PostgreSQL 13.3 and Reinstall via Brew | by Wen Yang | Towards Data Science

Who is this for?
For anyone who needs to completely uninstall PostgresSQL 13.3, which was installed via the installer.
This article will cover three topics:
How to uninstall PostgreSQL 13.3
How to reinstall PostgreSQL back via brew
Test to see if it’s working: create database, user, and grant privileges
How to uninstall PostgresSQL 13.3
How to uninstall PostgresSQL 13.3
Step 1: Open your terminal. Check installed version as well as location. In my case, it is installed under /Library/PostgreSQL/13/bin/psql
# check version$ postgres --versionpostgres (PostgreSQL) 13.3# locate where it is installed$ which psql/Library/PostgreSQL/13/bin/psql
Step 2: Depending on whether the uninstall-postgres.app is installed, we have two solutions.
Solution 2A:
Change the directory to run uninstall-postgres.app This app is located upper directory of the bin folder, which in my case it is /Library/PostgreSQL/13 .
# change directory$ cd /Library/PostgreSQL/13$ open uninstall-postgres.app
If the Uninstallation window prompt, you can follow this guide on section [Uninstalling PostgreSQL on Mac].
However, this solution didn’t work for me. I received an error message:
$ open uninstall-postgres.appThe file /Library/PostgreSQL/13/uninstall-postgres.app does not exist.
After trying many other methods online, though none seemed to lead to fruition, I noticed an interesting pattern, that is → for the same function, some people would usepostgres , and other people would use postgresql . Out of desperation, I accidentally discovered solution 2B.
Solution 2B:
Just change →
$ open uninstall-postgres.app to
$ open uninstall-postgresql.app .
It’s such a small change but it worked! 🤩
# change directory$ cd /Library/PostgreSQL/13$ open uninstall-postgresql.app
The uninstallation window prompted! If this works for you too, you can follow this guide on section [Uninstalling PostgreSQL on Mac] until Fig 8.
Important note: after you followed the above guide, all the way until Fig 8, we are not done yet! In order to remove all the Postgres related files, you need step 3.
Step 3: remove Postgres related files
# change to home directory$ cd ~$ sudo rm -rf /Library/PostgreSQL$ sudo rm /etc/postgres-reg.ini# some people also suggested to remove sysctl.conf# but I don't seem to have this file in my environment# so I ignored it. You can try if you'd like$ sudo rm /etc/sysctl.confrm: /etc/sysctl.conf: No such file or directory
🎉🎉🎉 Hooray! We successfully uninstalled PostgreSQL 13.3!!
2. How to reinstall PostgreSQL back via brew 🍺
The reason I need to uninstall PostgreSQL is that I couldn’t use the same code my coworker was using when I need to create a test database. And we suspect that there’s a difference between PostgreSQL installed via installer and PostgreSQL installed via brew. Long story short, it is true at least in my case, it solved the problem.
Install PostgreSQL back via brew is very simple, it has two steps:
# 1. update brew$ brew update# optional: run brew doctor (I did this.)$ brew doctor# 2. install postgresql$ brew install postgresql
By this point, we can launch PostgreSQL by running the below command.
$ brew services start postgresql
After running that, it tells us we successfully started postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
Now, let’s test to see if it’s working.
3. Test: perform three tasks — create a database, user, and grant privileges.
Step 1: launch Postgres
# 1. launch postgres$ psql postgrespsql (13.3)Type "help" for help.postgres=# lspostgres-# helpUse \? for help or press control-C to clear the input buffer.postgres-# \q
you can use the command \l to see all the databases available. For example, this is what I can see.
# note: postgres=# is a prompt, not part of the command# the command is \l, which lists all databasespostgres=# \l
Step 2: I created a database called discovery_db , you can name the database that suits your purpose.
postgres=# create database discovery_db;# use \l to check againpostgres=# \l
Now we have 4 rows and discovery_db is listed on the top. Neat!
Step 3: Create a user with a password.
postgres=# create user discovery_db_user with encrypted password 'discovery_db_pass';
Step 4: Grant all privileges to the user we just created.
postgres=# grant all privileges on database discovery_db to discovery_db_user;
Now, let’s check again →
In the output Access privileges , we can see discovery_db_users has the same privileges as the owner wen (me 😊).
Finally, we can exit postgres \qwith content.
postgres=# \q
Key Takeaways:
If you run into Postgres issues and the blog post you found online don’t seem to work for you, try to modify the commands postgres to postgresql or vice versa.There are many different versions of Postgres. If you can’t run other people’s code, it might be easier to uninstall Postgres completely and reinstall instead of debugging for days.I realized this post might be outdated once there’s a new version of Postgres, but I thought it would at least serve as a time-shot solution for PostgreSQL 13.3 + MacOS Catalina system.
If you run into Postgres issues and the blog post you found online don’t seem to work for you, try to modify the commands postgres to postgresql or vice versa.
There are many different versions of Postgres. If you can’t run other people’s code, it might be easier to uninstall Postgres completely and reinstall instead of debugging for days.
I realized this post might be outdated once there’s a new version of Postgres, but I thought it would at least serve as a time-shot solution for PostgreSQL 13.3 + MacOS Catalina system.