Linux Postgresql -bash: psql: 命令未找到

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6790088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:12:49  来源:igfitidea点击:

Postgresql -bash: psql: command not found

linuxpostgresqlpsql

提问by Peter

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:

我已经安装了 PostgreSQL,它工作正常。但是,当我去恢复备份时出现错误-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?

我做错了什么?

采纳答案by marto

perhaps psql isn't in the PATHof the postgres user. Use the locatecommand to find where psql is and ensure that it's path is in the PATHfor the postgres user.

也许 psql 不在PATHpostgres 用户中。使用locate命令查找 psql 所在的位置,并确保它的路径在PATHpostgres 用户的路径中。

回答by Pavel

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

The program executable psqlis 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.

程序可执行文件psql在 目录中/usr/pgsql-9.2/bin,默认情况下该目录不包含在路径中,因此我们必须告诉我们的 shell(终端)程序在哪里找到psql. 安装大多数软件包后,它们会添加到现有路径中,例如/usr/local/bin,但不会添加到此程序中。

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.

因此,如果我们不想在每次执行程序时都输入程序的完整路径,我们必须将程序的路径添加到 shell PATH 变量中。

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

此行通常应添加到 shell 启动脚本中,对于 bash shell,该脚本将位于文件~/.bashrc.

回答by Daniel Morris

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.

如果您使用 Postgres Mac 应用程序(由 Heroku 开发)和 Bundler,您可以将 pg_config 直接添加到应用程序中,添加到您的包中。

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/

回答by aelkz

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

如果您在FedoraCentOS上运行它,这对我有用(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:

要退出 postgreSQL 终端,您需要输入:

$ \q

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

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

回答by The Red Pea

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

问题是针对 linux 的,但我在 Windows 机器上使用 git bash 遇到了同样的问题。

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

我的 pqsql 安装在这里: C:\Program Files\PostgreSQL\10\bin\psql.exe

You can add the location of psql.exeto your Path environment variable as shown in this screenshot:

您可以将 的位置添加psql.exe到您的 Path 环境变量中,如下面的屏幕截图所示:

add psql.exe to your Path environment variable

将 psql.exe 添加到您的 Path 环境变量

回答by Chetan kapoor

It can be due to psql not being in PATH

这可能是由于 psql 不在 PATH 中

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

Then create a link in /usr/bin

然后在 /usr/bin 中创建一个链接

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

Then try to execute psql it should work.

然后尝试执行 psql 它应该可以工作。