Iai*_*der 4 postgresql centos sudo
该CKAN安装文档展示了如何列出已安装PostgreSQL数据库。
该命令如下所示:
sudo -u postgres psql -l
Run Code Online (Sandbox Code Playgroud)
当我在我的 shell 中尝试时,我收到一个错误:
$ sudo -u postgres psql -l
sudo: psql: command not found
Run Code Online (Sandbox Code Playgroud)
CentOS 论坛上的 Daniel2d2art通过完全限定 psql 的路径解决了这个问题。
我的 psql 位于目录中/usr/pgsql-9.2/bin
,所以我的解决方法现在看起来像这样:
sudo -u postgres /usr/pgsql-9.2/bin/psql -l
Run Code Online (Sandbox Code Playgroud)
当我在我的 shell 中尝试时,它可以工作:
$ sudo -u postgres /usr/pgsql-9.2/bin/psql -l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
--------------+----------+----------+-------------+-------------+-----------------------
postgis_test | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
Run Code Online (Sandbox Code Playgroud)
我不应该完全限定路径,对吗?
postgres 用户的路径中已经有 psql:
$ sudo -u postgres echo $PATH
/usr/pgsql-9.2/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
Run Code Online (Sandbox Code Playgroud)
我该如何正确解决这个问题?
sudo 不使用您的用户路径或 postgres 用户路径。sudo 有自己的路径,由secure_path
文件中的变量定义/etc/sudoers
。
echo $PATH
在这种情况下,的输出具有误导性。要查看 sudo 真正使用的路径,请printenv PATH
改用。就我而言,输出如下所示:
$ sudo -u postgres printenv PATH
/sbin:/bin:/usr/sbin:/usr/bin
Run Code Online (Sandbox Code Playgroud)
输出不包含/usr/pgsql-9.2/bin
, psql 所在的位置,因此它也不在 sudo 的路径中。
要解决此问题,您可以将 psql 所在的位置添加到secure_path
变量中。
用于在 vi 中sudo visudo
打开/etc/sudoers
。
该文件应包含如下一行:
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
Run Code Online (Sandbox Code Playgroud)
此行设置 sudo 的路径。等号后面的部分与上一个printenv PATH
示例的输出相同。
用这样的东西替换它:
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/pgsql-9.2/bin
Run Code Online (Sandbox Code Playgroud)
替换附加/usr/pgsql-9.2/bin
到路径列表。路径列表分隔符是一个冒号 ( :
)。
保存并关闭文件。
要检查它是否有效,请printenv PATH
再次尝试该命令:
$ sudo -u postgres printenv PATH
/sbin:/bin:/usr/sbin:/usr/bin:/usr/pgsql-9.2/bin
Run Code Online (Sandbox Code Playgroud)
看起来挺好的!
现在尝试psql -l
命令:
$ sudo -u postgres psql -l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
--------------+----------+----------+-------------+-------------+-----------------------
postgis_test | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
Run Code Online (Sandbox Code Playgroud)
有用!
感谢Drew Khoury,他为我指出了Super User上类似问题的解决方案。
归档时间: |
|
查看次数: |
14479 次 |
最近记录: |