【问题标题】:Rails: FATAL - Peer authentication failed for user (PG::Error)Rails:致命 - 用户的对等身份验证失败(PG::Error)
【发布时间】:2012-04-16 17:56:20
【问题描述】:

我在 Ubuntu 11.10 和 RubyMine 上运行我的开发

这是我对 database.yml 的开发设置:RubyMine 为我创建的

development:
  adapter: postgresql
  encoding: unicode
  database: mydb_development
  pool: 5
  username: myuser
  password:

当我尝试运行应用程序时,我在下面收到此错误,似乎我还没有创建“项目”用户,但是,如何在 postgres 中创建用户并授予其数据库?如果这是问题所在,那么在 Ubuntu 中推荐使用什么工具来完成此任务?如果这不是问题,那么,请指教。

Exiting
/home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:1194:in `initialize': FATAL:  Peer authentication failed for user "project" (PG::Error)
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:1194:in `new'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:1194:in `connect'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:329:in `initialize'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:303:in `new_connection'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:313:in `checkout_new_connection'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:237:in `block (2 levels) in checkout'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:232:in `loop'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:232:in `block in checkout'
    from /home/sam/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:229:in `checkout'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:95:in `connection'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:398:in `retrieve_connection'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:168:in `retrieve_connection'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:142:in `connection'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/model_schema.rb:308:in `clear_cache!'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/railtie.rb:91:in `block (2 levels) in <class:Railtie>'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:418:in `_run__757346023__prepare__404863399__callbacks'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:405:in `__run_callback'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:385:in `_run_prepare_callbacks'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:81:in `run_callbacks'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/actionpack-3.2.3/lib/action_dispatch/middleware/reloader.rb:74:in `prepare!'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/actionpack-3.2.3/lib/action_dispatch/middleware/reloader.rb:48:in `prepare!'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/application/finisher.rb:47:in `block in <module:Finisher>'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/initializable.rb:30:in `instance_exec'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/initializable.rb:30:in `run'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/initializable.rb:54:in `each'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/initializable.rb:54:in `run_initializers'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/application.rb:136:in `initialize!'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /home/sam/RubymineProjects/project/config/environment.rb:5:in `<top (required)>'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `block in require'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
    from /home/sam/RubymineProjects/project/config.ru:4:in `block in <main>'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/rack-1.4.1/lib/rack/builder.rb:51:in `instance_eval'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/rack-1.4.1/lib/rack/builder.rb:51:in `initialize'
    from /home/sam/RubymineProjects/project/config.ru:1:in `new'
    from /home/sam/RubymineProjects/project/config.ru:1:in `<main>'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/rack-1.4.1/lib/rack/builder.rb:40:in `eval'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/rack-1.4.1/lib/rack/builder.rb:40:in `parse_file'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/rack-1.4.1/lib/rack/server.rb:200:in `app'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/commands/server.rb:46:in `app'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/rack-1.4.1/lib/rack/server.rb:301:in `wrapped_app'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/rack-1.4.1/lib/rack/server.rb:252:in `start'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/commands/server.rb:70:in `start'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/commands.rb:55:in `block in <top (required)>'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/commands.rb:50:in `tap'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/commands.rb:50:in `<top (required)>'
    from /home/sam/RubymineProjects/project/script/rails:6:in `require'
    from /home/sam/RubymineProjects/project/script/rails:6:in `<top (required)>'
    from -e:1:in `load'
    from -e:1:in `<main>'

Process finished with exit code 1

