Step-by-step guide on removing PostgresSQL installed via installer and re-install via brew šŗ for macOS Catalina
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 a database, user, and grant privileges
1. 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 --version postgres (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 prompts, you can followĀ this guideĀ in section [Uninstalling PostgreSQL on Mac].
However, this solution didnāt work for me. I received an error message:
$ open uninstall-postgres.app The 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 say the type postgres
, and other people would suggest type 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Ā in 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=# ls
postgres-# help
Use \? 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 again
postgres=# \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Ā with the command \q
.
postgres=# \q
Key Takeaways:
- If you run into Postgres issues and the blog post you found online doesnā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.
romantik69.co.il says
Very nice write-up. I definitely appreciate this site. Thanks!
ThreePortkeys says
Thanks a lot! It’s very encouraging to hear that the solution actually works not just for me.