Psql command not found linux

Psql command not found linux

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"

— “/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:

Читайте также:  Удалить последние обновления linux

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.

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.

Источник

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:

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:

Читайте также:  Устанавливаем сетевой адаптер линукс

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.

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:

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

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:

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

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:

Save the file and close. Apply the changes as:

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:

Replace the version with your installed PostgreSQL version.

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

Читайте также:  Linux удалить часть файла

Источник

Postgresql -bash: psql: command not found

I have installed PostgreSQL and it is working ok. However, when I went to restore a backup I got the error -bash: psql: command not found :

 [root@server1 ~]# su postgres [postgres@server1 root]$ psql -f all.sql bash: psql: command not found [postgres@server1 root]$ 

This can happen on CentOS when you accidentally install PostgreSQL 8.4 (package postgresql-server ) after installing PostgreSQL 9.2 (package postgresql-server92 ) on the same machine. If you erase PostgreSQL 8.4, yum also removes the postgres bin directory from the path.

10 Answers 10

export PATH=/usr/pgsql-9.2/bin:$PATH 

The program executable psql is in the directory /usr/pgsql-9.2/bin , and that directory is not included in the path by default, so we have to tell our shell (terminal) program where to find psql . When most packages are installed, they are added to an existing path, such as /usr/local/bin , but not this program.

So we have to add the program’s path to the shell PATH variable if we do not want to have to type the complete path to the program every time we execute it.

This line should typically be added to theshell startup script, which for the bash shell will be in the file ~/.bashrc .

Would you be willing to expand this comment? It seems popular, but it could be much more useful with additional information (for example, where do the above lines go, and what do they do?)

If you are on Mac and installed Postgres.app, then «` export PATH=/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH «` or «` export PATH=/Applications/Postgres.app/Contents/Versions/9.6/bin:$PATH«`

perhaps psql isn’t in the PATH of the postgres user. Use the locate command to find where psql is and ensure that it’s path is in the PATH for the postgres user.

The question is for linux but I had the same issue with git bash on my Windows machine.

My pqsql is installed here: C:\Program Files\PostgreSQL\10\bin\psql.exe

You can add the location of psql.exe to your Path environment variable as described in this other answer, and shown in the screenshot below:

add psql.exe to your Path environment variable

After changing the above, please close all cmd and/or bash windows, and re-open them (as mentioned in the comments @Ayush Shankar). If you are using an IDE like Visual Studio Code, please close and re-open the entire IDE (as mentioned in the comments @Somraj Chowdhury)

You might need to change default logging user using below command.

Here postgres is the username. Without -U , it will pick the windows loggedin user.

Источник

Оцените статью
Adblock
detector