psql: command not found Mac

195,000

Solution 1

You have got the PATH slightly wrong. You need the PATH to "the containing directory", not the actual executable itself.

Your PATH should be set like this:

export PATH=/Library/PostgreSQL/9.5/bin:$PATH

without the extra sql part in it. Also, you must remove the spaces around the equals sign.

Keywords: Postgresql, PATH, macOS, OSX, psql

Solution 2

From the Postgres documentation page:

sudo mkdir -p /etc/paths.d && echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp

restart your terminal and you will have it in your path.

Solution 3

For me this worked:

  1. Downloading the App: https://postgresapp.com/downloads.html

  2. Running commands to configure $PATH - note though that it didn't work for me. https://postgresapp.com/documentation/cli-tools.html

  3. Manually add it to the .bash_profile or .zshrc document if you are using zsh:

     cd  # to get to your home folder
     open .bash_profile  # to open your bash_profile
     # Or
     open .zshrc  # to open your zshrc
    

    In your bash profile or zshrc add:

     # Postgres
     export PATH=/Applications/Postgres.app/Contents/Versions/latest/bin
    

    Save the file. Restart the terminal. Type 'psql'. Done.

Solution 4

If someone used homebrew with Mojave or later:

export PATH=/usr/local/opt/[email protected]/bin:$PATH

change version if you need!

Solution 5

If Postgresql was downloaded from official website. After installation, running these commands helped me resolve the psql issue.

Go to your home directory with cd ~

In your home directory, run ls -a. Edit the .bash_profile file with vim

vi .bash_profile opens the vim editor.

Insert by pressing i on the editor.

Add export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/<Version Number>/bin

The Version Number refers to the version number of the postgresql installed on your local machine. In my case, version 12 was installed, so I inputed

export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/12/bin .

Press the esc key and press :wq to exit the editor.

Enter source .bash_profile in your terminal to read and execute the content of a file just passed as an argument in the current shell script.

Run psql

terminal result

In summary:

  • cd ~
  • vi .bash_profile
  • export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/12/bin Take note of the version number
  • exit vim
  • source .bash_profile
  • psql Works 😁
Share:
195,000

Related videos on Youtube

user3147424
Author by

user3147424

please delete me

Updated on May 05, 2022

Comments

  • user3147424
    user3147424 about 2 years

    I installed PostgreSQL via the graphical install on http://www.postgresql.org/download/macosx/

    I see it in my applications and also have the psql terminal in my applications. I need psql to work in the regular terminal for another bash script I'm running for an app.

    For some reason, when I run

    psql
    

    in the Mac terminal, my output is

    -bash: psql: command not found
    

    I ran the following in the terminal:

    locate psql | grep /bin
    

    and the output was

    /Library/PostgreSQL/9.5/bin/psql
    

    I then edited my ~/.bash_profile and added it to the path like so:

    export PATH = /Library/PostgreSQL/9.5/bin/psql:$PATH
    

    The only other thing in ~/.bash_profile is SDK man and it's at the bottom of the script as it says it should be. I've tried setting the bath to just the /Library/PostgreSQL/9.5/bin/ as well. I've restarted my terminal also.

    How can I get psql to work?

    EDIT After adding to .bashrc, this output is returned when I open terminal

    -bash: export: `/Library/PostgreSQL/9.5/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin': not a valid identifier
    
    • Mark Setchell
      Mark Setchell about 8 years
      You have got the PATH slightly wrong. You need the PATH to "the containing directory", not the actual executable. Your PATH should be export PATH=/Library/PostgreSQL/9.5/bin:$PATH - without the extra psql bit in it. And also, you must remove the spaces around the equals sign.
    • user3147424
      user3147424 about 8 years
      @MarkSetchell I've tried that as well and it still returns psql: command not found
    • Julien Lopez
      Julien Lopez about 8 years
      Can you give the output of echo $PATH please?
    • user3147424
      user3147424 about 8 years
      /Users/name/.sdkman/candidates/grails/current/bin:/usr/local‌​/bin:/usr/bin:/bin:/‌​usr/sbin:/sbin
    • user3147424
      user3147424 about 8 years
      I just added an edit - there is an issue w/ the path.
    • user3147424
      user3147424 about 8 years
      @MarkSetchell I didn't see removing the spaces. That worked - if you want to add the answer I'll mark it as accepted.
  • user3147424
    user3147424 about 8 years
    I have also added to the .bashrc - still returns the same output.
  • user3147424
    user3147424 about 8 years
    Marked up for useful information, though it did not solve my specific problem. Thanks for the help.
  • Julien Lopez
    Julien Lopez about 8 years
    Yes I didn't spot the extra spaces in your PATH line before Mark pointed that out. :-) Glad I could help.
  • kevin11
    kevin11 over 6 years
    The instructions also say "Of course, you could also just edit your .profile file instead." My cli install did not work until I did this on MacOS 10.12 Sierra with Postgres 10 (the sudo command did not work for me): vi ~/.profile add this line: export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/10/b‌​in .source ~/.profile
  • russiansummer
    russiansummer over 6 years
    just RESTART YOUR TERMINAL, that might be all it is
  • benjaminz
    benjaminz over 6 years
    Never, never, never forget the :$PATH at the end of this command, or it's gonna hit the fan and paralyze the shell :p
  • Mark Setchell
    Mark Setchell about 5 years
    @Luffydude Questions are free. Feel free to ask a new one and we'll try and help.
  • Luffydude
    Luffydude about 5 years
    Actually didn't realise I needed to install something, now it works, thanks :)
  • Davide Carpini
    Davide Carpini over 4 years
    for Mojave users: export PATH=/usr/local/opt/[email protected]/bin:$PATH
  • Jan Katins
    Jan Katins over 4 years
    If you simply want to pin a single version of postgresql and still have the binaries in the default PATH, you can also run brew link [email protected] --force.
  • avocado
    avocado almost 3 years
    +1, don't understand why all the hustle, why not set things up automatically during the UI installation process
  • Nuzhdin Vladimir
    Nuzhdin Vladimir over 2 years
    if you work with "zsh", make sure you are modifying files with "z" prefix. It cost me an hour =(
  • Hamza.am.i
    Hamza.am.i over 2 years
    vi .zshrc for modern macos using zsh and at the end source .zshrc
  • Androidz
    Androidz about 2 years
    Thank! this is the only solution that worked for me.