Installing the PostgreSQL
To install PostgreSQL using homebrew, run the following command:
Make sure you have homebrew installed and updated. if not run the following command to update:
brew install postgresql
You do not need to install
libpq if you already have PostgreSQL installed using above command. Ony install libpq if you do not have postgresql installed and want to use psql and pg_dump clients.
To install libpq using homebrew, use the following command:
brew install libpq
This will give you access to utilities such as psql and pg_dump without having to install the full Postgres package.
However, since homebrew provides some of the same utilities as Postgres, it is installed as
"keg-only" by default. This means that it is not in the
PATH by default. Homebrew will provide instructions on how to add it to the PATH after installation.
In my case, it was this:
echo 'export PATH="/usr/local/opt/libpq/bin:$PATH"' >> ~/.zshrc
Alternatively, you can create symlinks for the utilities you need, for example:
ln -s /usr/local/Cellar/libpq/14.3/bin/psql /usr/local/bin/psql
replace 14.3 with the version of libpq that is installed.
Alternatively, you could instruct homebrew to "link all of its binaries to the PATH anyway"
brew link --force libpq
but then you would not be able to install the Postgres package later.
Managing the PostgreSQL
As we have installed postgresql with homebrew, we will use homebrew to start and stop the service.
Start the postgres service
brew services start postgresql
Stop the postgres service
brew services stop postgresql
Once it is up and running, you can create a new user to log in and interact with the database.
After you have logged in, you can create a new user and database:
CREATE USER <username> WITH PASSWORD '<password>';
CREATE DATABASE permission to the user
ALTER ROLE <username> WITH CREATEDB;