[macos] psql: command not found Mac

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

This question is related to macos bash postgresql psql

The answer is


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 document:

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

    In your bash profile add:

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

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


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

ANSWERED ON OCTOBER 2017

run

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

and then restart your terminal.


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.


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


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


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.


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

If someone used homebrew with Mojave or later:

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

change version if you need!


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

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


Examples related to macos

Problems with installation of Google App Engine SDK for php in OS X dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac Could not install packages due to an EnvironmentError: [Errno 13] How do I install Java on Mac OSX allowing version switching? Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) How can I install a previous version of Python 3 in macOS using homebrew? Could not install packages due to a "Environment error :[error 13]: permission denied : 'usr/local/bin/f2py'"

Examples related to bash

Comparing a variable with a string python not working when redirecting from bash script Zipping a file in bash fails How do I prevent Conda from activating the base environment by default? Get first line of a shell command's output Fixing a systemd service 203/EXEC failure (no such file or directory) /bin/sh: apt-get: not found VSCode Change Default Terminal Run bash command on jenkins pipeline How to check if the docker engine and a docker container are running? How to switch Python versions in Terminal?

Examples related to postgresql

Subtracting 1 day from a timestamp date pgadmin4 : postgresql application server could not be contacted. Psql could not connect to server: No such file or directory, 5432 error? How to persist data in a dockerized postgres database using volumes input file appears to be a text format dump. Please use psql Postgres: check if array field contains value? Add timestamp column with default NOW() for new rows only Can't connect to Postgresql on port 5432 How to insert current datetime in postgresql insert query Connecting to Postgresql in a docker container from outside

Examples related to psql

The Response content must be a string or object implementing __toString(), "boolean" given after move to psql psql: command not found Mac How to select a schema in postgres when using psql? "psql: could not connect to server: Connection refused" Error when connecting to remote database PostgreSQL: Why psql can't connect to server? How to show data in a table by using psql command line interface? Using psql how do I list extensions installed in a database? psql: FATAL: database "<user>" does not exist List tables in a PostgreSQL schema postgresql port confusion 5433 or 5432?