Ruby-on-rails rake db:create 使用 postgresql 抛出“数据库不存在”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25611004/
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-09-02 23:53:39  来源:igfitidea点击:

rake db:create throws "database does not exist" error with postgresql

ruby-on-railspostgresql

提问by Rodrigo Serrano

I'm using rails 4.1.5 with postgresql 9.1 under Debian 7, and I'm not able to create a database in my development environment. When I run

我在 Debian 7 下使用带有 postgresql 9.1 的 rails 4.1.5,但我无法在我的开发环境中创建数据库。当我跑

bin/rake db:create

I get

我得到

home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:898:in `rescue in connect': FATAL:  database "direct-dev" does not exist
Run `$ bin/rake db:create db:migrate` to create your database (ActiveRecord::NoDatabaseError)
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:888:in `connect'
from ...

I am trying to create the database so, naturally, it does not exist. However rails should create it ... Here's my config/database.yml:

我正在尝试创建数据库,因此它自然不存在。但是 rails 应该创建它......这是我的 config/database.yml:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5

development:
  <<: *default
  database: direct-dev

And here's a part of the postgresql log:

这是 postgresql 日志的一部分:

2014-09-01 19:30:40 CEST LOG:  connection received: host=[local]
2014-09-01 19:30:40 CEST LOG:  connection authorized: user=rs database=direct-dev
2014-09-01 19:30:40 CEST FATAL:  database "direct-dev" does not exist

Do you have any pointers? I've been at this for more than an hour, and still can't understand why this is happening ...

你有什么指示吗?我已经在这里呆了一个多小时了,但仍然无法理解为什么会发生这种情况......

Thanks!

谢谢!

回答by artm

Rails 4.1 ships with spring preloader, and

Rails 4.1附带弹簧预加载器,并且

New Rails 4.1 applications will ship with "springified" binstubs. This means that bin/rails and bin/rake will automatically take advantage of preloaded spring environments.

新的 Rails 4.1 应用程序将附带“弹簧化”的 binstub。这意味着 bin/rails 和 bin/rake 将自动利用预加载的弹簧环境。

which means that the "springified" bin/rakewill attempt to preload the app, which in turn will attempt to run the initilizers resulting in the problem you're seeing.

这意味着“springified”bin/rake将尝试预加载应用程序,这反过来将尝试运行初始化程序,从而导致您看到的问题。

To fix / work around this you want to run the initial setup rake tasks without spring. One way to achieve that is to run it with bundler instead:

要修复/解决此问题,您希望在没有 spring 的情况下运行初始设置 rake 任务。实现这一目标的一种方法是使用 bundler 来运行它:

bundle exec rake db:create

回答by Rodrigo Serrano

I found the issue ...

我发现问题...

It has to do with Rails initializers: Rails seems to load all the initializers before executing bin/rake db:create.

它与 Rails 初始化器有关:Rails 似乎在执行之前加载了所有初始化器bin/rake db:create

And, in this particular application, there are a few custom initializers which depend on an ActiveRecord model (which, in turn, depends on the database being created, available and with the corresponding table).

而且,在这个特定的应用程序中,有一些自定义初始值设定项依赖于 ActiveRecord 模型(反过来,依赖于正在创建的、可用的和具有相应表的数据库)。

Therefore rake never gets to actually execute the task, it fails when executing the initializers. I should have known if I had carefully read the full error message log. This is the full log (see below, in bold, the offending lines):

因此 rake 永远不会真正执行任务,它在执行初始化程序时失败。如果我仔细阅读了完整的错误消息日志,我应该知道。这是完整的日志(见下面,粗体,违规行):

