postgresql PG::ConnectionBad 致命:角色“myapp”不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22647993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:22:08  来源:igfitidea点击:

PG::ConnectionBad FATAL: role "myapp" does not exist

ruby-on-railspostgresqlheroku

提问by user3138341

I'm able to type rails s without any issues inside of an app using postgres.app on Mac using postgres.app, however, when I go to localhost:3000 it gives me this error:

我可以使用 postgres.app 在 Mac 上使用 postgres.app 在应用程序内部输入 rails s 没有任何问题,但是,当我转到 localhost:3000 时,它给了我这个错误:

PG::ConnectionBad
FATAL: role "myapp" does not exist

I thought at first the problem was the database.yml file but heroku docs even say to have it the way it is: https://devcenter.heroku.com/articles/getting-started-with-rails4

我一开始以为问题出在 database.yml 文件上,但 heroku 文档甚至说要按原样处理:https: //devcenter.heroku.com/articles/getting-started-with-rails4

development:
adapter: postgresql
encoding: unicode
database: myapp_development
pool: 5
username: myapp
password:

Here is my full log. I've seen similar issues but they only vaguely relate to this.

这是我的完整日志。我见过类似的问题,但它们只是模糊地与此相关。

snippet:

片段:

Started GET "/" for 127.0.0.1 at 2014-03-25 16:48:31 -0600

PG::ConnectionBad (FATAL:  role "myapp" does not exist
):
  activerecord (4.0.2) lib/active_record/connection_adapters/postgresql_adapter.rb:831:in `initialize'
  activerecord (4.0.2) lib/active_record/connection_adapters/postgresql_adapter.rb:831:in `new'
  activerecord (4.0.2) lib/active_record/connection_adapters/postgresql_adapter.rb:831:in `connect'
  activerecord (4.0.2) lib/active_record/connection_adapters/postgresql_adapter.rb:548:in `initialize'
  activerecord (4.0.2) lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `new'
  activerecord (4.0.2) lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `postgresql_connection'
  activerecord (4.0.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:440:in `new_connection'
  activerecord (4.0.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:450:in `checkout_new_connection'
  activerecord (4.0.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:421:in `acquire_connection'
  activerecord (4.0.2)  lib/active_record/connection_adapters/abstract/connection_pool.rb:356:in `block in checkout'
  /Users/johncurry/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
  activerecord (4.0.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `checkout'
  activerecord (4.0.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection'
  /Users/johncurry/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
  activerecord (4.0.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection'
  activerecord (4.0.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in 

any suggestions?

有什么建议?

回答by Craig Ringer

It would appear that you have not created a user account for the application. In psql:

您似乎尚未为该应用程序创建用户帐户。在psql

CREATE USER myapp WITH PASSWORD 'thepassword';

You'll also need to create a DB if you haven't:

如果您还没有,您还需要创建一个数据库:

CREATE DATABASE myapp_development OWNER myapp;

回答by CWitty

You need to create the Postgres user on your local machine. The role is your username.

您需要在本地计算机上创建 Postgres 用户。角色是您的用户名。

回答by Abd El Rahman Hassan

That's the best solution.

这是最好的解决办法。

sudo -u postgres createuser -s myapp