88

I installed PostgreSQL on a computer with Mac OS X using the One click installer. Then I try to access PostgreSQL using the psql command, but it doesn't seem to be available.

I get this message:

psql
-bash: psql: command not found

Do I have to install something more? Or how can I configure PostgreSQL so I can use it on my computer?

Jonas
  • 29,165
  • 26
  • 57
  • 64

8 Answers8

54

Locate the psql binary. (In a terminal, run locate psql | grep /bin, and make note of the path. (In my case, it's /opt/local/lib/postgresql90/bin/, as it was installed using MacPorts.)

Then, edit the .bash_profile file in your home folder (e.g. mate -w ~/.bash_profile assuming you've textmate), and add the needed line so it's in your path, e.g.:

export PATH=/opt/local/lib/postgresql90/bin/:$PATH

After having saved the file, read the file (. ~/.bash_profile) or open a new terminal, and type psql.

Denis de Bernardy
  • 1,289
  • 1
  • 8
  • 13
  • 20
    Thanks, my path to psql was `/Library/PostgreSQL/9.0/bin/psql` and I created a `.bash_profile` file as you suggested and it works great. – Jonas May 28 '11 at 17:18
  • 1
    @Jonas: +1 Your comment should really go into a separate answer and should be the accepted answer, because this is the default location when installing Postgres on a Mac! – Stefan Haberl Nov 20 '14 at 10:01
  • The locate command didn't work initially for me, I had to first run `sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist` then wait for the `find` process to finish – magritte May 28 '17 at 17:10
  • also had to create the .bash_profile file, mac noob here! ;-) working now though, thanks! – magritte May 28 '17 at 17:23
  • 4
    better `export PATH=/Applications/Postgres.app/Contents/Versions/latest/bin/:$PATH` – andilabs Sep 22 '17 at 07:43
  • I installed postgresql 9.6 via brew and my psql was at `/usr/local/Cellar/postgresql\@9.6/9.6.6/bin/psql` – vargen_ Nov 30 '17 at 11:30
  • 1
    Another option, if you don't want to mess with your PATH settings is to simply add a symlink to a well known location, which is already in your path. For me that would be `ln -s /Library/PostgreSQL/9.2/bin/psql /usr/local/bin/psql` – Stefan Haberl Dec 17 '18 at 21:42
  • If you are using zsh instead of bash, does the export statement have to go in the .zshrc? – Embedded_Mugs Jun 08 '21 at 04:52
33

Appears "the way" to install the client, if you want to use hombrew, is:

brew install postgresql

then psql (the client command line) will now be available to you (it also installs a local Postgres server/database, but you don't have to use that if all you want is the client).

Apparently there's also a 'wrapper' to the psql command to make it more "user friendly" also available via homebrew (brew install pgcli) in case interesting. Pgcli is a command line interface for Postgres with auto-completion and syntax highlighting.

Another option is to install the libpq homebrew package, but it doesn't get in your PATH.

brew install libpq

So follow the instructions it says after install out to add it to your PATH, or brew link --force libpq or add a symlink.

Jonas
  • 29,165
  • 26
  • 57
  • 64
rogerdpack
  • 744
  • 1
  • 9
  • 15
  • 6
    Apparently there's no way of using `psql` without installing the whole database engine. I found `pgcli` more convenient for when for instance you want to have all your services dockerized. Way to go @rogerdpack! – Saul Martínez Dec 31 '17 at 20:10
  • If all you need is the cli, I'd recommend having a look at the @luril [answer](https://dba.stackexchange.com/a/258890/228243) below. – dbaltor Apr 20 '21 at 17:12
  • 1
    Note about these comments suggesting pgcli to avoid installing the whole database engine - the `pgcli` docs state that installing pgcli will also install postgresql if it doesn't exist yet. https://www.pgcli.com/install – Neal Oct 08 '21 at 16:46
14

I strongly recommend using Postgres.app from the Heroku team, which is also supported by them!

It has a menubar icon and the menu has a psql item:

Postgres.app menubar icon menu

You will also find psql included here if you want the same version of that as the server (path may vary by version):

/Applications/Postgres.app/Contents/MacOS/bin

If you want, you can add this path to your startup script to execute psql directly:

PATH=/Applications/Postgres.app/Contents/MacOS/bin:$PATH

CAVEAT! AFAIK, Postgres.app doesn't support connections via Unix sockets (I am not sure what this is...), and supports only TCP/IP connections. So don't panic if you fail to connect there from some other programs.

Nick Chammas
  • 14,170
  • 17
  • 73
  • 119
Eonil
  • 427
  • 6
  • 11
8

Install just client with brew:

brew install pgcli

then link it:

brew link --force libpq

as output you will get the path to psql:

If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/usr/local/opt/libpq/bin:$PATH"' >> ~/.zshrc

to find this path again:

brew info libpq
5

According to the installation guide after the installation has finished there should be shortcuts for StackBuilder, pgAdmin3 and psql in the Application folder of Postgres:

You will also find additional shortcuts to run pgAdmin, the psql command line interface and to access the PostgreSQL documentation.

If there are such shortcuts check where the psql's one is pointing.

Milen A. Radev
  • 1,478
  • 13
  • 12
  • Thanks, this aslo worked. I hadn't seen that documentation, only the documentation on `postgresql.org`. – Jonas May 28 '11 at 17:19
0
brew install libpq    
brew link --force libpq 

I followed the below walkthrough and it worked. https://blog.timescale.com/tutorials/how-to-install-psql-on-mac-ubuntu-debian-windows/

nbk
  • 7,471
  • 5
  • 11
  • 27
0

You can also use the EDB Installer, but during the installation choose to install only the Command Line Tools enter image description here

Ilya
  • 101
-1

To highlight the comment: /Applications/Postgres.app/Contents/Versions/latest/bin – the folder of psql

Vyacheslav
  • 171
  • 1
  • 1
  • 4