Postgresql

為什麼“psql -U”對我不起作用?

  • May 31, 2016

我只是從 Postgres 開始。

這是在 Ubuntu 14.04 LTS 上全新安裝的 Postgres 9.5。我使用了來自: http ://www.postgresql.org/download/linux/ubuntu/的 apt repo

介紹頁面http://www.postgresql.org/docs/9.5/static/tutorial-createdb.html說“-U”應該可以工作。它沒有:

chris@blue:~$ psql -U postgres
psql: FATAL:  Peer authentication failed for user "postgres"

然而,如果我“su”到 postgres,一切都很好:

chris@blue:~$ sudo su - postgres
postgres@blue:~$ psql
psql (9.5.2)
Type "help" for help.

postgres=# 

我誤解了什麼?

psql -U 正在嘗試正常工作。但是,postgresql 嘗試對您進行身份驗證的方式失敗了。

當您 sudo 到 postgres 時,psql 命令會從您的 sudo’ed shell 中獲取您的身份。如果沒有“-U”,它會嘗試使用使用者 ID 並在角色列表中查找它。它說’嘿,客戶端在使用者 ID postgres 下執行!我們可以相信它!

在這兩種情況下,控制事物的是 pg_hba.conf 文件。它告訴 postgresql 信任名為“postgres”的本地使用者。但除此之外,它使用的是“對等”身份驗證。

使用“對等”身份驗證,它希望看到具有您姓名的數據庫使用者(“角色”),然後它將您作為該使用者進行身份驗證(僅!)。

引用自:https://serverfault.com/questions/775009