[Solved] psql: Command Not Found


In Linux, psql is a command-line interface for interacting with PostgreSQL databases. It allows users to execute SQL queries, manage database connections, and perform various administrative tasks efficiently from the terminal.

The error “psql: command not found” is thrown when the psql command is not recognized or available in the system’s PATH. This typically occurs when PostgreSQL or its client tools are not installed or when the command is not accessible due to incorrect permissions or misconfiguration.

This psql command not found error can occur on any Linux distribution where you are using the psql command line interface. This post will elaborate on the possible reasons and the solutions to eliminate the error “psql command not found” on Linux.

Note: The commands practiced in this guide refer to all the Linux distributions. However, we will show a practical demonstration of the commands on Ubuntu 22.04.

How to Fix the “bash: psql: command not found” Error on Linux?

There might be several reasons that invoke the “psql: command not found” error on Linux. The possible reasons are as follows:

PostgreSQL not installed

The error may occur if PostgreSQL is not installed on the system. psql is part of the PostgreSQL client tools, so it requires PostgreSQL to be installed separately.

Incorrect PATH Configuration

If the system’s PATH environment variable is not configured correctly, the shell may be unable to locate the psql command. The PATH should include the directory where psql is installed.

Missing or Incomplete PostgreSQL Installation

The command will not be found if PostgreSQL is installed but the client tools, including psql, are missing or not installed properly. This can happen if installation errors or only a partial installation is performed.

Insufficient Permissions

The user executing the command may need more permissions to access the psql command or the directory where it is located. This can occur if the command is restricted to certain users or if the permissions on the executable file are not set correctly.

The error on the terminal looks as follows:

Solution 1: Install Postgres

The first and foremost solution to eliminate the error is to install Postgres on your Linux system. The command depends on the Linux distribution you are using. The following commands can be used as per your Linux distribution to install Postgres:

$ sudo apt install postgresql #Debian/Ubuntu
$ sudo yum install postgresql-server postgresql-contrib #CentOS/RHEL
$ sudo dnf install postgresql-server postgresql-contrib #Fedora-Based
$ sudo pacman -Sy postgresql #Arch Linux

Although the successful execution of the command ensures the installation of the package. However, you might be unable to execute the psql command because the current user will not be recognized. So, switch to the postgres user via the command:

$ sudo -i -u postgres


Once switched, use the following command to ensure the command is working fine:

$ psql

Solution 2: Add the Postgres Path

If you have installed Postgres and are still getting the error, you might need to add the Postgres path to the system’s environment. This will enable your system to recognize the Postgres installed on your system. Let’s understand it through the steps:

Step 1: Check the Installation of the psql

First, you must check the path where the psql executable is installed on your system. This can be done using the which command as follows:

$ which psql

Step 2: Add the Path

Use the above path (received in the above output) and add it to the bashrc file. For this, open the bashrc file using the nano editor as follows:

$ sudo nano ~/.bashrc

Add the following line at the end of the file and then save the file.

export PATH="usr/bin:$PATH"

Step 3: Refresh the .bashrc File

To use psql in the same terminal, you have to refresh the .bashrc file using the source command as follows:

$ source ~/.bashrc


Note: You can bypass the above command by starting a new terminal.

Now, verify whether psql command is working or not. This method will resolve the temporary hold on the psql command put by the system.

Wrap Up

The “psql: command not found” error on Linux can be resolved by taking the following steps. Firstly, ensure that PostgreSQL is installed on the system using the appropriate installation command for the Linux distribution. Secondly, verify the PATH configuration to include the directory where the psql executable is installed. If necessary, modify the system’s environment variables to add the Postgres path. By installing Postgres and correctly configuring the PATH, users can eliminate the “psql: command not found” error and successfully use the psql command on Linux. You can get hands-on troubleshooting guides at Linux Genie.

Print Friendly, PDF & Email
Categories
Tags