Linux (Fedora) 无法在终端中 运行 psql 命令 - 角色不存在

Linux (Fedora) Cannot run psql command in terminal - role does not exist

我已经在 Whosebug 上看到了所有关于类似主题的问题,但我想知道为什么会这样,问题出在哪里,以及如何正确设置它。我正在学习编码,所以对于任何误解,我深表歉意。请耐心等待我。

我的情况是 -

我无法从终端发出 运行 psql 命令。

回复是 -

psql: FATAL:  role "some_name" does not exist

在我写下并按回车后 -

sudo -u postgres -i

一切正常,我可以 运行 psql 命令。每次打开终端时,我都需要写 sudo -u postgres -i 命令,一遍又一遍。

非常感谢任何回复。

如果您有更多问题,我可以为您提供更多信息。

在这里查看一些附加信息:

[postgres@localhost ~]$ psql
psql (9.6.6)
Type "help" for help.

postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member
 of 
-----------+------------------------------------------------------------+-------
----
 matus     |                                                            | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

另一个:

[postgres@localhost ~]$ whoami
postgres

所以,我有两个打开的终端,一个是 postgres,另一个是 user_name,我可以在其中做其他事情,以某种方式使用 db。

根据你的描述,我想你在 hba 文件中有对等身份验证。要检查它,请使用 psql(您的 sudo -u postgres 方法)连接到数据库并:

t=# show hba_file ;
      hba_file
---------------------
 /pg/d10/pg_hba.conf
(1 row)
t=# \! grep local /pg/d10/pg_hba.conf | grep -v "#"
local   all             all                                     peer

如果您看到同行高于信任,或者只是同行,那么就是这种情况。来自手册:

https://www.postgresql.org/docs/current/static/auth-methods.html#AUTH-PEER

The peer authentication method works by obtaining the client's operating system user name from the kernel and using it as the allowed database user name (with optional user name mapping)

正在格式化我的。

所以为了避免

psql: FATAL: role "some_name" does not exist

只需创建这样的数据库用户:

create user some_name;

或在hba.conf

中将peer更改为trust