关于postgresql:Rails 3.2,致命:用户的对等身份验证失败(PG ::错误)

Rails 3.2, FATAL: Peer authentication failed for user (PG::Error)

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

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

1
2
3
4
5
6
7
development:
  adapter: postgresql
  encoding: unicode
  DATABASE: mydb_development
  pool: 5
  username: myuser
  password:

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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。否则如果不是localhost肯定告诉应用程序在哪里找到它的数据库。

1
2
3
4
5
6
7
8
development:
  adapter: postgresql
  encoding: unicode
  DATABASE: kickrstack_development
  host: localhost
  pool: 5
  username: kickrstack
  password: secret

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

1
sudo -u postgres psql

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

1
postgres=# \password

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

1
2
postgres=# CREATE USER"guy_on_stackoverflow" WITH password 'keepitonthedl';
postgres=# CREATE DATABASE"dcaclab_development" owner"guy_on_stackoverflow";

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


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

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

1
$ rails NEW my_application -d postgresql

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

1
$ rails g scaffold cats name:string age:INTEGER colour:string

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

1
2
3
4
5
encoding: UTF-8
host: localhost
DATABASE: my_application_development
username: thisismynewusername
password: thisismynewpassword

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

这就是config/database.yml的外观(在copypasting中没有羞耻感:D):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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:

1
$ psql

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

1
psql: FATAL:  ROLE"your_username" does NOT exist

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

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

1
$ sudo su postgres

然后运行

1
$ psql

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

1
2
3
4
5
$ psql
psql (9.1.10)
TYPE"help" FOR help.

postgres=#

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

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

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

  • 角色的用户名thisismynewusername没有任何类型的引号
  • 在WITH之后指定关键字LOGIN。如果不这样做,仍将创建角色,但无法登录数据库!
  • 角色的密码thisismynewpassword需要使用单引号。不是双引号。
  • 在末尾添加一个半冒号;)
  • 您应该在终端中看到这个:

    1
    2
    3
    postgres=#
    CREATE ROLE
    postgres=#

    这意味着,"ROLE CREATED",但postgres'警报似乎采用了与git hub相同的命令式约定。

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

    1
    postgres=# CREATE DATABASE my_application_development OWNER thisismynewusername;

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

    1
    CREATE DATABASE

    8)退出postgres shell:

    1
    \q

    9)现在是真相的时刻:

    1
    $ RAILS_ENV=development rake db:migrate

    如果你得到这个:

    1
    2
    3
    4
    ==  CreateCats: migrating =================================================
    -- create_table(:cats)
       -> 0.0028s
    ==  CreateCats: migrated (0.0028s) ========================================

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

    9a)在我的本地计算机上,我一直收到权限错误。我不记得确切,但这是一个错误

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

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

    9b)爬上一个目录级别:

    1
    $ cd ..

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

    1
    $ chmod -R 0666 my_application

    9d)再次运行迁移:

    1
    2
    3
    4
    5
    6
    $ 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 shell:

    1
    $ psql

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

    1
    postgres=# DROP DATABASE my_app_development;

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

    1
    postgres=# CREATE DATABASE my_application_development OWNER mynewusername;

    4)退出shell:

    1
    postgres=# \q

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

    1)打开你的YAML,并将密码复制到剪贴板:

    1
    2
    3
    4
    5
    6
    7
    8
    9
     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:

    1
    $ psql

    3)更新mynewusername的密码。粘贴密码,并记住在其周围放置单引号:

    1
    postgres=# ALTER ROLE mynewusername PASSWORD `musthavebeenverydrunkwheniwrotethis`;

    4)退出shell:

    1
    postgres=# \q

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

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

    1
    $ sudo passwd postgres

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

    1
    [sudo] password FOR starkers: myaccountpassword

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

    1
    2
    3
    Enter NEW UNIX password: databasesarefun
    Retype NEW UNIX password: databasesarefun
    passwd: password updated successfully

    收到此错误消息?:

    1
    2
    3
    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:

    1
    ALTER ROLE thisismynewusername WITH CREATEDB

    好。


    永久解决方案:

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

    1
    LOCAL   ALL             postgres                                peer

    应该

    1
    LOCAL   ALL             postgres                                md5

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

    如果你在Linux上,命令就是

    1
    sudo service postgresql restart


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

    $ sudo su - postgres

    它会要求输入密码,默认密码为postgres

    将用户切换到postgres后,打开psql控制台

    1
    $ psql

    所以如果有多个版本可用,请检查postgres的版本

    1
    2
    3
    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是版本返回表单上部命令

    并替换

    1
    LOCAL   ALL             postgres                                peer

    1
    LOCAL   ALL             postgres                                md5

    <5233>

    我也在博客上写了一些步骤

    http://tarungarg402.blogspot.in/2014/10/set-up-postgresql-on-ubuntu.html


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

    1
    2
    LOCAL   ALL ALL trust
    host    ALL 127.0.0.1/32    trust

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


    添加"host:locahost"对我来说是神奇的

    1
    2
    3
    4
    5
    development:
    adapter: postgresql
    DATABASE: database_name_here
    host: localhost
    username: user_name_here


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