Rails 3.2,FATAL:用户对等身份validation失败(PG :: Error)

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

这里是我的database.yml的开发设置:哪个RubyMine为我创build的

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

当我尝试运行应用程序,我得到这个错误下面,似乎我没有创build一个'项目'的用户,但是,我怎么能创build一个用户,并授予它在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 

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

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

通过创build数据库并将所有权分配给应用程序用户来build立连接,确保您的用户凭据设置正确。 在postgresql 9中创build一个新用户运行:

 sudo -u postgres psql 

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

 postgres=# \password 

创build一个新的用户和密码以及用户的新数据库:

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

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

这是在Ubuntu 13.10的开发环境中让你的rails应用程序和postgres一起工作的最简单的方法。

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

 $ rails new my_application -d postgresql 

2)给它一些CRUDfunction。 如果你只是看到postgres的作品,创build一个脚手架:

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

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

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

请注意, database参数适用于尚未退出的数据库,并且usernamepassword也是不存在的angular色的凭证。 我们将在稍后创build它们!

这是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)如果您的当前用户(如您的计算机用户)没有相应的pipe理员postgresangular色,则可能会出现此错误。

 psql: FATAL: role "your_username" does not exist 

现在我只安装了一次postgres,所以我可能在这里是错误的,但我认为postgres会自动创build一个pipe理angular色,使用与您安装postgres的用户相同的凭据。

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

 $ sudo su postgres 

然后运行

 $ psql 

5)你会知道你在postgresshell,因为你的terminal将如下所示:

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

6)使用postgresql语法,让我们创build一个我们在config/database.yml开发部分中指定的用户:

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

现在,这里有一些微妙之处,让我们来看看它们。

  • angular色的用户名thisismynewusername周围没有任何引号
  • WITH后指定关键字LOGIN 。 如果你不这样做,这个angular色仍然会被创build,但是不能login数据库!
  • angular色的密码, thisismynewpassword ,需要在单引号。 不是双引号
  • 在结尾添加一个分号;)

你应该在你的terminal上看到这个:

 postgres=# CREATE ROLE postgres=# 

这意味着,“创build的angular色”,但postgres的警报似乎采用git枢纽相同的命令约定。

7)现在,仍然在postgres shell中,我们需要用我们在YAML中设置的名字创build数据库。 让我们在步骤6创build的用户拥有者:

 postgres=# CREATE DATABASE my_application_development OWNER thisismynewusername; 

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

 CREATE DATABASE 

8)退出postgresshell:

 \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. 

虽然我build议仔细考虑在本地生产机器上recursion设置写入私有设置,但是我给了我的整个应用程序读写权限,如下所示:

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作为所有者重新创build它:

1)启动postgresshell:

 $ psql 

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

 postgres=# DROP DATABASE my_app_development; 

3)重新创build另一个my_app_development ,并使mynewusername成为所有者:

 postgres=# CREATE DATABASE my_application_development OWNER mynewusername; 

4)退出shell:

 postgres=# \q 

mynewusername用户不能login到数据库? 认为你在YAML中input了错误的密码,并不能完全记住你使用postgres shellinput的密码? 用YAML密码简单地改变angular色:

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)启动postgresshell:

 $ psql 

3)更新mynewusername的密码。 粘贴密码,并记住单引号:

 postgres=# ALTER ROLE mynewusername PASSWORD `musthavebeenverydrunkwheniwrotethis`; 

4)退出shell:

 postgres=# \q 

尝试通过数据库查看器(如Dbeaver)连接到本地主机,并不知道你的postgres用户的密码是什么? 像这样改变它:

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

 $ sudo passwd postgres 

2)为sudoinput您的帐户密码(与postgres无关):

 [sudo] password for starkers: myaccountpassword 

3)创buildpostgres帐户的新密码:

 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)你需要给你的用户创build数据库的能力。 从psql shell:

 ALTER ROLE thisismynewusername WITH CREATEDB 

永久解决scheme:

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

 local all postgres peer 

应该

 local all postgres md5 

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

如果你在Linux上,命令将是

 sudo service postgresql restart 

我在Ubuntu机器上面临同样的问题,所以我通过以下步骤删除了这个错误。 切换到postgres用户

$ sudo su - postgres

它会要求input密码,默认密码是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

你可以去你的/var/lib/pgsql/data/pg_hba.conf文件,并添加信任的地方Ident它为我工作。

 local all all trust host all 127.0.0.1/32 trust 

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

如果在运行unit testing时出现错误消息( Peer authentication failed for user (PG::Error) ),请确保testing数据库存在。

Interesting Posts