2013-03-09 75 views
23

我有密码匹配的database.ymlPG对等身份验证失败

postgres=# select * from pg_user 
; 
    usename | usesysid | usecreatedb | usesuper | usecatupd | userepl | passwd | valuntil | useconfig 
------------+----------+-------------+----------+-----------+---------+----------+----------+----------- 
goodsounds | 16386 | t   | t  | t   | t  | ******** |   | 
postgres |  10 | t   | t  | t   | t  | ******** |   | 
(2 rows) 

这指定的一个错误

[email protected] ~/rails_projects/goodsounds.org $ rake db:create 
FATAL: Peer authentication failed for user "goodsounds" 

这里用户是我的pg_hba.conf:

# Database administrative login by Unix domain socket 
local all    postgres        peer 

# TYPE DATABASE  USER   ADDRESS     METHOD 

# "local" is for Unix domain socket connections only 
local all    all          peer 
# IPv4 local connections: 
host all    all    127.0.0.1/32   trust 
# IPv6 local connections: 
host all    all    ::1/128     trust 
# Allow replication connections from localhost, by a user with the 
# replication privilege. 
#local replication  postgres        peer 
#host replication  postgres  127.0.0.1/32   trust 
#host replication  postgres  ::1/128     trust 

以前以前“信任”是md5,但我改变了看看是否有帮助。

这里是我的database.yml:

# PostgreSQL. Versions 8.2 and up are supported. 
# 
# Install the pg driver: 
# gem install pg 
# On Mac OS X with macports: 
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config 
# On Windows: 
# gem install pg 
#  Choose the win32 build. 
#  Install PostgreSQL and put its /bin directory on your path. 
# 
# Configure Using Gemfile 
# gem 'pg' 
# 
development: 
    adapter: postgresql 
    encoding: unicode 
    database: goodsounds_development 
    pool: 5 
    username: goodsounds 
    password: test 

    # Connect on a TCP socket. Omitted by default since the client uses a 
    # domain socket that doesn't need configuration. Windows does not have 
    # domain sockets, so uncomment these lines. 
    host: localhost 
    port: 5432 

    # Schema search path. The server defaults to $user,public 
    #schema_search_path: myapp,sharedapp,public 

    # Minimum log levels, in increasing order: 
    # debug5, debug4, debug3, debug2, debug1, 
    # log, notice, warning, error, fatal, and panic 
    # The server defaults to notice. 
    #min_messages: warning 

# Warning: The database defined as "test" will be erased and 
# re-generated from your development database when you run "rake". 
# Do not set this db to the same as development or production. 
test: 
    adapter: postgresql 
    encoding: unicode 
    database: goodsounds_test 
    pool: 5 
    username: goodsounds 
    password: test 

production: 
    adapter: postgresql 
    encoding: unicode 
    database: goodsounds_production 
    pool: 5 
    username: goodsounds 
    password: test 

回答

82

“对等身份验证”指的是它使用Unix套接字,并期待连接UNIX用户具有相同的UNIX用户名作为用户名的PostgreSQL。

由于本地UNIX用户名是funkdified和你试图通过Unix域套接字(local)连接,用户goodsounds连接您的pg_hba.conf指定peer认证,PG正确地拒绝你的连接尝试。

这是使用unix套接字时许多安装的默认行为。

您可以:

  • 通过TCP/IP连接在你的数据库连接设置指定主机名;
  • 编辑pg_hba.conf使用md5密码验证代替peer验证为unix套接字(local连接类型)所以Pg接受密码验证;或者
  • 连接PostgreSQL用户名与您的unix用户名相同,并在PostgreSQL中创建用户(如果它尚不存在)。

请参阅the docs for pg_hba.conf和其余的client authentication chapter of the documentation

请注意,对pg_hba.conf的更改不会立即生效,您必须重新启动或至少重新加载PostgreSQL以使其重新读取pg_hba.conf。噢,并且,如果你安装了多个PostgreSQL版本,你可能会有一个版本的libpq和另一个版本的服务器。在这种情况下,确保默认情况下libpq连接到的unix套接字的位置与服务器的unix_socket_directories相同,或者在连接字符串中使用(例如)host=/tmp覆盖它。

+2

如果我能给你更多的分数,我 将。谢谢Craig ... – Abram 2013-03-09 10:28:03

+1

'在数据库连接设置中指定主机名'是否意味着'将'host:localhost'添加到database.yml'中?因为那对我不起作用。 – Maarten 2013-09-29 07:42:15

+1

@MaarrenSep yep。如果它对您“不起作用”,请发布一个带有详细信息的新问题(版本,pg_hba.conf和database.yml内容,确切的错误消息等)。请链接回此内容并在此处添加评论,并提供指向您新问题的链接。 – 2013-10-06 05:42:55

7

如果“同行认证”不起作用,请尝试md5认证。

要指定主机尝试是这样的:

psql -d <dbname> -U <username> -h <hostname> 

或本:

psql -d <dbname> -U <username> -h <hostname> -W 
+0

这个问题与'psql'无关,因此指定'psql'命令行选项对于这个问题并不是有用的答案。这是问为什么'rake db:create'不能用看起来合理配置的Rails应用程序。 – 2017-02-16 00:36:01

9

我面临的Ubuntu机器上同样的问题,所以我通过以下一些步骤删除此错误。 切换到postgres用户

$ sudo su - postgres

它会询问密码,默认密码为postgres

切换用户Postgres的,开放的psql控制台后

$ psql 

所以检查版本postgres如果有多个版本可用

psql=# select VERSION(); 

PostgreSQL 9.1.13 on x86_64-unk....   # so version is 9.1 

现在打开postgres user

vim /etc/postgresql/9.1/main/pg_hba.conf

9.1是版本申报表上的命令

并更换

local all    postgres        peer 

local all    postgres        md5 

sudo service postgresql restart

我写步骤,在我的博客也

http://tarungarg402.blogspot.in/2014/10/set-up-postgresql-on-ubuntu.html

+0

这个问题与'psql'无关,因此指定'psql'命令行选项对于这个问题并不是一个有用的答案。这是问为什么'rake db:create'不能用看起来合理配置的Rails应用程序。 – 2017-02-16 00:37:25

1

编辑/etc/postgresql/9.3/main/pg_hba.conf

# "local" is for Unix domain socket connections only 
    local all    all          peer 

变化以下行,它的工作原理对我来说

# "local" is for Unix domain socket connections only 
local all    all          md5