Rails db:使用Postgres创建失败,声称角色并不存在

时间:2017-05-11 05:39:45

标签: ruby-on-rails ruby postgresql fedora pg

我已经看到了大量与此相关的问题,但到目前为止,没有一个适合其他人的解决方案对我自己有用。

目前我尝试运行“db db:create'给我以下

[ethannicolai@localhost EthansDevcampPortfolio]$ rails db:create --trace EDP_DB_PASSWORD='password1' RAILS_ENV=development
** Invoke db:create (first_time)
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:create
FATAL:  role "EthansDevcampPortfolio" does not exist
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"EthansDevcampPortfolio_development", "username"=>"EthansDevcampPortfolio", "password"=>"password1", "host"=>nil}
rails aborted!
ActiveRecord::NoDatabaseError: FATAL:  role "EthansDevcampPortfolio" does not exist
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:705:in `rescue in connect'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:701:in `connect'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:220:in `initialize'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:38:in `new'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:38:in `postgresql_connection'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:759:in `new_connection'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:803:in `checkout_new_connection'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:782:in `try_to_checkout_new_connection'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:743:in `acquire_connection'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:500:in `checkout'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:374:in `connection'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:931:in `retrieve_connection'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_handling.rb:116:in `retrieve_connection'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_handling.rb:88:in `connection'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/tasks/postgresql_database_tasks.rb:10:in `connection'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/tasks/postgresql_database_tasks.rb:19:in `create'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/tasks/database_tasks.rb:117:in `create'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/tasks/database_tasks.rb:137:in `block in create_current'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/tasks/database_tasks.rb:304:in `block in each_current_configuration'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/tasks/database_tasks.rb:303:in `each'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/tasks/database_tasks.rb:303:in `each_current_configuration'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/tasks/database_tasks.rb:136:in `create_current'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/railties/databases.rake:27:in `block (2 levels) in <top (required)>'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/task.rb:250:in `block in execute'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/task.rb:250:in `each'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/task.rb:250:in `execute'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/task.rb:194:in `block in invoke_with_call_chain'
/usr/share/ruby/monitor.rb:214:in `mon_synchronize'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/task.rb:187:in `invoke_with_call_chain'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/task.rb:180:in `invoke'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/application.rb:152:in `invoke_task'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/application.rb:108:in `block (2 levels) in top_level'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/application.rb:108:in `each'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/application.rb:108:in `block in top_level'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/application.rb:117:in `run_with_threads'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/application.rb:102:in `top_level'
/usr/local/share/gems/gems/railties-5.1.0/lib/rails/commands/rake/rake_command.rb:21:in `block in perform'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/application.rb:178:in `standard_exception_handling'
/usr/local/share/gems/gems/railties-5.1.0/lib/rails/commands/rake/rake_command.rb:18:in `perform'
/usr/local/share/gems/gems/railties-5.1.0/lib/rails/command.rb:46:in `invoke'
/usr/local/share/gems/gems/railties-5.1.0/lib/rails/commands.rb:16:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
PG::ConnectionBad: FATAL:  role "EthansDevcampPortfolio" does not exist
/home/ethannicolai/.gem/ruby/gems/pg-0.20.0/lib/pg.rb:56:in `initialize'
/home/ethannicolai/.gem/ruby/gems/pg-0.20.0/lib/pg.rb:56:in `new'
/home/ethannicolai/.gem/ruby/gems/pg-0.20.0/lib/pg.rb:56:in `connect'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:701:in `connect'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:220:in `initialize'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:38:in `new'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:38:in `postgresql_connection'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:759:in `new_connection'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:803:in `checkout_new_connection'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:782:in `try_to_checkout_new_connection'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:743:in `acquire_connection'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:500:in `checkout'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:374:in `connection'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:931:in `retrieve_connection'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_handling.rb:116:in `retrieve_connection'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/connection_handling.rb:88:in `connection'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/tasks/postgresql_database_tasks.rb:10:in `connection'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/tasks/postgresql_database_tasks.rb:19:in `create'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/tasks/database_tasks.rb:117:in `create'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/tasks/database_tasks.rb:137:in `block in create_current'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/tasks/database_tasks.rb:304:in `block in each_current_configuration'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/tasks/database_tasks.rb:303:in `each'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/tasks/database_tasks.rb:303:in `each_current_configuration'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/tasks/database_tasks.rb:136:in `create_current'
/usr/local/share/gems/gems/activerecord-5.1.0/lib/active_record/railties/databases.rake:27:in `block (2 levels) in <top (required)>'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/task.rb:250:in `block in execute'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/task.rb:250:in `each'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/task.rb:250:in `execute'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/task.rb:194:in `block in invoke_with_call_chain'
/usr/share/ruby/monitor.rb:214:in `mon_synchronize'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/task.rb:187:in `invoke_with_call_chain'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/task.rb:180:in `invoke'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/application.rb:152:in `invoke_task'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/application.rb:108:in `block (2 levels) in top_level'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/application.rb:108:in `each'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/application.rb:108:in `block in top_level'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/application.rb:117:in `run_with_threads'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/application.rb:102:in `top_level'
/usr/local/share/gems/gems/railties-5.1.0/lib/rails/commands/rake/rake_command.rb:21:in `block in perform'
/home/ethannicolai/.gem/ruby/gems/rake-12.0.0/lib/rake/application.rb:178:in `standard_exception_handling'
/usr/local/share/gems/gems/railties-5.1.0/lib/rails/commands/rake/rake_command.rb:18:in `perform'
/usr/local/share/gems/gems/railties-5.1.0/lib/rails/command.rb:46:in `invoke'
/usr/local/share/gems/gems/railties-5.1.0/lib/rails/commands.rb:16:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:create

