372

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.

Vitaly Zdanevich
  • 13,032
  • 8
  • 47
  • 81
  • Why do you think there exists a "correct way" to do this, given that you linked to the official download page, which says there isn't a way? – Ssswift Jun 21 '17 at 17:50
  • For those on MacPorts, here's what I did: https://superuser.com/questions/305031/how-do-i-install-the-postgres-command-line-client-psql-on-os-x-using-macports – sudo Sep 01 '19 at 20:04
  • 13
    @Ssswift It doesn't say there isn't a way, just doesn't say there is a way. – sudo Sep 01 '19 at 20:05

8 Answers8

797

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.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
PPS
  • 8,040
  • 2
  • 9
  • 16
  • 23
    Works like a charm after creating symlink: ln -s /usr/local/Cellar/libpq/10.3/bin/psql /usr/local/bin/psql – Engrost Apr 30 '18 at 10:37
  • 38
    You could also do `brew link --force libpq` but that will create a bunch of other symlinks you may not want/need. – Dave Jun 20 '18 at 11:39
  • 5
    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` – hotzen May 15 '19 at 09:16
  • 2
    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` – David Hull Aug 23 '19 at 22:13
  • `PATH=/usr/local/opt/libpq/bin:$PATH` to add all commands to your path in one go, using a directory name that doesn't change with the version of `libpq` that you install. – George Hawkins Jun 26 '20 at 14:50
  • does this handle multiple different versions of postgresql? – J.Wolfe Sep 08 '20 at 13:46
  • @J.Wolfe kind of there's only one version in homebrew of libpq (i.e. the latest). If you run ` brew search postgresql` it's got a few different older versions available, though they disappear over time... – rogerdpack May 05 '21 at 18:03
  • 30
    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` – Esko Nov 11 '21 at 07:17
  • For M1 macs (insert your version of PSQL): `ln -s /opt/homebrew/Cellar/libpq/15.0/bin/psql /usr/local/bin/psql` – Dmytro Bogatov Oct 14 '22 at 19:20
  • which one is the better way? exporting PATH or creating symlinks? it seems that the export operation needs to work on every terminal session, so it is an extra process, Am I right? – Samet Baskıcı Jan 18 '23 at 09:24
  • 1
    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`). – Josh Hibschman Mar 20 '23 at 13:50
95

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
    
C.K.
  • 4,348
  • 29
  • 43
  • 2
    `echo 'export PATH="/usr/local/opt/libpq/bin:$PATH"' >> ~/.bash_profile` if you're using bash. – HenryC May 14 '19 at 19:30
  • 1
    @HenryC yep, thanks, bash is much more common, my answer was based on zsh. – C.K. May 14 '19 at 20:31
  • 1
    See [this](https://stackoverflow.com/questions/44654216/correct-way-to-install-psql-without-full-postgres-on-macos#comment123605725_49689589) comment for Mac Mini M1 `PATH` variable. – Timo Jun 21 '22 at 09:17
  • 8
    `/opt/homebrew/opt/libpq/bin` on M1 processor – it3xl Nov 01 '22 at 08:16
70

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.

Andrew Bobulsky
  • 1,070
  • 8
  • 9
  • 3
    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 https://stackoverflow.com/a/49689589/2469559 is the correct one. – Benjamin R Mar 11 '19 at 06:49
  • 4
    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](https://github.com/Homebrew/homebrew-core/blob/6a886b97fa4f04525bfeb3fb21c4b79c2ec92a12/Formula/libpq.rb#L13) 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. – Andrew Bobulsky Mar 11 '19 at 17:17
  • 1
    Yep this way doesn't require messing with symlinks or your PATH :) – rogerdpack May 05 '21 at 18:17
60

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
Josh Goebel
  • 837
  • 5
  • 10
  • 1
    I like that I don't have to create the symlink manually. This should be the accepted answer. Thank you! – demisx May 16 '20 at 15:13
38

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.

ngeek
  • 7,733
  • 11
  • 36
  • 42
Virak
  • 491
  • 4
  • 4
7

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;'
J.Wolfe
  • 688
  • 8
  • 11
3

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
Sithu
  • 4,241
  • 1
  • 20
  • 28
-11

You could try brew install postgresql

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

Saidur Rahman
  • 420
  • 2
  • 6
  • 19