【问题讨论】:

    标签: ruby-on-rails postgresql ruby-on-rails-3 ruby-on-rails-3.1 ubuntu-11.10


    【解决方案1】:

    如果你在你的服务器上安装了 postresql,那么只需将 host: localhost 放到 database.yml,我通常把它扔到它说 pool: 5 的地方。否则,如果它不是 localhost,一定要告诉那个应用程序在哪里可以找到它的数据库。

    development:
      adapter: postgresql
      encoding: unicode
      database: kickrstack_development
      host: localhost
      pool: 5
      username: kickrstack
      password: secret
    

    通过创建数据库并将所有权分配给应用用户以建立连接,确保您的用户凭据设置正确。要在 postgresql 9 中创建新用户,请运行:

    sudo -u postgres psql
    

    如果你没有设置postgresql用户密码,它只是反斜杠密码。

    postgres=# \password
    

    创建新用户和密码以及用户的新数据库:

    postgres=# create user "guy_on_stackoverflow" with password 'keepitonthedl';
    postgres=# create database "dcaclab_development" owner "guy_on_stackoverflow"; 
    

    在您确认创建数据库、用户、密码并设置这些权限后,现在更新您的 database.yml 文件。不要忘记主机:localhost。

    【讨论】:

    • 如果您仍然无法通过 localhost 登录和完成连接,然后通过终端返回 postgresql 并应用这些 8.2 约定来建立您的连接用户的权限,语法是 GRANT ALL ON DATABASE [ dbname] 到 [usrname]。此语法在 psql 9+ 上同样适用,但您可能只能在用户拥有的数据库上切换权限,然后才会 GRANT ALL:postgresql.org/docs/8.2/static/sql-grant.html
    • host: localhostpool: 5 在我的配置中丢失。添加后,错误消失了。
    • 对我来说,host: localhost 不见了。在我添加之后,一切正常。这是在 Ubuntu 13.04
    • 强调也很重要:不要忘记分号
    • 我个人认为能够使用 rake db:setup 运行设置很重要。否则,您将在生成新的部署环境时遇到问题,但此描述确实适用于开发环境。
    【解决方案2】:

    这是让您的 Rails 应用在 Ubuntu 13.10 的开发环境中与 postgres 一起工作的最简单的方法。

    1) 在 Gemfile 中使用 postgres YAML 和 'pg' gem 创建 rails 应用程序:

    $ rails new my_application -d postgresql
    

    2) 给它一些 CRUD 功能。如果您只是查看 postgres 是否有效,请创建一个脚手架:

    $ rails g scaffold cats name:string age:integer colour:string
    

    3) 从rails 4.0.1 开始,-d postgresql 选项会生成不包含主机参数的 YAML。 我发现我需要这个。编辑开发部分并创建以下参数:

    encoding: UTF-8
    host: localhost
    database: my_application_development
    username: thisismynewusername
    password: thisismynewpassword 
    

    请注意,database 参数适用于尚未退出的数据库,usernamepassword 是也不存在的角色的凭据。我们稍后会创建它们!

    这就是config/database.yml 的外观(复制粘贴时不要感到羞耻:D):

    development:
      adapter: postgresql
      pool: 5
      # these are our new parameters
      encoding: UTF-8
      database: my_application_development
      host: localhost
      username: thisismynewusername
      password: thisismynewpassword
    
    test:
      # this won't work 
      adapter: postgresql
      encoding: unicode
      database: my_application_test
      pool: 5
      username: my_application
      password:
    
    production:
      # this won't work 
      adapter: postgresql
      encoding: unicode
      database: my_application_production
      pool: 5
      username: my_application
      password:
    

    4) 使用以下命令启动 postgres shell:

    $ psql
    

    4a) 如果您的当前用户(如您的计算机用户)没有相应的管理 postgres 角色,您可能会收到此错误。

    psql: FATAL:  role "your_username" does not exist
    

    现在我只安装了一次 postgres,所以我在这里可能错了,但我认为 postgres 会自动创建一个管理角色,其凭据与您安装 postgres 的用户相同。

    4b) 所以这意味着你需要更改为安装 postgres 的用户才能使用 psql 命令并启动 shell:

    $ sudo su postgres
    

    然后运行

    $ psql
    

    5) 你会知道你在 postgres shell 中,因为你的终端看起来像这样:

    $ psql
    psql (9.1.10)
    Type "help" for help.
    
    postgres=# 
    

    6) 使用 postgresql 语法,让我们创建我们在 config/database.ymldevelopment 部分中指定的用户:

    postgres=# CREATE ROLE thisismynewusername WITH LOGIN PASSWORD 'thisismynewpassword';
    

    现在,这里有一些微妙之处,让我们回顾一下。

    • 角色的用户名,thisismynewusername没有周围有任何引号
    • WITH 之后指定关键字 LOGIN。否则,仍会创建角色,但无法登录数据库!
    • 角色的密码 thisismynewpassword 需要用单引号括起来。 不是双引号
    • 在末尾添加一个分号 ;)

    你应该在你的终端中看到这个:

    postgres=#
    CREATE ROLE
    postgres=#
    

    这意味着“角色已创建”,但 postgres 的警报似乎采用了与 git hub 相同的命令式约定。

    7) 现在,仍然在 postgres shell 中,我们需要使用我们在 YAML 中设置的名称创建数据库。将我们在第 6 步中创建的用户设为所有者:

    postgres=# CREATE DATABASE my_application_development OWNER thisismynewusername;
    

    你会知道你是否成功,因为你会得到输出:

    CREATE DATABASE
    

    8) 退出 postgres shell:

    \q
    

    9) 现在是关键时刻:

    $ RAILS_ENV=development rake db:migrate
    

    如果你得到这个:

    ==  CreateCats: migrating =================================================
    -- create_table(:cats)
       -> 0.0028s
    ==  CreateCats: migrated (0.0028s) ========================================
    

    恭喜,postgres 与您的应用完美配合。

    9a) 在我的本地机器上,我不断收到权限错误。我记不太清楚了,但这是一个错误

    Can't access the files. Change permissions to 666.
    

    虽然我建议非常仔细地考虑在生产机器上递归设置写入权限,但在本地,我给了我的整个应用程序读写权限,如下所示:

    9b) 上一级目录:

    $ cd ..
    

    9c) 将 my_application 目录及其所有内容的权限设置为 666:

    $ chmod -R 0666 my_application
    

    9d) 再次运行迁移:

    $ RAILS_ENV=development rake db:migrate
    
    ==  CreateCats: migrating =================================================
    -- create_table(:cats)
       -> 0.0028s
    ==  CreateCats: migrated (0.0028s) ========================================
    

    如果你搞砸了一些提示和技巧

    在重新开始所有这些步骤之前尝试这些:

    mynewusername 用户没有权限对my_app_development 数据库进行CRUD?删除数据库并以 mynewusername 作为所有者再次创建它:

    1) 启动 postgres shell:

    $ psql
    

    2) 删除 my_app_development 数据库。当心!删除意味着彻底删除!

    postgres=# DROP DATABASE my_app_development;
    

    3) 重新创建另一个 my_app_development 并将 mynewusername 设为所有者:

    postgres=# CREATE DATABASE my_application_development OWNER mynewusername;
    

    4) 退出 shell:

    postgres=# \q
    

    mynewusername 用户无法登录数据库?认为您在 YAML 中输入了错误的密码并且记不起您使用 postgres shell 输入的密码?只需使用 YAML 密码更改角色:

    1) 打开您的 YAML,然后将密码复制到剪贴板:

     development:
          adapter: postgresql
          pool: 5
          # these are our new parameters
          encoding: UTF-8
          database: my_application_development
          host: localhost
          username: thisismynewusername
          password: musthavebeenverydrunkwheniwrotethis
    

    2) 启动 postgres shell:

    $ psql    
    

    3) 更新mynewusername 的密码。粘贴密码,记得用单引号括起来:

    postgres=# ALTER ROLE mynewusername PASSWORD `musthavebeenverydrunkwheniwrotethis`;
    

    4) 退出 shell:

    postgres=# \q
    

    试图通过数据库查看器(例如 Dbeaver)连接到 localhost,但不知道您的 postgres 用户的密码是什么?像这样改变它:

    1) 以超级用户身份运行passwd

    $ sudo passwd postgres
    

    2) 输入sudo 的账户密码(与postgres 无关):

    [sudo] password for starkers: myaccountpassword
    

    3) 创建 postgres 帐户的新密码:

    Enter new UNIX password: databasesarefun
    Retype new UNIX password: databasesarefun
    passwd: password updated successfully
    

    收到此错误消息?:

    Run `$ bin/rake db:create db:migrate` to create your database
    $ rake db:create db:migrate
    PG::InsufficientPrivilege: ERROR:  permission denied to create database
    

    4) 您需要让您的用户能够创建数据库。从 psql 外壳:

    ALTER ROLE thisismynewusername WITH CREATEDB
    

    【讨论】:

    • 我希望我能为这个出色的解决方案给你更多的 +1,如果它对我有用,我会更高兴。仍然出色的答案。快速提问,密码周围的反引号是不是在改变角色?另外,您不需要更早地使用反引号吗?
    • @MikeH-R 谢谢,可惜它不适合你!你确定你的相关 YAML 有host: localhost 吗?这对我来说是真正的问题。在密码周围你需要单引号。在我的键盘上是shift + @。使用 psql shell,您只需要在某些参数周围加上单引号。例如,您需要它们围绕密码,not 围绕角色名称。老实说,这非常繁琐,但是一旦你知道它的细微差别就可以了:)
    • @MikeH-R 我已经调整了我的答案以使事情更清楚。考虑再试一次!
    • 现在整理出来,再次感谢您的精彩回答,我遇到的大多数其他答案都是关于放松安全性,这在生产中显然没有好处。
    • 对于CREATE ROLECREATE DATABASE 部分,您也可以选择使用sudo -u postgres createuser -s {}; sudo -u postgres psql; postgres=# \password {}rake db:create
    【解决方案3】:

    永久解决方案:

    问题在于您的 pg_hba。这一行:

    local   all             postgres                                peer
    

    应该是

    local   all             postgres                                md5
    

    然后在更改此文件后重新启动您的 postgresql 服务器。

    如果你在 Linux 上,命令是

    sudo service postgresql restart
    

    【讨论】:

    • 谢谢!这是真正的答案。
    • 救大家一步,如果你想知道pg_hba在哪里:SHOW hba_file;
    【解决方案4】:

    我在 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 Alter user "username" set password 'xyz'; 后运行此命令来更改密码;
    • 请记住,md5 身份验证需要您的数据库用户的非空密码(今天刚刚在创建 Rails 应用程序时尝试最大限度地提高惰性)。
    【解决方案5】:

    您可以转到 /var/lib/pgsql/data/pg_hba.conf 文件并添加信任来代替 Ident 它对我有用。

    local   all all trust
    host    all 127.0.0.1/32    trust
    

    更多详情请参阅此问题 Ident authentication failed for user

    【讨论】:

    • Centos 6.6,试试:/var/lib/pgsql/9.2/data/pg_hba.conf
    【解决方案6】:

    添加host: localhost 对我来说很神奇

    development:
      adapter: postgresql
      database: database_name_here
      host: localhost
      username: user_name_here
    

    【讨论】:

      【解决方案7】:

      我在开发环境中工作时也遇到了同样的问题,问题是我在config/database.yml 文件中留下了host: localhost 注释掉了。

      所以我的应用程序无法连接到 PostgreSQL 数据库,只需取消注释即可解决问题。

      development:
        <<: *default
        database: database_name
      
        username: database_username 
      
        password: database_password
      
        host: localhost
      

      就是这样。

      我希望这会有所帮助

      【讨论】:

        【解决方案8】:

        如果您在运行单元测试时收到该错误消息 (Peer authentication failed for user (PG::Error)),请确保测试数据库存在。

        【讨论】:

          猜你喜欢
          • 2013-02-24
          • 2016-03-01
          • 2018-02-04
          • 2013-07-23
          • 2013-06-30
          • 2018-10-09
          • 2015-08-29
          • 2015-02-17
          • 2011-02-25
          相关资源
          最近更新 更多