CKAN安装文档显示如何列出已安装的PostgreSQL数据库。
该命令如下所示:
sudo -u postgres psql -l
当我尝试在我的shell中,我得到一个错误:
$ sudo -u postgres psql -l sudo: psql: command not found
在CentOS论坛上的Daniel2d2art通过完全限定psql的path来解决这个问题。
我的psql位于目录/usr/pgsql-9.2/bin ,所以我的解决方法现在看起来像这样:
sudo -u postgres /usr/pgsql-9.2/bin/psql -l
当我尝试在我的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)
我不应该完全限定path,对吧?
postgres用户的path中已经有了psql:
$ sudo -u postgres echo $PATH /usr/pgsql-9.2/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
我如何解决这个问题呢?
sudo不使用你的用户path或postgres用户的path。 sudo有自己的path,由/etc/sudoers文件中的secure_pathvariables定义。
echo $PATH的输出在这种情况下是误导性的。 要查看sudo真正使用的path,请改为使用printenv PATH 。 在我的情况下,输出如下所示:
$ sudo -u postgres printenv PATH /sbin:/bin:/usr/sbin:/usr/bin
输出不包含psql所在的/usr/pgsql-9.2/bin ,所以它不在sudo的path中。
要解决这个问题,你可以添加psql所在的地方到secure_pathvariables。
使用sudo visudo在vi中打开/etc/sudoers 。
该文件应该包含这样一行:
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
这一行设置了sudo的path。 等号后的部分与之前的printenv PATH示例的输出相同。
把它replace成这样的东西:
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/pgsql-9.2/bin
replace将/usr/pgsql-9.2/bin附加到path列表中。 path列表分隔符是冒号(:)。
保存并closures文件。
要检查它是否工作, printenv PATH再次尝试使用printenv PATH命令:
$ sudo -u postgres printenv PATH /sbin:/bin:/usr/sbin:/usr/bin:/usr/pgsql-9.2/bin
看起来不错!
现在尝试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)
有用!
感谢Drew Khoury指出我在Super User上遇到类似问题的解决scheme。
一个小的解决方法 – 如果你没有执行更多命令的问题,你可以切换用户
sudo su postgres
接着
psql -l
应该pipe用。