Peter
Peter

Reputation: 885

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]$ 

What have I done wrong?

Upvotes: 71

Views: 324998

Answers (11)

After doing an installation on Centos 7.9, I encountered this problem. My finding yielded the following:

  • Installation on postgres went to to directory /usr/pgsql-15
  • Perfoming echo $PATH yielded /sbin:/bin:/usr/sbin:/usr/bin

From the output of path above you realise postgresql installation directory is not in PATH.

Resolution

  • Add /usr/pgsql-15 to PATH on your ~/.basr_profile user file as below:

    PATH=$PATH:/usr/pgsql-15/bin

    export PATH

Upvotes: 0

Damirqa
Damirqa

Reputation: 1

Run pg-wrapper as root to make the installed client and server programs available via PATH and add SQL man pages to the man page configuration file. This utility is provided in the postgrespro-std-14-client package.

/opt/pgpro/std-14/bin/pg-wrapper links update

For details on how to handle possible conflicts, see this description.

Upvotes: 0

Ayer
Ayer

Reputation: 140

Sometimes we face this issue when the gem installation command doesn't find the pg client library for various reasons, such as if psql is not in the path.

In those cases, providing the command with the path to pg_config may fix the issue.

gem install pg -v 1.3.5 -- --with-pg-config=/path/to/pg_config

In my case, I faced a similar issue when I installed postgresql@12 with Homebrew in the Rosetta environment.

Following command solved the issue in my case.

gem install pg -v 1.3.5 -- --with-pg-config=/usr/local/Homebrew/Cellar/postgresql@12/12.13/bin/pg_config

Upvotes: 0

Nate Anderson
Nate Anderson

Reputation: 21216

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.

psql -U postgres

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

Upvotes: 19

Serjio
Serjio

Reputation: 11

there must be two reasons for this either the package is not install or psql is not defined in the PATH

the simple way is to create a link within the /usr/bin or /usr/local/sbin/

First find the the file

sudo find / -name psql

then create soft link

sudo ln -sf /opt/pgpro/1c-14/bin/psql /usr/local/sbin/psql

Upvotes: 1

officialrahulmandal
officialrahulmandal

Reputation: 3128

Check if PostgreSQL is installed or not.

If not you can do the same in ubuntu using this command.

sudo apt update
sudo apt install postgresql postgresql-contrib

Upvotes: 2

Chetan kapoor
Chetan kapoor

Reputation: 855

It can be due to psql not being in PATH

$ locate psql
/usr/lib/postgresql/9.6/bin/psql

Then create a link in /usr/bin

ln -s /usr/lib/postgresql/9.6/bin/psql /usr/bin/psql

Then try to execute psql it should work.

Upvotes: 13

aelkz
aelkz

Reputation: 1875

In case you are running it on Fedora or CentOS, this is what worked for me (PostgreSQL 9.6):

In terminal:

$ sudo visudo -f /etc/sudoers

modify the following text from:

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

to

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/pgsql-9.6/bin

exit, then:

$ printenv PATH

$ sudo su postgres

$ psql

To exit postgreSQL terminal, you need to digit:

$ \q

Source: https://serverfault.com/questions/541847/why-doesnt-sudo-know-where-psql-is#comment623883_541880

Upvotes: 5

marto
marto

Reputation: 4503

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.

Upvotes: 29

Pavel
Pavel

Reputation: 641

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.

Upvotes: 54

Daniel Morris
Daniel Morris

Reputation: 6892

If you are using the Postgres Mac app (by Heroku) and Bundler, you can add the pg_config directly inside the app, to your bundle.

bundle config build.pg --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config

...then run bundle again.

Note: check the version first using the following.

ls /Applications/Postgres.app/Contents/Versions/

Upvotes: 1

Related Questions