In the event you need to install the Postgres client libraries on Mac (as a dependency for another tool), or you just need a working psql
command on your Mac without needing to install a full Postgres server on your Mac, then you can follow the instructions below.
Homebrew is one of the most used package managers for Mac. It allows you to install and manage packages through the brew
command.
At this point we are assuming that you already have your Mac setup, following the article Setting up your Mac OS X system for support, where installing Homebrew is one of the first steps. But if not, you can install Homebrew following the instructions from the official website.
From the official PostgreSQL documentation, you can see that there are multiple options for getting the full PostgreSQL installation on your Mac. The most widely used option is the EDB interactive Postgres installer, but there is an option for installing full PostgreSQL using Homebrew too:
brew install postgresql
It works fine if you want to get a full Postgres installation on your Mac, but that's not the purpose of the article.
In many cases you want only the Postgres client libraries needed as a dependency for another tool. In other cases, you just need the Postgres client tools such as psql
, pg_dump
and pg_restore
, for example if you have the Postgres server running inside a Docker container or a virtual machine, or even on another server accessible via the network.
The way to install the Postgres client is:
brew install libpq
However, as the libpq
package conflicts with the postgresql
package (because the full postgresql
package also install the same client libraries and tools), then the libpq
package is installed as "keg-only", which means that by default it's not included in the system path.
So in order to be able to use psql
you have 2 options:
1- Include the libpq
directory into your $PATH
variable, for example:
echo 'export PATH="/usr/local/opt/libpq/bin:$PATH"' >> ~/.bash_profile
2- Or you can tell brew
to forcefully link the libpq
binaries to the system path anyway:
brew link --force libpq
However, by doing this then you won't be able to install the postgresql
package anymore.