Rails 3.2,致命:对用户的对等身份验证失败(pg::Error)

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (123)

我正在Ubuntu11.10和Rubymy上运行我的开发

以下是我为database.yml创建的开发设置:Rubymy为我创建的

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
提问于
用户回答回答于

如果在服务器上安装了postresql,那么只需将:localhost安装到database.yml中,我通常会将其放在池里:5。否则,如果不是本地主机,一定要告诉应用程序在哪里找到它的数据库。

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文件。别忘了主人:本地主人。

用户回答回答于

这是让Rails应用程序在开发环境在Ubuntu 13.10。

1)使用Gemfile中的Postgres YAML和PG‘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 should look (no shame in 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

如果当前用户(如您的计算机用户)没有相应的管理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周围有各种各样的引号
  • 指定关键字登录在,如果不这样做,角色仍然会被创建,但是它将无法登录到数据库!
  • 角色的密码,密码,需要用单引号。不是双引号
  • 在末尾加一个分号。

你应该在你的航站楼里看到这个:

postgres=#
CREATE ROLE
postgres=#

这意味着“创建了角色”,但Postgres的警报似乎采用了Git中心相同的命令式约定。

7)现在,仍然在Postgres shell中,我们需要使用我们在YAML中设置的名称创建数据库。

postgres=# CREATE DATABASE my_application_development OWNER thisismynewusername;

将知道您是否成功,因为将获得输出:

CREATE DATABASE

8)退出Postgres外壳:

\q

9)现在是真相时刻:

$ RAILS_ENV=development rake db:migrate

如果你收到这个:

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

在我的本地机器上,我一直收到一个权限错误。我不太清楚,但这是个错误

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

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

9b)爬上一个目录层:

$ cd ..

(9c)设置my的权限。_应用程序目录及其所有内容到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用户没有CRUD的特权。my_app_development数据库?删除数据库,并以mynewusername作为所有者再次创建它:

1)启动Postgres外壳:

$ psql

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

postgres=# DROP DATABASE my_app_development;

3)重新创造另一种my_app_development并使我的用户名成为所有者:

postgres=# CREATE DATABASE my_application_development OWNER mynewusername;

4)退出外壳:

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外壳:

$ psql    

3)最新情况mynewusername密码。粘贴密码,并记住在密码周围加上单引号:

postgres=# ALTER ROLE mynewusername PASSWORD `musthavebeenverydrunkwheniwrotethis`;

4)退出外壳:

postgres=# \q

试图通过数据库查看器(如Dbeaver)连接本地主机,而不知道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

扫码关注云+社区