psql: command not found Mac – Psql

Photo of author
Written By M Ibrahim
bash macos postgresql psql

Quick Fix: Set PATH variable correctly to point to the containing directory of psql on your Mac. The PATH should be /Library/PostgreSQL/9.5/bin:$PATH, ensuring there are no spaces around the equals sign.

The Problem:

A user downloaded Postgres and installed it on their Mac through the graphical installer, but when they try to run "psql" in the terminal, they get a "command not found" error. They have added the correct path to psql in their ".bash_profile" but it still doesn’t work. They also tried adding it to ".bashrc" but that resulted in an error.

The Solutions:

Solution 1: Correct the PATH Variable

To fix the “psql: command not found” error on Mac, you need to ensure that the PATH variable in your shell configuration file (.bash_profile or .bashrc) includes the directory containing the psql executable. Follow these steps:

  1. Locate the psql Executable Directory:

    • Open a terminal window.
    • Run the following command to find the directory where the psql executable is located:
      locate psql | grep /bin
      
    • This command should return a path similar to "/Library/PostgreSQL/9.5/bin".
  2. Edit Your Shell Configuration File:

    • Open your shell configuration file using a text editor. For Bash users, it’s typically ~/.bash_profile or ~/.bashrc.
    • Locate the line that starts with "export PATH=". This line defines your PATH variable.
  3. Add the PostgreSQL Bin Directory to the PATH:

    • Add the directory containing the psql executable to the PATH variable. Use the following syntax:
      export PATH=/path/to/psql:$PATH
      
    • Replace "/path/to/psql" with the directory you found in step 1. For example:
      export PATH=/Library/PostgreSQL/9.5/bin:$PATH
      
    • Make sure there are no spaces around the equals sign (=).
  4. Save and Close the Configuration File:

    • Save the changes to your shell configuration file.
  5. Restart Your Terminal:

    • Close and reopen your terminal window. This will load the updated PATH variable.
  6. Verify the PATH Setting:

    • Run the following command to check if the PATH variable includes the PostgreSQL bin directory:
      echo $PATH
      
    • You should see the PostgreSQL bin directory listed in the output.

Now, you should be able to use the psql command in your terminal without encountering the "command not found" error.

Solution 2: Installing Postgres App

To resolve the issue of "psql: command not found" in Mac terminal, follow these steps:

  1. Install the Postgres App from the official website.
  2. Once installed, open Terminal and run the following command:
sudo mkdir -p /etc/paths.d && \
echo /Applications/Postgres.app/Contents/Versions/latest/bin \
| sudo tee /etc/paths.d/postgresapp
  1. Restart your Terminal.

After completing these steps, you should be able to use the psql command in your Terminal without encountering the "command not found" error.

Solution 3: Manually add to bash profile

You can manually add the path to the PostgreSQL bin directory to your bash profile or zshrc file.

  1. Navigate to your home directory using the command cd ~.
  2. Open your bash profile using the command open .bash_profile or your zshrc file using the command open .zshrc if you are using zsh.
  3. In the bash profile or zshrc file, add the following line:
  4. # Postgres
    export PATH=/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH
    
  5. Save the file.
  6. Restart the terminal.
  7. Type psql. You should now be able to use the psql command.

Solution 4: Add PostgreSQL’s path to the environment variables

If you used homebrew with Mojave or a later version of macOS, you can resolve this issue by adding PostgreSQL’s path to your environment variables. Here’s how to do it:

  1. Open Terminal on your Mac.

  2. Run the following command to add PostgreSQL’s path to your $PATH environment variable:

    export PATH=/usr/local/opt/[email protected]/bin:$PATH
    
  3. Change the 9.5 in the command to match the version of PostgreSQL you installed.

  4. Restart your Terminal window or run the following command to apply the changes immediately:

    source ~/.bash_profile
    

Now, you should be able to run psql from the Terminal without encountering the "command not found" error.

Solution 5: Get psql in your path

If you used brew to install Postgres, you need to link the psql executable to your path. To do this, run the following command:

brew link [email protected] --force

Replace 9.6 with the version of Postgres you installed. This will create a symbolic link from /usr/local/bin/psql to the actual location of the psql executable.

Once you’ve done this, you should be able to run psql from the command line.