在我的postgres控制台中,当我进入\ du时,我会回来     ethansdevcampportfolio |超级用户,无继承,创建角色,创建数据库

我已多次仔细检查用户名和密码。

pg_hba.conf看起来像

local   all         all                               trust

host    all         all         127.0.0.1/32          trust

host    all         all         ::1/128               trust

host    all         all         0.0.0.0/0         md5(I even tried this at 'trust')

config / database.yml看起来像

# PostgreSQL. Versions 9.1 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On OS X with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: EthansDevcampPortfolio_development

  # The specified database role being used to connect to postgres.
  # To create additional roles in postgres see `$ createuser --help`.
  # When left blank, postgres will use the default role. This is
  # the same name as the operating system user that initialized the database.
  username: EthansDevcampPortfolio

  # The password associated with the postgres role (username).
  password: <%= ENV['EDP_DB_PASSWORD'] %>

  # Connect on a TCP socket. Omitted by default since the client uses a
  # domain socket that doesn't need configuration. Windows does not have
  # domain sockets, so uncomment these lines.
  host:

  # The TCP port the server listens on. Defaults to 5432.
  # If your server runs on a different port number, change accordingly.
  #port: 5432

  # Schema search path. The server defaults to $user,public
  #schema_search_path: myapp,sharedapp,public

  # Minimum log levels, in increasing order:
  #   debug5, debug4, debug3, debug2, debug1,
  #   log, notice, warning, error, fatal, and panic
  # Defaults to warning.
  #min_messages: notice

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: EthansDevcampPortfolio_test

# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
production:
  url: <%= ENV['DATABASE_URL'] %>
#
production:
  <<: *default
  database: EthansDevcampPortfolio_production
  username: EthansDevcampPortfolio
  password: <%= ENV['EDP_DB_PASSWORD'] %>

我已经没有想要看的地方了。我想也许有一种方法我无意中安装了更多的postgres实例。我的意思是我对所有这些都是菜鸟,所以我安装了dnf列表,我只看到了一个postgres实例。

欢迎任何关于在哪里观看,尝试什么或者要问谁的想法。

4 个答案:

答案 0 :(得分:0)

尝试以下config/database.yml

中的代码
default: &default
  adapter: postgresql
  pool: 5
  timeout: 5000
  username : db_username
  password : db_password

development:
  <<: *default
  database: db_name

test:
  <<: *default
  database: db_name

production:
  <<: *default
  database: db_name

然后运行以下命令来创建数据库:

rake db:create

答案 1 :(得分:0)

问题是您的数据库没有该角色。您可能希望在数据库中添加EthansDevcampPortfolio角色或使用现有的postgres角色

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: EthansDevcampPortfolio_development
  username: postgres
  password: <%= ENV['EDP_DB_PASSWORD'] %>

答案 2 :(得分:0)

进入postgres控制台并创建一个超级用户,其中包含您的unix帐户的名称(不是您现在拥有的数据库本身的名称)

sudo su postgres
psql
postgres=# create user ethannicolai superuser;
CREATE ROLE

然后当postgresql通过你帐户的unix管道连接时,运行&#39; rails,&#39;您将拥有超级用户权限并成功创建数据库。

您还可以取出&#34;用户名&#34;和#34;密码&#34; config / database.yml中的字段。

虽然您是控制台中的超级用户,但您也可以使用create database创建所需的数据库,然后您甚至不需要运行db:create,但可以直接进行迁移。

答案 3 :(得分:0)

那是最好的解决方案。

sudo -u postgres createuser -s EthansDevcampPortfolio