529

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.

3
  • For those on MacPorts, here's what I did: superuser.com/questions/305031/… Commented Sep 1, 2019 at 20:04
  • 16
    @Ssswift It doesn't say there isn't a way, just doesn't say there is a way. Commented Sep 1, 2019 at 20:05
  • Congratulations! The wiki now links to your question. wiki.postgresql.org/wiki/Homebrew Commented Apr 21 at 15:37

9 Answers 9

1072

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:

# if using Apple Silicon (M1 or later)
echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc

# if using Intel x64
echo 'export PATH="/usr/local/opt/libpq/bin:$PATH"' >> ~/.zshrc

Alternatively, you can create symlinks for the utilities you need. E.g.:

ln -s /opt/homebrew/opt/libpq/bin/psql /opt/homebrew/bin/

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.

Sign up to request clarification or add additional context in comments.

13 Comments

You could also do brew link --force libpq but that will create a bunch of other symlinks you may not want/need.
symlinks du jour ln -s /usr/local/Cellar/libpq/11.3/bin/psql /usr/local/bin/psql / ln -s /usr/local/Cellar/libpq/11.3/bin/pg_dump /usr/local/bin/pg_dump / ln -s /usr/local/Cellar/libpq/11.3/bin/pg_restore /usr/local/bin/pg_restore
Symlinks that do not depend on libpq version: for cmd in psql pg_dump pg_restore; do ln -s ../opt/libpq/bin/$cmd /usr/local/bin/$cmd; done
Now that we're in M1 world, the correct path to binaries has changed and the command thus is echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc
More simply, sudo ln -s $(brew --prefix)/opt/libpq/bin/psql /usr/local/bin/psql. Repeat for others, like pg_dump. And @SametBaskıcı -- I prefer symlinks so other non-terminal tools can use it (like Intellij DB connector with pg_dump).
|
171

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
    
    # or on M1 MacOS
    echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc
    source ~/.zshrc
    

    if use bash:

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

5 Comments

echo 'export PATH="/usr/local/opt/libpq/bin:$PATH"' >> ~/.bash_profile if you're using bash.
@HenryC yep, thanks, bash is much more common, my answer was based on zsh.
See this comment for Mac Mini M1 PATH variable.
/opt/homebrew/opt/libpq/bin on M1 processor
thumbs up for M1
87

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

1 Comment

I like that I don't have to create the symlink manually. This should be the accepted answer. Thank you!
78

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.

3 Comments

This doesn't actually answer the question, which boils down to "how do I install psql (and maybe other postgres utilities) WITHOUT installing postgres". @PPS's answer stackoverflow.com/a/49689589/2469559 is the correct one.
IMO, this is the better option because it doesn't require the brew link step. Forcing the link with libpq is necessary due to the keg_only declaration in the formula. Given this specific complication, I stand by this answer as being the "correct" way to do what the question asks. I recognize that many users will still prefer the libpq approach though.
Yep this way doesn't require messing with symlinks or your PATH :)
65

Install libpq:

 brew install libpq

Then, create a symlink:

sudo ln -s $(brew --prefix)/opt/libpq/bin/psql /usr/local/bin/psql

Hope it helps.

3 Comments

I think this is the best answer because it's clear, to the point, and works independently of mac version.
I also managed to link pg_dump and pg_restore in the same way, thanks!
sudo ln -s $(brew --prefix)/bin/psql /usr/local/bin/psql
11

Found so many useful answers here, but a bit outdated since homebrew moved the installation files to /opt/homebrew/Cellar/libpq/15.1. After libpq is installed with brew install libpq you can run below command to see new location

brew link --force libpq

Then you can add it to your zshrc with

echo 'export PATH="/opt/homebrew/Cellar/libpq/15.1/bin:$PATH"' >> ~/.zshrc

Comments

10

Binaries courtesy of EnterpriseDB

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;'

2 Comments

This worked for me. Thanks! For future reference, just download the version with the same major number (minor doesn't matter)
I need only backup or restore DB. Easiest solution
5

A little late, but really the simpliest way is only to:

brew install libpq

It should, automatically add the libpq binaries (which include psql) in /opt/homebrew/opt/libpq/bin. This path might is not in your PATH environment variable. In my case, my regular terminal (that I do not use) has it but Warp (that I do use) did not. To fix this, the libpq binary path needs to be added to your PATH environment variable. To do so, the following line has to be added to the shell configuration file :

export PATH="/opt/homebrew/opt/libpq/bin:$PATH"

Example for the shell configuration file being .zshrc

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

To check it, run : psql

Comments

-14

You could try brew install postgresql

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

4 Comments

brew install postgresql install psql with database itself :(
It happens to also install psql, but, as a bonus, you don't have to mess with the PATH or symlinks...
There may be many reasons not to install full postgress. And the question was how to avoid installing it
PostgresApp is amazing and could be the answer if you choose to never initialize the database and use it just for the CLI tools.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.