How to run psql on Mac OS X?

  • 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?

  • 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.

    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: +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!

    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

    also had to create the .bash_profile file, mac noob here! ;-) working now though, thanks!

    better `export PATH=/Applications/Postgres.app/Contents/Versions/latest/bin/:$PATH`

    I installed postgresql 9.6 via brew and my psql was at `/usr/local/Cellar/postgresql\@9.6/9.6.6/bin/psql`

    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`

  • 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 psql 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 you have to force-link it (which is discouraged these days)

     brew link --force libpq  
    

    or add it to your PATH (which is encouraged), see the directions that homebrew says after installation.

    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!

  • 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.

    The latest documentation http://postgresapp.com/documentation/cli-tools.html lists the path as `/Applications/Postgres.app/Contents/Versions/latest/bin`

    Actually comment turns out to be a better answer :)

    better `export PATH=/Applications/Postgres.app/Contents/Versions/latest/bin/:$PATH`

    I looked but couldn't find any relation between `Heroku team` and the app. Can you share how are they connected?

  • 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.

    Thanks, this aslo worked. I hadn't seen that documentation, only the documentation on `postgresql.org`.

  • 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
    

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM