PG对等身份验证失败

时间:2013-03-09 03:24:58

标签: ruby-on-rails postgresql

我的用户密码与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

5 个答案:

答案 0 :(得分:91)

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

由于您的本地unix用户名为funkdified,并且您尝试以goodsounds指定local的unix域套接字(pg_hba.conf)连接作为用户peer进行连接{1}}身份验证,Pg正确拒绝您的连接尝试。

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

你可以:

  • 通过在数据库连接设置中指定主机名来连接TCP / IP;
  • 编辑pg_hba.conf使用md5密码验证而不是peer验证unix套接字(local连接类型),以便Pg接受密码验证;或
  • 使用与您的unix用户名相同的PostgreSQL用户名连接,并在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覆盖它。

答案 1 :(得分:16)

我在Ubuntu机器上遇到同样的问题,所以我按照一些步骤删除了这个错误。 切换到postgres用户

$ sudo su - postgres

它将要求输入密码,默认密码为postgres

将用户切换到postgres后,打开psql console

$ 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

答案 2 :(得分:10)

如果"对等身份验证#34;不起作用,请尝试md5身份验证。

要指定主机,请尝试以下操作:

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

或者这个:

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

答案 3 :(得分: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

答案 4 :(得分:0)

这是我的解决方法

运行以下命令以确认您的PostgreSQL版本。

psql --version

导航到服务器上的 PostgreSQL 配置目录,该目录位于/etc/postgresql/10/main中。请注意,10是我的服务器上 PostgreSQL 安装的版本。您的版本可能是9.51112或任何其他版本。

cd ~
cd /etc/postgresql/10/main

导航到/etc/postgresql/10/main目录后,使用以下命令打开文件pg_hba.conf。该文件控制:允许连接哪些主机,如何验证客户端,可以使用哪些PostgreSQL用户名,可以访问哪些数据库:

sudo nano pg_hba.conf

在下面替换以下行:

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

在下面的行:

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

此外,替换以下行:

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

在下面的行:

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

此外,我们可能希望允许进入生产环境中的PostgreSQL数据库的入站连接(以允许使用md5方法从所有数据库,所有用户,所有地址进行主机连接)。

要实现此目的,请在文件末尾添加以下行,然后保存文件:

# remote connections
host      all      all      all      md5

仍然在/etc/postgresql/10/main目录中,使用以下命令打开并编辑文件postgresql.conf

sudo nano postgresql.conf

用下面的行替换行# listen_address='127.0.0.1'或行listen_address='127.0.0.1'或行# listen_address='localhost'或行listen_address='localhost'到下面的行,以允许PostgreSQL数据库监听连接来自所有地址:

listen_addresses = '*'

保存文件,然后导航到服务器的根目录:

cd ~

使用以下命令重新启动或重新加载PostgreSQL服务器:

sudo systemctl restart postgresql       # To restart
sudo systemctl reload postgresql        # To reload
相关问题