【问题标题】:PostgreSQL: FATAL - Peer authentication failed for user (PG::ConnectionBad)PostgreSQL:致命 - 用户的对等身份验证失败 (PG::ConnectionBad)
【发布时间】:2013-02-24 18:02:15
【问题描述】:

我正在使用 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)

但是当我尝试通过运行命令来创建数据库时

rails db:create

我得到了错误

致命:用户“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

【问题讨论】:

    标签: ruby-on-rails postgresql


    【解决方案1】:

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

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

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

    你可以:

    • 通过在数据库连接设置中指定主机名通过 TCP/IP 进行连接;
    • 编辑pg_hba.conf 以使用md5 密码身份验证而不是peer 身份验证用于unix 套接字(local 连接类型),因此 Pg 接受密码身份验证;或
    • 使用与您的 unix 用户名相同的 PostgreSQL 用户名进行连接,如果该用户尚不存在,则在 PostgreSQL 中创建该用户。

    参见the docs for pg_hba.confclient authentication chapter of the documentation 的其余部分。

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


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

    【讨论】:

    • “在数据库连接设置中指定主机名”是否意味着“将 host: localhost 添加到 database.yml”?因为那对我不起作用。
    • @MaarrenSep 是的。如果它对您“不起作用”,请发布一个带有详细信息的新问题(版本、pg_hba.conf 和 database.yml 内容、确切的错误消息等)。链接回此内容以获取内容,并在此处添加带有指向您的新问题的链接的评论。
    • 当我将“host:'localhost'”添加到我的连接设置时,它解决了我的问题。谢谢!
    • sudo -u postgres psql dbname 将是对等方法的一种方式,其中postgres 是相关用户。
    • 第三点很容易被忽视,但却很重要。一旦我创建了一个与我的 Linux 用户名匹配的 Postgres 用户名,然后在我的 rails 应用程序中设置它,就解决了我的问题。那时我不必添加用户名或密码条目。
    【解决方案2】:

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

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

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

    或者这个:

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

    【讨论】:

    • 该问题与psql 无关,因此指定psql 命令行选项不是该问题的有用答案。这是在问为什么 rake db:create 不能与看似合理配置的 Rails 应用程序一起工作。
    【解决方案3】:

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

    【讨论】:

    • 该问题与psql 无关,因此指定psql 命令行选项不是该问题的有用答案。这是在问为什么rake db:create 不能与看似合理配置的 Rails 应用程序一起工作。
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      我是这样解决的

      运行以下命令以确认您的 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
      

      【讨论】:

        猜你喜欢
        • 2018-02-04
        • 2012-04-16
        • 2013-07-23
        • 1970-01-01
        • 2018-10-09
        • 2016-03-01
        • 2013-06-30
        • 2020-11-30
        • 1970-01-01
        相关资源
        最近更新 更多