Pet*_*ter 34 linux postgresql psql
我已经安装了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]$
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
小智 41
export PATH=/usr/pgsql-9.2/bin:$PATH
Run Code Online (Sandbox Code Playgroud)
程序可执行文件psql位于目录中/usr/pgsql-9.2/bin,默认情况下该目录不包含在路径中,因此我们必须告诉shell(终端)程序在哪里查找psql.安装大多数软件包时,会将它们添加到现有路径,例如/usr/local/bin但不是此程序.
因此,如果我们不想在每次执行时都必须输入程序的完整路径,那么我们必须将程序的路径添加到shell PATH变量中.
通常应将此行添加到shell启动脚本中,对于bash shell,该脚本将位于文件中~/.bashrc.
The*_*Pea 16
问题是针对 Linux 的,但我在 Windows 机器上使用 git bash 也遇到了同样的问题。
我的 pqsql 安装在这里:
C:\Program Files\PostgreSQL\10\bin\psql.exe
您可以将位置添加psql.exe到 Path 环境变量中,如另一个答案中所述,如下面的屏幕截图所示:
更改上述内容后,请关闭所有cmd和/或bash窗口,然后重新打开它们(如评论 @Ayush Shankar 中所述)。如果您使用的是 Visual Studio Code 等 IDE,请关闭并重新打开整个 IDE(如评论 @Somraj Chowdhury 中所述)
您可能需要使用以下命令更改默认日志记录用户。
psql -U postgres
Run Code Online (Sandbox Code Playgroud)
这postgres是用户名。如果没有-U,它将选择 Windows 登录用户。
Che*_*oor 10
这可能是由于 psql 不在 PATH 中
$ locate psql
/usr/lib/postgresql/9.6/bin/psql
Run Code Online (Sandbox Code Playgroud)
然后在 /usr/bin 中创建一个链接
ln -s /usr/lib/postgresql/9.6/bin/psql /usr/bin/psql
Run Code Online (Sandbox Code Playgroud)
然后尝试执行 psql 它应该可以工作。
如果您在Fedora或CentOS上运行它,这对我有用(PostgreSQL 9.6):
在终端:
$ sudo visudo -f /etc/sudoers
Run Code Online (Sandbox Code Playgroud)
修改以下文本:
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
Run Code Online (Sandbox Code Playgroud)
到
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/pgsql-9.6/bin
Run Code Online (Sandbox Code Playgroud)
退出,然后:
$ printenv PATH
$ sudo su postgres
$ psql
Run Code Online (Sandbox Code Playgroud)
要退出 postgreSQL 终端,您需要输入:
$ \q
Run Code Online (Sandbox Code Playgroud)
来源:https : //serverfault.com/questions/541847/why-doesnt-sudo-know-where-psql-is#comment623883_541880