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

Ruby on-RailsPostgresql

Ruby on-Rails Problem Overview


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

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:

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

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

And here's a part of the postgresql log:

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!

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Rails 4.1 ships with spring preloader, and

> 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.

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

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:

bundle exec rake db:create

Solution 2 - Ruby on-Rails

I found the issue ...

It has to do with Rails initializers: Rails seems to load all the initializers before executing 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).

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):

/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:migrateto 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:inconnect'
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 <top (required)>' 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 <top (required)>' 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).

Solution 3 - Ruby on-Rails

You might try running this command-

bin/rake db:create RAILS_ENV=development

Or this command-

bin/rake db:migrate RAILS_ENV=development

Solution 4 - Ruby on-Rails

Had the same issue today (in OS X).

Solved with:

psql -U postgres
CREATE ROLE rolename WITH CREATEDB LOGIN;

Use \du to check if it's created.

Use \password rolename to create a password for that user/role.

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

Solution 5 - Ruby on-Rails

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

$ brew services list
$ brew services restart postgresql

Solution 6 - Ruby on-Rails

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 = true in config/environments/development.rb. I changed it to false and it solved the problem.

Solution 7 - Ruby on-Rails

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

You also need to create the postgres role:

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

Solution 8 - Ruby on-Rails

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

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

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'

Solution 9 - Ruby on-Rails

Here is my Dockerfile for the rails project. Take a look at this part:

CMD mkdir -p tmp/pids && \
    bundle exec rake db:create && \
    bundle exec rails db:migrate && \
    bundle exec rake db:seed && \
    bundle exec puma -C config/puma.rb

Bundler 2.1.4, ruby 2.7.2, rails 6.0.3 compatible. Docker compose part:

server:
    container_name: server
    build:
      context: Server
      dockerfile: Dockerfile
      args:
        ENV: 'development'
    restart: unless-stopped
    depends_on:
      - db
    env_file:
      - ./Server/.env
    ports:
      - '9090:3000'
    volumes:
      - ./Server:/app
      - ./Server/Docker/usr/.gemrc:/root/.gemrc
    networks:
      - app-network

FROM ruby:2.7.2-alpine3.12

# env and arg variables setup
ARG APP_HOME='/app'
ARG ENV
ENV RAILS_ENV=$ENV \
    RACK_ENV=$ENV \
    RAILS_ROOT=$APP_HOME

# timezone setup
RUN apk add --update tzdata && \
    cp /usr/share/zoneinfo/Europe/London /etc/localtime && \
    echo "Europe/London" > /etc/timezone

# building in tmp dir
WORKDIR /tmp
ADD Gemfile ./

#install dependencies (bundler 2.1.4, ruby 2.7.2, rails 6.0.3 compatible) 
RUN apk add --update --virtual runtime-deps postgresql-client nodejs libffi-dev readline sqlite xz && \
    apk add --virtual build-deps build-base openssl postgresql-dev libc-dev linux-headers libxml2-dev libxslt-dev readline-dev && \
    gem install bundler -v 2.1.4 && \
    bundle install --jobs=4 && \
    apk del build-deps

# /app
WORKDIR $APP_HOME

# create dbs if no such, migrate, seed, start
CMD mkdir -p tmp/pids && \
    bundle exec rake db:create && \
    bundle exec rails db:migrate && \
    bundle exec rake db:seed && \
    bundle exec puma -C config/puma.rb

Solution 10 - Ruby on-Rails

Here is the list of commands I run at each deployment (including the first one)

//create pids folder if no yet
mkdir -p tmp/pids

//create dbs if no yet
bundle exec rake db:create

//run all new migrations
bundle exec rails db:migrate

//seed db with data if required
bundle exec rake db:seed

//start the server with config
bundle exec puma -C config/puma.rb

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRodrigo SerranoView Question on Stackoverflow
Solution 1 - Ruby on-RailsartmView Answer on Stackoverflow
Solution 2 - Ruby on-RailsRodrigo SerranoView Answer on Stackoverflow
Solution 3 - Ruby on-RailsnrakoView Answer on Stackoverflow
Solution 4 - Ruby on-RailslucasarrudaView Answer on Stackoverflow
Solution 5 - Ruby on-RailsClementView Answer on Stackoverflow
Solution 6 - Ruby on-Railswho_khiersView Answer on Stackoverflow
Solution 7 - Ruby on-RailsJason NobleView Answer on Stackoverflow
Solution 8 - Ruby on-RailsRaounak SharmaView Answer on Stackoverflow
Solution 9 - Ruby on-RailsTim KozakView Answer on Stackoverflow
Solution 10 - Ruby on-RailsTim KozakView Answer on Stackoverflow