PostgreSQL

PSQL Command Not Found

PSQL is the de-facto terminal-based utility for managing your PostgreSQL cluster. It is a simple, interactive, and intuitive tool that allows you to manage your PostgreSQL server using commands.

It also allows you to create batch commands, load them from a file, and then execute them on the server.

Needless to say, it is a great tool when working with PostgreSQL from the terminal.

This tutorial will discuss how to fix the “psql command not found” error on Windows, macOS, and Linux.

Installing PostgreSQL. – Windows

The first step is to ensure that you have PostgreSQL installed on your system.

On Windows: Start by opening your browser and navigating to the resource below:

https://www.enterprisedb.com/downloads/postgres-postgresql-downloads

Select the version for your Windows system and start the download.

Launch the installer package once the download is complete and follow along with the setup wizard.

Under the “Select Components” Section, ensure to select “Command Line Tools.”

Set your Data Directory, Password for your PostgreSQL cluster, and the running port.

Click Finish to finish the installation process.

Installing PostgreSQL – macOS

On macOS, open the browser and navigate to the link below:

https://www.enterprisedb.com/downloads/postgres-postgresql-downloads

Download the installer version for OSX.

Once the download is complete, mount the dmg file and launch the PostgreSQL app.

Follow along with the Wizard and set the appropriate options as you see fit.

Under “Select Components,” ensure to include “Command Line Tools.”.

And with that, you should have PostgreSQL installed on your system.

Installing PostgreSQL – Linux

To install PostgreSQL on Linux, navigate to the resource below, select your Linux version, and copy the installer script.

https://www.postgresql.org/download/

Paste the command in your terminal. It should install PostgreSQL.

Fix : Adding PATH for PostgreSQL Tools

Once you have installed PostgreSQL on your system, you must add the PostgreSQL bin directory to your PATH variable.

On Windows, launch the command prompt and enter the command:

setx /M path "%PATH%;C:\Program Files\PostgreSQL\14\bin"

In our case, the path to the PostgreSQL bin directory is

"C:\Program Files\PostgreSQL\14\bin"

If your installation is in a custom location, feel free to update it as you see fit.

If you are looking for a GUI option, open the Windows Search and enter “env.”

Select “Edit the System Environment Variables.”

Select the “Environment Variables” option in the “System Properties” window at the down-right corner.

Under the “System Variables” option, select the PATH option and click “Edit.”

Click on New and add the path to your PostgreSQL bin directory.

Click on Save to apply the changes.

Open the terminal and run the command:

$ psql --version

The command above should return the PostgreSQL version installed on your system.

psql --version
psql (PostgreSQL) 14.3

Fix: Add PostgreSQL Tools to Path – macOS

To add PostgreSQL tools to the PATH on macOS, edit your .zshrc or .bashrc file and add the following line:

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

Save the file and close. Apply the changes as:

source ~/.bashrc

Fix: Add PostgreSQL Tools to Path – Linux

If you installed PostgreSQL using the installer script in the provided instructions above, you should have the PostgreSQL tools added to the path.

However, if you don’t, add the following line to your .basrch file:

export PATH=$PATH:/var/lib/postgresql/[version]/

Replace the version with your installed PostgreSQL version.

Apply the changes;

$ source .bashrc

Closing

In this article, we dove deep into how you can fix the “psql command not found” on Windows, Linux, and macOS.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list