5

I'm using rails 5.2.2.1 with postgresql 10.6, and I'm not able to create a database in my development environment. When I run

bin/rake db:create

or

bundle exec rake db:create

I get

rake aborted!
ActiveRecord::NoDatabaseError: FATAL:  database "mplaces_dev" does not exist
/home/user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:696:in `rescue in connect'
/home/user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:691:in `connect'    

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

development:
 adapter: postgis
 database: mplaces_dev
 encoding: unicode
 username: postgres
 password: postgres
 host: localhost
 postgis_extension: true
 schema_search_path: "public,postgis"

However I also created all the extensions. I've been at this for more than an hour, and still can't understand why this is happening ...

Thanks!

7
  • did you installed postgres in you're system? & pg gem in you're application Commented Apr 22, 2019 at 9:29
  • Yes, everything is installed. Commented Apr 22, 2019 at 9:31
  • did you tried the command by specifying the environment ? Commented Apr 22, 2019 at 9:34
  • 1
    Yes, by this command bin/rake db:create RAILS_ENV=development. Commented Apr 22, 2019 at 9:36
  • you're adapter name is incorrect postgis change this to postgresql Commented Apr 22, 2019 at 9:37

1 Answer 1

5

For me, it was because I had some Model dependant logic in one of my initializers.

This was never a problem before, but since setting up CI, I need to modify my project so that it excludes some logic from the initializers for the Model dependent part.

For initializer_example.rb:

# non model dependent
Aws.config(secret_key: xyz, access_key: zyx)

# model dependent
User.where(admin:true).update(field: value)

To:

# non model dependent
Aws.config(secret_key: xyz, access_key: zyx)

# model dependent
db_loaded = ::ActiveRecord::Base.connection_pool.with_connection(&:active?) rescue false
if db_loaded
    User.where(admin:true).update(field: value)
end

This will run the initializer as usual ONLY if the database has been loaded (i.e in production)

As for my CI/Testing environments, I modify the RSPEC's to load the initializer again (bad practice I know) before testing (of course by this stage the environment should be configured so this time it will run as normal) using the code:

load "#{Rails.root}/config/initializers/initializer_example.rb"
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! I googled for an hour before I found this solution. I also created a custom initializer to load some data. When I moved to a different machine, rake db:create failed. Commenting out the Model.create calls fixed this.
ActiveRecord::Base.connected? works for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.