/home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:898:in `rescue in connect': FATAL:  database "ds-dev" does not exist
Run `$ bin/rake db:create db:migrate` to create your database (ActiveRecord::NoDatabaseError)
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:888:in `connect'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:568:in `initialize'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `new'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `postgresql_connection'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:435:in `new_connection'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:445:in `checkout_new_connection'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:416:in `acquire_connection'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:351:in `block in checkout'
from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:350:in `checkout'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection'
from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:541:in `retrieve_connection'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_handling.rb:87:in `connection'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/model_schema.rb:209:in `table_exists?'
from /home/rs/pr/ds/app/models/property.rb:32:in `get'
from /home/rs/pr/ds/config/initializers/custom/setup_mail.rb:3:in `'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `load'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `block in load'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:232:in `load_dependency'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `load'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/engine.rb:648:in `block in load_config_initializer'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/notifications.rb:161:in `instrument'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/engine.rb:647:in `load_config_initializer'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/engine.rb:612:in `block (2 levels) in '
from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/engine.rb:611:in `each'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/engine.rb:611:in `block in '
from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/initializable.rb:30:in `instance_exec'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/initializable.rb:30:in `run'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/initializable.rb:55:in `block in run_initializers'
from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:226:in `block in tsort_each'
from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:418:in `block (2 levels) in each_strongly_connected_component_from'
from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:427:in `each_strongly_connected_component_from'
from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:417:in `block in each_strongly_connected_component_from'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/initializable.rb:44:in `each'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/initializable.rb:44:in `tsort_each_child'
from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:411:in `call'
from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:411:in `each_strongly_connected_component_from'
from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:347:in `block in each_strongly_connected_component'
from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:345:in `each'
from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:345:in `call'
from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:345:in `each_strongly_connected_component'
from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:224:in `tsort_each'
from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:205:in `tsort_each'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/initializable.rb:54:in `run_initializers'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/application.rb:300:in `initialize!'
from /home/rs/pr/ds/config/environment.rb:5:in `'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:247:in `require'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:247:in `block in require'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:232:in `load_dependency'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:247:in `require'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/application.rb:92:in `preload'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/application.rb:140:in `serve'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/application.rb:128:in `block in run'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/application.rb:122:in `loop'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/application.rb:122:in `run'
from /home/rs/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/application/boot.rb:18:in `'
from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `'

I will work on refactoring the code, in order to get rid of any model dependencies in the initializers (which I'm pretty sure it must be a very bad practice).

我将致力于重构代码,以摆脱初始化程序中的任何模型依赖项(我很确定这一定是一个非常糟糕的做法)。

回答by nrako

You might try running this command-

您可以尝试运行此命令-

bin/rake db:create RAILS_ENV=development

Or this command-

或者这个命令——

bin/rake db:migrate RAILS_ENV=development

回答by lucasarruda

Had the same issue today (in OS X).

今天有同样的问题(在 OS X 中)。

Solved with:

解决了:

psql -U postgres
CREATE ROLE rolename WITH CREATEDB LOGIN;

Use \duto check if it's created.

使用\du检查,如果它的创建。

Use \password rolenameto create a password for that user/role.

使用\password rolename创建一个密码,用户/角色

Then you can setup DB with bundle exec rake db:migrate

然后你可以设置数据库 bundle exec rake db:migrate

回答by Clement

None of the solutions worked for me, but this worked for me.

没有一个解决方案对我有用,但这对我有用。

$ brew services list
$ brew services restart postgresql

回答by who_khiers

None of the solutions worked for me, but then I saw that some answers mentioned preloading as part of the problem. I realized I had config.eager_load = truein config/environments/development.rb. I changed it to false and it solved the problem.

没有一个解决方案对我有用,但后来我看到一些答案提到预加载是问题的一部分。我意识到我 config.eager_load = trueconfig/environments/development.rb. 我将其更改为 false 并解决了问题。

回答by Jason Noble

I see two issues, as is04 pointed out you probably need at least a usernamevalue in your database.yml.

我看到了两个问题,正如 is04 指出的那样username,您的 database.yml 中可能至少需要一个值。

You also need to create the postgres role:

您还需要创建 postgres 角色:

su - postgres
create role direct-dev with createdb login password 'password1'

回答by Raounak Sharma

I had the same problem and in my case, I had used Answer.column_namein a validation in the Answer model itself. Because test database was already created on my local so RAILS_ENV=test rails db:createwas working fine on local, but giving the same error in CI/CD pipeline

我遇到了同样的问题,就我而言,我曾Answer.column_name在 Answer 模型本身的验证中使用过。因为测试数据库已经在我的本地创建,所以RAILS_ENV=test rails db:create在本地运行良好,但在 CI/CD 管道中出现相同的错误

It was due to the reason that Rails load all the files inside app/ directory before running db:createcommand, and at that time as no database is there Answer.column_namesfailed.

这是由于 Rails 在运行db:create命令之前加载了 app/ 目录中的所有文件,并且当时没有数据库Answer.column_names失败。

I used something like this:

我使用了这样的东西:

  validates_uniqueness_of :content, scope: (Answer.column_names - %w[id created_at updated_at]).map(&:to_sym), message: 'Duplicate answer'

which is wrong. Then I changed to:

这是错误的。然后我改为:

DUPLICATE_ANSWER_SCOPE = %i[content question_id session_id]
validates_uniqueness_of :content, scope: DUPLICATE_ANSWER_SCOPE, message: 'Duplicate answer'