我已经在我的Ubuntu karma box上安装了PostgreSQL和pgAdminIII。

我能够成功地使用pgAdminIII(即连接/登录),但是当我尝试在命令行(使用psql)上使用相同的用户名/pwd登录到服务器时,我得到错误:

psql: FATAL:  Ident authentication failed for user "postgres"

现在有人知道如何解决这个问题吗?


当前回答

在以上所有的答案中,没有一个对我有用。我不得不手动更改数据库中的用户密码,它突然工作了。

psql -U postgres -d postgres -c "alter user produser with password 'produser';"

我使用了以下设置:

pg_hba.conf

local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            password  
# IPv6 local connections:
host    all             all             ::1/128                 password

以下命令连接成功:

psql -U produser -d dbname -h localhost -W 

其他回答

我有类似的问题,我在pg_hba.conf中修复了它,当删除所有识别方法时,即使是IP6地址(尽管我在机器上只有IP4)。

host all all 127.0.0.1/32 password
host all all ::1/128 password
#for pgAdmin running at local network
host all all 192.168.0.0/24 md5

可以设置环境变量PGHOST=localhost:

$ psql -U db_user db_name
psql: FATAL:  Peer authentication failed for user "db_user"

$ export PGHOST=localhost
$ psql -U db_user db_name

Password for user mfonline:

嗯…

如果可以使用pgAdminIII中的用户名和密码连接,但不能使用psql连接,那么这两个程序连接到数据库的方式可能不同。

[如果您连接到不同的数据库,首先尝试连接到相同的数据库。见下文。)

From PostgreSQL: Documentation: 9.3: psql:

如果省略主机名,psql将通过unix域套接字连接到本地主机上的服务器,或者在没有unix域套接字的机器上通过TCP/IP连接到localhost。

如果你没有运行类似psql的程序…-h host_name…如果你运行的是Ubuntu, psql应该通过unix域套接字连接,所以PostgreSQL可能没有配置为允许postgres用户的密码验证方法之一。

你可以通过运行:

sudo -u postgres psql

如果上述工作,您的服务器可能被配置为使用postgres用户对本地连接进行对等身份验证,即请求操作系统提供您的用户名以确认您是postgres。

所以这可能是你的pg_hba.conf文件

该文件的完整路径将类似于/etc/postgresql/9.3/main/pg_hba.conf。可以通过sudo cat /etc/postgresql/9.3/main/pg_hba.conf | more查看。

如果你在psql命令中忽略了主机名,如果你在pg_hba.conf文件中添加以下条目,你应该能够连接:

# Connection type   Database   User       IP addresses   Method
local               all        postgres                  md5

[pg_hba.conf文件中的注释行以#开头。]

如果你在psql命令中包含主机名,添加以下条目:

# Connection type   Database   User       IP addresses   Method
host                all        postgres   127.0.0.1/32   md5

您需要在通过psql为您的连接匹配任何其他条目之前放置该条目。如果不知道该把它放在哪里,就把它放在第一个未注释的行之前。

更多关于pg_hba.conf的信息

pg_hba.conf文件[粗体强调]:

具有匹配的连接类型、客户端地址、请求的数据库和用户名的第一条记录用于执行身份验证。没有“失败”或“备份”:如果选择了一条记录并且认证失败,则不考虑后续记录。如果没有匹配的记录,则拒绝访问。

请注意,在认证方法上没有匹配记录。所以,如果你的pg_hba.conf文件包含以下条目:

# Connection type   Database   User       IP addresses   Method
local               all        postgres                  peer

那么你将无法通过以下方式连接:

psql -u postgres

除非你的pg_hba.conf文件中有一个条目在前一个条目的上面:

# Connection type   Database   User       IP addresses   Method
local               all        postgres                  md5
local               all        postgres                  password   # Unencrypted!
local               all        all                       md5
local               all        all                       password   # Unencrypted!

我在GCP SQL中通过terraform提供了用户名和密码,问题是密码没有通过terraform正确设置,所以虽然不是一个适当的修复,但只是为了找出确切的原因。

我从GCP控制台更改了用户的密码,这是有效的。

我在Mac OSX的PostgreSQL 9.3上的解决方案是在bash shell中使用sudo进入数据文件夹,然后将必要的行追加到pg_hba.conf文件,以允许所有用户都是可信的,并能够登录。这就是我所做的:

# in bash_profile edit PGDATA environmental variable
open ~/.bash_profile

# append this line to bash_profile
export PGDATA="/Library/PostgreSQL/9.3/data"

# reload bash_profile
source ~/.bash_profile

# open pg_hba.conf in vim
sudo vi /Library/PostgreSQL/9.3/data/pg_hba.conf

# append these two lines to the end of the pg_hba.conf file
local   all   all                  trust
host    all   all   127.0.0.1/32   trust

# can now login as user in bash
psql -d <db_name> -U <user_name> -W