Ubuntu / Rails PG :: InsufficientPrivilege:错误:拒绝创建数据库的权限

时间:2016-12-30 19:04:14

标签: ruby-on-rails ruby postgresql ubuntu

我正在我的Ubuntu 16.04 Droplet上托管rails应用程序。我已经使用Apache配置了FATAL: database "MyAppName_development" does not existraise ActiveRecord::NoDatabaseError.new(error.message, error)

的database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 5

development:
  <<: *default
  database: MyAppName_development

test:
  <<: *default
  database: MyAppName_test

production:
  <<: *default
  database: MyAppName_production
  username: myusername
  password: <%= ENV['MYAPPNAME_DATABASE_PASSWORD'] %>

我在Droplet中运行了以下命令:

sudo -i -u postgres

psql

# Inside psql:
CREATE USER myusername WITH PASSWORD mypassword
CREATE DATABASE MyAppName_production
CREATE DATABASE MyAppName_test
CREATE DATABASE MyAppName_development
GRANT ALL PRIVILEGES ON DATABASE MyAppName_production to myusername
GRANT ALL PRIVILEGES ON DATABASE MyAppName_test to myusername
GRANT ALL PRIVILEGES ON DATABASE MyAppName_development to myusername
ALTER USER myusername CREATEDB
\q

exit

从我的Droplet中的MyAppName根目录,我运行了bundle exec rake db:drop,然后bundle exec rake db:create:all并取回了以下内容:

PG::InsufficientPrivilege: ERROR:  permission denied to create database
: CREATE DATABASE "MyAppName_development" ENCODING = 'unicode'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract_adapter.rb:472:in `block in log'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `log'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql/schema_statements.rb:78:in `create_database'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/tasks/postgresql_database_tasks.rb:15:in `create'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:93:in `create'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:102:in `block in create_all'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:284:in `block in each_local_configuration'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:280:in `each_value'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:280:in `each_local_configuration'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:102:in `create_all'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/railties/databases.rake:11:in `block (3 levels) in <top (required)>'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:248:in `block in execute'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:243:in `each'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:243:in `execute'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:187:in `block in invoke_with_call_chain'
/home/myusername/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:180:in `invoke_with_call_chain'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:173:in `invoke'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:150:in `invoke_task'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `each'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `block in top_level'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:115:in `run_with_threads'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:100:in `top_level'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:78:in `block in run'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:176:in `standard_exception_handling'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:75:in `run'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/bin/rake:33:in `<top (required)>'
/home/myusername/.rvm/gems/ruby-2.3.1/bin/rake:23:in `load'
/home/myusername/.rvm/gems/ruby-2.3.1/bin/rake:23:in `<main>'
/home/myusername/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `eval'
/home/myusername/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `<main>'
Couldn't create database for {"username"=>"myusername", "adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"MyAppName_development", "password"=>nil}
PG::InsufficientPrivilege: ERROR:  permission denied to create database
: CREATE DATABASE "MyAppName_test" ENCODING = 'unicode'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract_adapter.rb:472:in `block in log'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `log'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql/schema_statements.rb:78:in `create_database'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/tasks/postgresql_database_tasks.rb:15:in `create'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:93:in `create'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:102:in `block in create_all'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:284:in `block in each_local_configuration'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:280:in `each_value'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:280:in `each_local_configuration'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:102:in `create_all'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/railties/databases.rake:11:in `block (3 levels) in <top (required)>'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:248:in `block in execute'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:243:in `each'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:243:in `execute'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:187:in `block in invoke_with_call_chain'
/home/myusername/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:180:in `invoke_with_call_chain'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:173:in `invoke'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:150:in `invoke_task'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `each'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `block in top_level'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:115:in `run_with_threads'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:100:in `top_level'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:78:in `block in run'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:176:in `standard_exception_handling'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:75:in `run'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/bin/rake:33:in `<top (required)>'
/home/myusername/.rvm/gems/ruby-2.3.1/bin/rake:23:in `load'
/home/myusername/.rvm/gems/ruby-2.3.1/bin/rake:23:in `<main>'
/home/myusername/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `eval'
/home/myusername/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `<main>'
Couldn't create database for {"username"=>"myusername", "adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"MyAppName_test", "password"=>nil}
PG::InsufficientPrivilege: ERROR:  permission denied to create database
: CREATE DATABASE "MyAppName_production" ENCODING = 'unicode'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract_adapter.rb:472:in `block in log'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `log'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql/schema_statements.rb:78:in `create_database'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/tasks/postgresql_database_tasks.rb:15:in `create'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:93:in `create'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:102:in `block in create_all'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:284:in `block in each_local_configuration'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:280:in `each_value'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:280:in `each_local_configuration'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:102:in `create_all'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/railties/databases.rake:11:in `block (3 levels) in <top (required)>'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:248:in `block in execute'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:243:in `each'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:243:in `execute'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:187:in `block in invoke_with_call_chain'
/home/myusername/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:180:in `invoke_with_call_chain'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:173:in `invoke'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:150:in `invoke_task'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `each'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `block in top_level'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:115:in `run_with_threads'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:100:in `top_level'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:78:in `block in run'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:176:in `standard_exception_handling'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:75:in `run'
/home/myusername/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/bin/rake:33:in `<top (required)>'
/home/myusername/.rvm/gems/ruby-2.3.1/bin/rake:23:in `load'
/home/myusername/.rvm/gems/ruby-2.3.1/bin/rake:23:in `<main>'
/home/myusername/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `eval'
/home/myusername/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `<main>'
Couldn't create database for {"username"=>"myusername", "adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"MyAppName_production", "password"=>nil}

1 个答案:

答案 0 :(得分:1)

我建议你可以在迁移前重置postgres中的数据库。

pg:reset DATABASE_NAME

在你的项目目录之后

rake db:schema:load
rake db:migrate

如果您有种子数据

rake db:seed