ChatGPT解决这个技术问题 Extra ChatGPT

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

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!

what about username and passowrd?
I think that is not it: I am using postgresql "peer" authentication method; anyway, just in case, I also added username and password to database.yml, and still got the same error. I also think that, if it was an authentication problem, postgres would have complained about the user not existing before complaining about the db.

a
artm

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

You pointed out the problem correctly. If any of the initializers depends on an existing database then the whole rake task db:create fails. but what you suggested is not necessarily the generic solution. It might work in your case which is using spring but doesn't prevent other initializers
Works with Rails 4.2 as well. Thanks!
If you hit the issue on Rails 4.2 +, this is the correct answer. There is likely no need to mess around in postgres proper.
Same for Rails 5.0.1. Stopping Spring first or disabling it with DISABLE_SPRING did not change the behavior. Running it without Spring altogether was the only thing that worked.
R
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.

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


+ 1 for stressing the importance of reading error stack traces more closely. Stuff like this is the entire point of having them.
I had the same problem with the rack-attack gem that reads the db in its initialiser. As I don't care about DoS attacks when running rake tasks I wrapped the initialiser in unless defined?(Rake::Application).
a
atw

You might try running this command-

bin/rake db:create RAILS_ENV=development

Or this command-

bin/rake db:migrate RAILS_ENV=development

l
lucasarruda

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


C
Clement

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

$ brew services list
$ brew services restart postgresql

J
Jason Swett

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.


J
Jason Noble

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'

I tried, but I think that is not it: the role was already created (as a superadmin, actually), and I am using postgrsql "peer" authentication method. I tried adding only a username, and both username and password to database.yml in any case, and I still got the same error.
As far as I know migration will automatically create db for SQLite and for Postgres you have to create it on your own on development and further environments.
That's not correct. Rails defaults to the operating system user that initialized the database as the default database.yml template explains: github.com/rails/rails/blob/…. So whoami is used as the default I believe. psql postgres -U `whoami` should work if the database was initialized with the correct user.
R
Raounak Sharma

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'

T
Tim Kozak

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


T
Tim Kozak

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

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now