Correct way to install psql without full Postgres on macOS?

Homebrew only really has the postgres formula, and doesn't have any specific formula that only installs the psql tool.

So the "correct way" to get the psql application is indeed to install the postgres formula, and you'll see toward the bottom of the "caveats" section that it doesn't actually run the database, it just puts the files on your system:

$  brew install postgres
==> Downloading https://homebrew.bintray.com/bottles/postgresql-9.6.5.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring postgresql-9.6.5.sierra.bottle.tar.gz
==> /usr/local/Cellar/postgresql/9.6.5/bin/initdb /usr/local/var/postgres
==> Caveats
<snip>
To have launchd start postgresql now and restart at login:
  brew services start postgresql
Or, if you don't want/need a background service you can just run:
  pg_ctl -D /usr/local/var/postgres start
==> Summary
🍺  /usr/local/Cellar/postgresql/9.6.5: 3,269 files, 36.7MB

Now you can use psql to connect to remote Postgres servers, and won't be running a local one, although you could if you really wanted to.

To verify that the local postgres daemon isn't running, check your installed homebrew services:

$ brew services list
Name       Status  User Plist
mysql      stopped      
postgresql stopped      

If you don't have Homebrew Services installed, just

$ brew tap homebrew/services

...and you'll get this functionality. For more information on Homebrew Services, read this excellent blog post that explains how it works.


You could also use homebrew to install libpq.

brew install libpq

This would give you psql, pg_dump and a whole bunch of other client utilities without installing Postgres.

Unfortunately since it provides some of the same utilities as are included in the full postgresql package, brew installs it "keg-only" which means it isn't in the PATH by default. Homebrew will spit out some information on how to add it to your 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. E.g.:

ln -s /usr/local/Cellar/libpq/10.3/bin/psql /usr/local/bin/psql

Note: used installed version instead of 10.3.

Alternatively, you could instruct homebrew to "link all of its binaries to the PATH anyway"

 brew link --force libpq

but then you'd be unable to install the postgresql package later.


libpq 11.2
MacOS & zsh or bash

below works

  1. install libpq
brew install libpq
  1. update PATH

    if use zsh:

    echo 'export PATH="/usr/local/opt/libpq/bin:$PATH"' >> ~/.zshrc
    source ~/.zshrc
    

    if use bash:

    echo 'export PATH="/usr/local/opt/libpq/bin:$PATH"' >> ~/.bash_profile
    source ~/.bash_profile