Correct way to install psql without full Postgres on macOS?

MacosPostgresqlPsqlMacos Sierra

Macos Problem Overview


Official page do not mention such case. But many users need only psql without a local database (I have it on AWS). Brew do not have psql.

Macos Solutions


Solution 1 - Macos

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.

Solution 2 - Macos

> 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
    

Solution 3 - 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.

Solution 4 - Macos

If you truly don't need postgresql then you don't even have to alter your path to use libra, just link libpq. The docs say the only reason it isn't is to avoid conflicts with the PostgreSQL package.

brew uninstall postgresql
brew install libpq
brew link --force libpq

Solution 5 - Macos

Install libpq:

 brew install libpq

Then, create a symlink:

ln -s /usr/local/opt/libpq/bin/psql /usr/local/bin/psql

Hope it helps.

Solution 6 - Macos

I found all of these really unsatisfying, especially if you have to support multiple versions of postgres. A MUCH easier solution is to download the binaries here:

https://www.enterprisedb.com/download-postgresql-binaries

And simply run the executable version of psql that matches the database you're working against without any extra steps.

example:

./path/to/specific/version/bin/psql -c '\x' -c 'SELECT * FROM foo;'

Solution 7 - Macos

You could try brew install postgresql

But this provides a nice GUI to manage your databases https://postgresapp.com

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVitaly ZdanevichView Question on Stackoverflow
Solution 1 - MacosPPSView Answer on Stackoverflow
Solution 2 - MacosC.K.View Answer on Stackoverflow
Solution 3 - MacosAndrew BobulskyView Answer on Stackoverflow
Solution 4 - MacosJosh GoebelView Answer on Stackoverflow
Solution 5 - MacosVirakView Answer on Stackoverflow
Solution 6 - MacosJ.WolfeView Answer on Stackoverflow
Solution 7 - MacosSaidur RahmanView Answer on Stackoverflow