117

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?

11 Answers 11

70

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

brew install postgresql@15

(you need to specify the version after @) 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.

5
  • 11
    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! Commented Dec 31, 2017 at 20:10
  • If all you need is the cli, I'd recommend having a look at the @luril answer below.
    – dbaltor
    Commented Apr 20, 2021 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. pgcli.com/install
    – Neal
    Commented Oct 8, 2021 at 16:46
  • This answer no longer works because you're required to specify the version now-a-days. The solution is brew install postgresql@15 or whatever the newest version is. Commented Mar 31, 2023 at 21:31
  • 1
    Want to add a note here and say that if you target a specific version like this, it will not add the executables to $HOMEBREW_PREFIX/bin and therefore you will not be able to use the commands until you add them to your PATH. Commented Nov 29, 2023 at 16:37
57

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.

8
  • 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
    Commented May 28, 2011 at 17:18
  • 2
    @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! Commented Nov 20, 2014 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
    Commented May 28, 2017 at 17:10
  • 4
    better export PATH=/Applications/Postgres.app/Contents/Versions/latest/bin/:$PATH
    – andilabs
    Commented Sep 22, 2017 at 7:43
  • 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 Commented Dec 17, 2018 at 21:42
19

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
1
  • It is not just client, the documentation says: "If you're on macOS you can install it via Homebrew. Just be aware that this will install postgresql if you don't already have it."
    – Meglio
    Commented May 23, 2022 at 14:03
18

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.

4
  • 6
    The latest documentation postgresapp.com/documentation/cli-tools.html lists the path as /Applications/Postgres.app/Contents/Versions/latest/bin
    – Bryan Ash
    Commented Jan 18, 2016 at 23:36
  • Actually comment turns out to be a better answer :) Commented May 4, 2017 at 4:43
  • 1
    better export PATH=/Applications/Postgres.app/Contents/Versions/latest/bin/:$PATH
    – andilabs
    Commented Sep 22, 2017 at 7:43
  • I looked but couldn't find any relation between Heroku team and the app. Can you share how are they connected? Commented Feb 14, 2018 at 13:58
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.

1
  • Thanks, this aslo worked. I hadn't seen that documentation, only the documentation on postgresql.org.
    – Jonas
    Commented May 28, 2011 at 17:19
1
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/

1

I installed PostgresQL with the installer from https://www.postgresql.org/download/ The psql executable was put into /Library/PostgreSQL/14/bin. I added that to my path.

0

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

0

If you don't want to use brew and installing the whole server is overkill, I found that pgAdmin (the GUI app) provides you with a psql cli. Right click the database and choose PSQL Tool to open up a session.

0

Kinda similar to @inyourcorner, but i had to double-click on the database... see image: (on mac) (and thanks for leading me there!)... -Click on elephant (postgres) icon in upper toolbar -Double-click on the database to work on -Takes you to terminal with prompt to enter psql commands

How to enter psql commands on mac

-1

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

1
  • please add more explantion, you have lesss than the comment
    – nbk
    Commented Jan 30, 2021 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.