psql: command not found Mac

MacosBashPostgresqlPsql

Macos Problem Overview


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

Macos Solutions


Solution 1 - Macos

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 - Macos

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 - Macos

If someone used homebrew with Mojave or later:

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

change version if you need!

Solution 4 - Macos

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 5 - Macos

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 

Solution 6 - Macos

Mojave, Postgres was installed via

brew install https://raw.githubusercontent.com/lembacon/homebrew-core/bede8a46dea462769466f606f86f82511949066f/Formula/postgresql@9.6.rb

How to get psql in your path:

brew link postgresql@9.6 --force

Solution 7 - Macos

Modify your PATH in .bashrc, not in .bash_profile:

http://www.gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files

Solution 8 - Macos

If Postgres was downloaded and installed, running this should fix the issue:

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

Restart the terminal and you'll be able to use psql command.

Ref: https://postgresapp.com/documentation/cli-tools.html

Solution 9 - Macos

Open the file .bash_profile in your Home folder. It is a hidden file.

Add this path below to the end export PATH line in you .bash_profile file :/Applications/Postgres.app/Contents/Versions/latest/bin

The symbol : separates the paths.

Example:

If the file contains: export PATH=/usr/bin:/bin:/usr/sbin:/sbin

it will become: export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Postgres.app/Contents/Versions/latest/bin

How to show hidden files

In Terminal, paste the following: defaults write com.apple.finder AppleShowAllFiles YES

Solution 10 - Macos

ANSWERED ON OCTOBER 2017

run

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

and then restart your terminal.

Solution 11 - Macos

As a postgreSQL newbie I found the os x setup instructions on the postgresql site impenetrable. I got all kinds of errors. Fortunately the uninstaller worked fine.

cd /Library/PostgreSQL/11; open uninstall-postgresql.app/

Then I started over with a brew install followed by this article How to setup PostgreSQL on MacOS

It works fine now.

Solution 12 - Macos

In my case, I have updated my .bash_profile and added Postgres path as shown on some of the answers here but running psql in Terminal still gave me a command not found error.

Had to update .zprofile file and add the Postgres path and it worked!

Steps in Terminal:

  1. nano ~/.zprofile
  2. Add this on the end of PATH: /Library/PostgreSQL/14/bin:${PATH} Note: Mind the version number
  3. Save your changes. Ctrl + o then Ctrl + x
  4. source .zprofile

Solution 13 - Macos

I know some others have already mentioned that the path needs to be updated however for me it did not work till I added the path itself in quotations. I an not 100% sure why that happened for me but this is the command that worked for me (on MacOS Terminal)

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

Solution 14 - Macos

Your path is not defined correctly. Run this command in your terminal:

echo 'export PATH="$PATH:/Library/PostgreSQL/<your psql version>/bin/"' >> ~/.zshrc

zshrc is for you if you are using MacOS Catalina or a newer version.

Solution 15 - Macos

I installed postgresql@13 with Homebrew on Mac, and I followed the given instructions in the zsh terminal after installation.

> postgresql@13 is keg-only, which means it was not symlinked into > /usr/local,because this is an alternate version of another formula. > > If you need to have postgresql@13 first in your PATH, run:
> echo 'export PATH="/usr/local/opt/postgresql@13/bin:$PATH"' >> ~/.zshrc

> For compilers to find postgresql@13 you may need to set:
export LDFLAGS="-L/usr/local/opt/postgresql@13/lib"
export CPPFLAGS="-I/usr/local/opt/postgresql@13/include"

> For pkg-config to find postgresql@13 you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/postgresql@13/lib/pkgconfig" >

> To restart postgresql@13 after an upgrade:
> brew services restart postgresql@13

>Or, if you don't want/need a background service you can > just run:
> /usr/local/opt/postgresql@13/bin/postgres -D /usr/local/var/postgresql@13

You can check if the path is added by running: open .zshrc
and restart the zsh terminal after: exec zsh -l

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3147424View Question on Stackoverflow
Solution 1 - MacosMark SetchellView Answer on Stackoverflow
Solution 2 - MacosmishapView Answer on Stackoverflow
Solution 3 - MacosDavide CarpiniView Answer on Stackoverflow
Solution 4 - MacosamcpView Answer on Stackoverflow
Solution 5 - MacosSobowale BukolaView Answer on Stackoverflow
Solution 6 - MacosVittoriusView Answer on Stackoverflow
Solution 7 - MacosJulien LopezView Answer on Stackoverflow
Solution 8 - MacosObinna NnenanyaView Answer on Stackoverflow
Solution 9 - MacosAndré Luiz ReisView Answer on Stackoverflow
Solution 10 - MacosUtkarsh SharmaView Answer on Stackoverflow
Solution 11 - Macosmt_View Answer on Stackoverflow
Solution 12 - MacosIan C.View Answer on Stackoverflow
Solution 13 - MacosFarhan RiazView Answer on Stackoverflow
Solution 14 - MacosHusnain MehmoodView Answer on Stackoverflow
Solution 15 - MacosikonukView Answer on Stackoverflow