How to Fix psql Command Not Found Error in PostgreSQL?

PostgreSQL is a widely used relational database that supports various CLI and GUI tools. These tools assist us in managing and manipulating databases efficiently. The traditional way of working with Postgres is using a CLI tool.

SQL Shell aka “psql” is an interactive command line tool that helps us access PostgreSQL via the terminal. However, while accessing Postgres via psql, you may encounter the “psql command not found error”. The stated error arises because of various reasons.

This blog post will present a detailed guide on how to resolve the psql command not found error using one of the following fixes:

- Install Postgres
- Set Environment Variable

How to Fix psql Command Not Found Error in Postgres?

In PostgreSQL, the “psql command not found” error or the “psql” is not recognized as an internal or external command arises because of the following reasons:

- Postgres is not installed on the Machine.
- The Path for Postgres tools is not set on our system.

The stated error can be fixed either by installing PostgreSQL or by setting the environment variable for the Postgres tools.

Solution 1: Download and Install Postgres

The very first reason that causes this error is Postgres is not installed on the system. In that case, installing Postgres will rectify the stated problem.

Solution 2: Set Environment Variable

The primary reason which leads to the command not found error is that the path for the Postgres tools is not set on the system. In such a case, you can enforce one of the following solutions:

- Set Path Using Command Prompt
- Set Path Using Edit System Environment Variables

Setting Path Using Command Prompt

Open the windows search menu by pressing the “🪟 + S” button. Search CMD, and launch it as an administrator. Once the CMD terminal is open, execute the “setx” command to set the path for the Postgres tools using CMD:

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

In this command:

- “/M” is used to set the variable at the SYSTEM level/scope.
- “15” represents the Postgres version installed on our system. You must replace the with the Postgres version installed on your OS:

img

The Postgres bin directory’s path has been set successfully. For confirmation, you can re-launch the CMD and type any psql command:

img

The Postgres version confirms that the stated error has been rectified.

Setting Path Using Edit System Environment Variables

Open the “Edit the System Environment Variables” settings from the Windows search menu:

img

Click on the “Environment variables…” button to launch the system properties:

img

Select the path variable available under the system variables and hit the “Edit” button:

img

Now, copy the “bin directory’s path”, click on the “New” button, and paste the copied path here:

img

Click on the “OK” button to add the path to the “Edit Environment Variables” window. Next, hit the “OK” button to close the “Environment Variables” Window:

img

Next, close the “System Properties” Window by clicking on the “OK” button:

img

Setting the path for the Postgres tools will fix the "psql command not found" error.

Conclusion

In PostgreSQL, the “psql command not found” or “psql is not recognized as an internal or external command” error arises if Postgres is not installed or the path for Postgres tools is not set on your system. Installing Postgres or setting up the environment for Postgres will fix this error. This post explained a couple of solutions to fix the “psql command not found error” in PostgreSQL.