Rails:致命-用户的对等身份验证失败(PG :: Error)


143

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

Answers:


315

如果您在服务器上安装了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。


2
如果您仍然无法登录并无法通过localhost完成连接,然后通过终端返回postgresql并应用一些8.2约定来建立您的连接用户权限,则语法为GRANT ALL ON DATABASE [dbname]到[usrname]。这种语法在psql 9+上同样有效,但是您可能只能切换用户拥有的数据库的权限,然后才能授予全部权限:postgresql.org/docs/8.2/static/sql-grant.html
Bent Cardan

38
host: localhost并且pool: 5在我的配置中丢失。添加它们后,错误消失了。
阿米特·帕特尔

21
对我来说host: localhost失踪了。添加完之后,一切正常。这是在Ubuntu 13.04中
Jesse

7
强调同样重要:不要忘记中场
马丁(Maarten)2013年

我个人认为使用rake db:setup运行安装程序很重要。否则,您将在生成新的部署环境时遇到问题,但是此描述确实适用于开发环境。
2014年

54

这是在Ubuntu 13.10 的开发环境中使Rails应用程序与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应该看起来的样子(copypasting: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外壳中,因为您的终端如下所示:

$ psql
psql (9.1.10)
Type "help" for help.

postgres=# 

6)使用PostgreSQL的语法,让我们创建我们指定的用户config/database.yml发展部分:

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 shell:

ALTER ROLE thisismynewusername WITH CREATEDB

1
希望我能为您提供更多+1的机会,来解决这个出色的解决方案,如果对我有用,我会更加高兴。仍然是出色的答案。简短的问题是,密码周围的那些反引号是否充当alter角色?另外,您不需要更早使用反引号吗?
Mike HR

@ MikeH-R谢谢,可惜它对您不起作用!您确定您的相关YAML拥有host: localhost吗?对我来说,那才是真正的陷阱。密码周围需要单引号。在我的键盘上是shift+ @。使用psql shell,您只需要在某些参数周围加上单引号即可。例如,您需要在密码附近而不是角色名称附近使用它们。说实话,这很奇怪,但是一旦您知道它的细微差别就可以了:)
Starkers

@ MikeH-R我调整了答案以使情况更清楚。考虑再解决一下!
Starkers

现在进行排序,再次感谢您提供了一个出色的答案,我遇到的其他大多数答案都涉及到放宽安全性,这显然在生产中没有用。
Mike HR

29

对于永久解决方案:

问题出在你的pg_hba上。这行:

local   all             postgres                                peer

应该

local   all             postgres                                md5

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

如果您使用的是Linux,则命令为

sudo service postgresql restart

1
谢谢!这是真正的答案。
skplunkerin

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


您给出了正确的答案,只错过了一件事,无需检查版本。您可以在psql Alter user“用户名”设置密码'xyz'之后通过运行此命令来更改密码。
vishu 2015年

请记住,md5身份验证需要您的数据库用户使用非空密码(今天,在创建Rails应用程序时尝试最大化懒惰的同时,请注意这一点)。
马克·莱顿·费舍尔

5

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

local   all all trust
host    all 127.0.0.1/32    trust

有关更多详细信息,请参阅此问题 。用户的身份验证失败


Centos 6.6,请尝试:/var/lib/pgsql/9.2/data/pg_hba.conf
Patrick

4

host: localhost给我的魔力

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

0

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


0

在开发环境中工作时,我也遇到了同样的问题,问题是我host: localhostconfig/database.yml文件中。

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

development:
  <<: *default
  database: database_name

  username: database_username 

  password: database_password

  host: localhost

就这样。

我希望这有帮助

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.