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

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

下面是我为 datase.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
124931 次浏览

如果您在服务器上安装 postresql,那么只需要 host: localhost to database. yml,我通常会在它显示 pool: 5的地方加上 postresql。否则,如果它不是本地主机一定告诉该应用程序在哪里找到它的数据库。

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

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

sudo -u postgres psql

如果没有设置 postgreql 用户密码,那么它只是反斜杠密码。

postgres=# \password

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

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

现在,在确认创建了数据库、用户和密码并设置了这些权限之后,更新 datase.yml 文件。别忘了 host: localhost。

这是让你的 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)使用 postgreql 语法,创建我们在 config/database.yml发展部分中指定的用户:

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

现在,这里有一些微妙的地方,让我们过一遍。

  • 角色的用户名 这是我的新用户名没有都有引号
  • 之后指定关键字 LOGIN。如果不这样做,角色仍然会被创建,但是它将无法登录到数据库!
  • 角色的密码 这是我的新密码需要使用单引号。
  • 在末尾加一个分号;)

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

postgres=#
CREATE ROLE
postgres=#

这意味着“ ROLE CREated”,但 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 外壳:

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

$ psql

3)更新 mynewusername的密码。粘贴密码,并在密码周围加上引号:

postgres=# ALTER ROLE mynewusername PASSWORD `musthavebeenverydrunkwheniwrotethis`;

4)退出 shell:

postgres=# \q

试图通过数据库查看器(如 Dbeever)连接到 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)你需要给你的用户创建数据库的能力:

ALTER ROLE thisismynewusername WITH CREATEDB

永久解决方案:

问题在于你的 pg _ hba。这行:

local   all             postgres                                peer

应该是

local   all             postgres                                md5

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

如果你在 Linux 上,命令就是

sudo service postgresql restart

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

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

您可以转到/var/lib/pgsql/data/pg _ hba. conf 文件并添加信任来代替 Ident 这招对我很管用。

local   all all trust
host    all 127.0.0.1/32    trust

有关详情,请参阅此问题 用户身份验证失败

添加 host: localhost对我来说是一种魔力

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

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

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

development:
<<: *default
database: database_name


username: database_username


password: database_password


host: localhost

仅此而已。

希望这个能帮上忙