194

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 
6
  • 10
    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. Commented Mar 22, 2016 at 13:14
  • @MarkSetchell I've tried that as well and it still returns psql: command not found Commented Mar 22, 2016 at 13:27
  • Can you give the output of echo $PATH please? Commented Mar 22, 2016 at 13:33
  • /Users/name/.sdkman/candidates/grails/current/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin Commented Mar 22, 2016 at 13:36
  • I just added an edit - there is an issue w/ the path. Commented Mar 22, 2016 at 13:37

23 Answers 23

212

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.

Sign up to request clarification or add additional context in comments.

9 Comments

just RESTART YOUR TERMINAL, that might be all it is
Never, never, never forget the :$PATH at the end of this command, or it's gonna hit the fan and paralyze the shell :p
for Mojave users: export PATH=/usr/local/opt/[email protected]/bin:$PATH
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.
Running brew link postgresql@16 --force solved the issue for me. I didn't even need to update my .zshrc.
|
56

N.B. The following answer has been edited to correct a dangerous typo in the edit of PATH! (see comments below)

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:$PATH 

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

5 Comments

I thought this one was the solution for me but When I modified the .zshrc file and add export PATH=/Applications/Postgres.app/Contents/Versions/latest/bin suddenly all my zshell features were not working. Whenever I type 'ls', 'open' or another says 'command not found in zsh' .
@Erick the same happened to me. Beware this answer, folks
You need to append existing $PATH variable to the newly created one so it does not overwrite everything. export PATH=/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH
I had to open .zshrc using this command: open ~/.zshrc. then copy there this line: export PATH=/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH
Original answer edited to correct dangerous suggestion
55

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.

3 Comments

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/bin .source ~/.profile
postgresapp.com is not an official Postgres documentation.
This did not work on an m2
35

If someone used homebrew with Mojave or later:

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

change version if you need!

Comments

30

Mojave, Postgres was installed via

brew install https://raw.githubusercontent.com/lembacon/homebrew-core/bede8a46dea462769466f606f86f82511949066f/Formula/[email protected] 

How to get psql in your path:

brew link [email protected] --force 

2 Comments

Forcing brew to use the given postgresql version works for me.
be cautious for using --force flag, mine work without the --force flag.
16

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 😁

3 Comments

+1, don't understand why all the hustle, why not set things up automatically during the UI installation process
vi .zshrc for modern macos using zsh and at the end source .zshrc
For the exit vim 1) Save and exit: If you want to save your changes and exit vim, you can use the :wq command. 2) Exit without saving: If you don't want to save your changes and just want to exit vim, you can use the :q!
13

For MacOs Monterey 12.4:

export PATH=$PATH:/Library/PostgreSQL/<postgres version>/bin 

Example : (That works for me) PostgreSQL version 14

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

Then run : exec zsh -l to restart the teminal

Good luck!!

Comments

12

After installing with homebrew, it yields the following instruction:

echo 'export PATH="/opt/homebrew/opt/postgresql@15/bin:$PATH"' >> ~/.zshrc 

Which works after restarting the terminal.

Comments

7

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

Comments

6

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

Comments

5

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

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

3 Comments

I have also added to the .bashrc - still returns the same output.
Marked up for useful information, though it did not solve my specific problem. Thanks for the help.
Yes I didn't spot the extra spaces in your PATH line before Mark pointed that out. :-) Glad I could help.
4

ANSWERED ON OCTOBER 2017

run

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

and then restart your terminal.

Comments

4

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

Comments

3

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.

Comments

2

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.

Comments

2

If you've got Postgres.app installed on macOS:

You can check path with command below (Optional step):

ls /Applications/Postgres.app/Contents/Versions/latest/bin 

If so, just add this line in the end of ~/.zshrc file:

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

Then just close and open terminal and psql command should works well.

Comments

2

The simplest solution for macbook

  1. Open .zshrc file:

nano .zshrc

  1. Add into file(Do not add to the end of file cause some libs like sdk etc must be at the end):

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

  1. Save file by pressing 'control + o' and the press enter.

  2. Exist from terminal by pressing 'control + x'.

  3. Now type 'source ~/.zshrc'

  4. Restart terminals.

  5. Check psql access:

psql

psql (16.1) Type "help" for help.

Comments

2

Using macOS Sequoia & postgres version 15
Add following line to your bash file:

export PATH="/usr/local/opt/postgresql@15/bin:$PATH" 

Comments

1

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" 

Comments

1

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

1 Comment

Thank! this is the only solution that worked for me.
1

A simple restart of the terminal worked for me after the export commands mentioned in the post installation logs were executed.

Comments

1

If you are using brew to install PostgreSQL you will need to add it to the path:

export PATH=/opt/homebrew/Cellar/postgresql@13/13.16/bin:$PATH 

To make it work as default you can add that line to your .zshrc file. Also to check the version of Postgres you installed just navigate to the folder /opt/homebrew/Cellar to look for it.

Comments

1

I have added this to .profile and it worked!

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

2 Comments

Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?
Please don't add "thank you" as an answer. Once you have sufficient reputation, you will be able to vote up questions and answers that you found helpful. - From Review

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.