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

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

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

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


当前回答

我只需要简单地添加-h localhost位就可以工作了

其他回答

以防以上方法都不适合你: 我已经安装了不少Postgres,但今天在RedHat 6.5系统上(安装Postgres 9.3)却被搞糊涂了。Aron在上面展示的典型hba.conf配置不起作用。原来我的系统使用的是IPV6,而忽略了IPV4配置。添加一行:

host    all             all             ::1/128                 password

允许我成功登录。

这招对我很管用: http://tecadmin.net/fatal-ident-authentication-failed-for-user-postgres/#

local   all             postgres                                trust
local   all             myapp_usr                               trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
#host    all             all             ::1/128                 trust

我在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

编辑/etc/postgresql/8.4/main/pg_hba.conf文件,并将ident或peer替换为md5或trust,这取决于您是否希望它在您自己的计算机上询问密码。 然后重新加载配置文件:

/etc/init.d/postgresql reload

嗯…

如果可以使用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!