PG对等身份validation失败

我有一个用户密码与database.yml中指定的密码匹配

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) 

这是错误

 funkdified@vizio ~/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 

“对等身份validation”表示它使用的是unix套接字,并期望连接的unix用户具有与postgresql用户名相同的unix用户名。

由于您的本地unix用户名是funkdified并且您尝试通过unix域套接字( local )连接上的用户goodsounds连接,其中pg_hba.conf指定了peer身份validation,Pg正确拒绝您的连接尝试。

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

您可以:

  • 通过在数据库连接设置中指定主机名,通过TCP / IP连接;
  • 编辑pg_hba.conf以使用md5密码validation而不是对于unix套接字的peervalidation( local连接类型),因此Pg接受密码validation; 要么
  • 使用与您的unix用户名相同的PostgreSQL用户名连接,并在PostgreSQL中创建用户(如果它尚不存在)。

请参阅pg_hba.conf的文档以及文档的其余客户端身份validation章节 。

请注意,对pg_hba.conf更改不会立即生效,您必须重新启动或至少重新加载PostgreSQL才能重新读取pg_hba.conf


哦,如果你安装了多个PostgreSQL版本,你可能有一个版本的libpq和另一个版本的服务器。 在这种情况下,请确保默认情况下libpq连接到的unix套接字的位置与服务器的unix_socket_directories相同,或者在连接字符串中使用(例如) host=/tmp覆盖它。

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

如果“对等身份validation”不起作用,请尝试md5 authenticnticaion。

要指定主机尝试这样的事情:

  psql -d  -U  -h  

或这个:

  psql -d  -U  -h  -W 

编辑/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