I have installed PostgreSQL and it is working ok. However, when I went to restore a backup I got the开发者_StackOverflow社区 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?
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
.
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:
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.
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.
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
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/
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
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
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
精彩评论