简体   繁体   English

使用 postgres 角色运行 psql 命令

[英]Run psql command with postgres role

I am try to run psql command with postgres role.我正在尝试使用postgres角色运行psql命令。

(1) sudo psql -U postgres Since postgres differs from my OS username, I receive the Peer authentication failed for user "postgres" error when I run it. (1) sudo psql -U postgres由于postgres与我的操作系统用户名不同,我在运行时收到用户“postgres”错误的 Peer authentication failed

(2) But when I run sudo -u postgres psql , it succeeds (2) 但是当我运行sudo -u postgres psql时,它成功了

My terminal image我的终端形象

I'm not sure how the (2) commands can work because, according to a Postgres document, peer authentication happens automatically locally and my current OS username is different from postgres .我不确定 (2) 命令如何工作,因为根据 Postgres 文档,对等身份验证在本地自动发生,而我当前的操作系统用户名与postgres不同。 (I made no changes to pg_hba.config or pg_ident.config files.) (我没有更改 pg_hba.config 或 pg_ident.config 文件。)

And what is the difference between the (1) and the (2) command? (1) 和 (2) 命令有什么区别?

In your first attempt, you used sudo to become the root user (because there was no -u option) and tried to connect as database user postgres , which will fail with peer authentication because root is different from postgres .在您的第一次尝试中,您使用sudo成为root用户(因为没有-u选项)并尝试以数据库用户postgres的身份连接,这将因peer身份验证而失败,因为rootpostgres不同。

In your second attempt, you used sudo to become user postgres and called psql without specifying a username, so that the username defaults to the same as your current operating system user name, namely postgres .在您的第二次尝试中,您使用sudo成为用户postgres并在未指定用户名的情况下调用psql ,因此用户名默认与您当前的操作系统用户名相同,即postgres Then peer authentication works.然后peer身份验证起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM