Postgresql - 无法创建角色

时间:2015-07-03 09:32:33

标签: ruby-on-rails postgresql

您好我试着在rails应用程序的ruby中创建数据库但是我遇到了这个错误

    FATAL:  role "giovanni" does not exist
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:661:in `rescue in connect'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `connect'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `new'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `postgresql_connection'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:436:in `new_connection'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:446:in `checkout_new_connection'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
/usr/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
/usr/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:565:in `retrieve_connection'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_handling.rb:87:in `connection'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/tasks/postgresql_database_tasks.rb:8:in `connection'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/tasks/postgresql_database_tasks.rb:17:in `create'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:93:in `create'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:107:in `block in create_current'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:275:in `block in each_current_configuration'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:274:in `each'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:274:in `each_current_configuration'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:106:in `create_current'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:17:in `block (2 levels) in <top (required)>'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `call'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `block in execute'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `each'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/usr/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `each'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:75:in `run'
/var/lib/gems/2.1.0/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
/usr/local/bin/rake:23:in `load'
/usr/local/bin/rake:23:in `<main>'
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"app_angular_rails_development"}
FATAL:  role "giovanni" does not exist
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:661:in `rescue in connect'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `connect'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `new'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `postgresql_connection'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:436:in `new_connection'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:446:in `checkout_new_connection'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
/usr/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
/usr/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:565:in `retrieve_connection'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_handling.rb:87:in `connection'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/tasks/postgresql_database_tasks.rb:8:in `connection'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/tasks/postgresql_database_tasks.rb:17:in `create'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:93:in `create'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:107:in `block in create_current'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:275:in `block in each_current_configuration'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:274:in `each'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:274:in `each_current_configuration'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:106:in `create_current'
/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:17:in `block (2 levels) in <top (required)>'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `call'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `block in execute'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `each'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/usr/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `each'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:75:in `run'
/var/lib/gems/2.1.0/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
/usr/local/bin/rake:23:in `load'
/usr/local/bin/rake:23:in `<main>'
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"app_angular_rails_test"}

似乎在我的本地计算机中没有名为“giovanni”的角色,所以我尝试通过登录postgresql控制台创建一个并插入查询以创建一个用户:

sudo -u postgress -i
CREATE ROLE giovanni WITH PASSWORD 'password' CREATEDB LOGIN CREATEROLE CREATEUSER SUPERUSER;

但是我发现了这个错误:

  

错误:冲突或冗余选项

我尝试按照数据库的官方文档以其他方式重写此查询,但它没有用

3 个答案:

答案 0 :(得分:2)

您可以尝试使用createuser程序创建用户,它具有与CREATE ROLE相同的功能(事实上,它调用此命令)但可以从命令shell运行。

$ createuser giovanni
Shall the new role be a superuser? (y/n) y
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) y
CREATE USER

command line选项:

$ createuser -P -s -e giovanni
Enter password for new role: xyzzy
Enter it again: xyzzy
CREATE ROLE giovanni PASSWORD 'xyzzy' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;
CREATE ROLE

答案 1 :(得分:0)

这是我通常用来设置postgres,创建数据库及其在ubuntu终端上的database.yml中使用的角色

add-apt-repository ppa:pitti/postgresql
##or use sudo apt-get install postgresql instead of repository
apt-get -y update
apt-get -y install postgresql libpq-dev
sudo -u postgres psql
# \password
# create user blog with password 'secret';
# create database blog_production owner blog;
# \q

database.yml

中使用上述角色
development:
  adapter: postgresql
  encoding: unicode
  database: blog_production 
  pool: 5
  host: localhost
  username: blog
  password:secret

一旦完成,您只需要运行bundle exec rake db:setup ....并且您将准备好数据库中的所有表。

答案 2 :(得分:0)

这是因为您同时使用CREATEUSER和CREATEROLE。将查询更改为CREATE ROLE test WITH SUPERUSER LOGIN CREATEDB CREATEROLE ENCRYPTED PASSWORD 'password';