Setup PostgreSQL on MacOS using Brew

October 8, 2019

Well, The setup process are fairly similar with what you might expect on any other Unix-like system.

For this guide I'll be using Homebrew to install the PostgreSQL packages.

If you don't have Homebrew set on your MacOS, please visit their website for detailed guide.

After you have brew set, you can just simply run following command to install the PostgreSQL package:

$ brew install postgresql

Easy right?

But, Unlike usual Linux system that we can just start the PostgreSQL service using systemctl or services command. Apparently we are need to manualy assign our PostgreSQL .plist files (Properties Files) to launchctl command everytime we want to start and stop the PostgreSQL service.

This .plist files already provided by the PostgreSQL package, and we just need to link it and make alias to it for more convenient access in the future. Let's start with linking the .plist files first into our user LaunchAgents directory:

$ ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents 

and now create the alias with following commad:

alias pg_start="launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist"
alias pg_stop="launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist"

For the alias, i recommend to put that line into your shell config files to make it permanent everytime you open the shell, ~/.bashrc if you use Bash or ~/.zshrc if you use ZSH.

With that set, we can just type pg_start everytime we want to turn on our PostgreSQL services, and pg_stop if we want to shut it down.

Get in Touch