我在Debian 7下使用rails 4.1.5和postgresql 9.1,而且我无法在我的开发环境中创建数据库。我跑的时候
bin/rake db:create
我得到了
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 ...
我正在尝试创建数据库,因此,它自然不存在。但是rails应该创建它......这是我的config / database.yml:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: direct-dev
这是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
你有什么指针吗?我已经在这里工作了一个多小时,但仍然无法理解为什么会这样......
谢谢!
答案 0 :(得分:98)
Rails 4.1 ships with spring preloader和
新的Rails 4.1应用程序将附带&#34; springified&#34; binstubs。这意味着bin / rails和bin / rake将自动利用预加载的弹簧环境。
这意味着&#34;弹跳&#34; bin/rake
会尝试预加载应用,然后会尝试运行启动器,从而导致您看到的问题。
要修复/解决此问题,您需要在没有弹簧的情况下运行初始设置rake任务。实现这一目标的一种方法是使用bundler运行它:
bundle exec rake db:create
答案 1 :(得分:19)
我发现了这个问题......
它与Rails初始值设定项有关:Rails似乎在执行bin/rake db:create
之前加载所有初始值设定项。
并且,在这个特定的应用程序中,有一些自定义初始值设定项依赖于ActiveRecord模型(反过来,它取决于正在创建的数据库,可用的数据库以及相应的表)。
因此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 `'
我将努力重构代码,以便摆脱初始化程序中的任何模型依赖关系(我非常确定它必定是一种非常糟糕的做法)。
答案 2 :(得分:13)
您可以尝试运行此命令 -
bin/rake db:create RAILS_ENV=development
或者这个命令 -
bin/rake db:migrate RAILS_ENV=development
答案 3 :(得分:2)
今天有同样的问题(在OS X中)。
解决方案:
psql -U postgres
CREATE ROLE rolename WITH CREATEDB LOGIN;
使用\du
检查是否已创建。
使用\password rolename
为用户/角色创建密码。
然后您可以使用bundle exec rake db:migrate
答案 4 :(得分:2)
这些解决方案都不适合我,但这对我有用。
$ brew services list
$ brew services restart postgresql
答案 5 :(得分:1)
我看到两个问题,因为is04指出你在database.yml中可能至少需要一个username
值。
您还需要创建postgres角色:
su - postgres
create role direct-dev with createdb login password 'password1'
答案 6 :(得分:1)
没有一个解决方案对我有用,但是后来我看到一些答案提到预加载是问题的一部分。我意识到我有 {。{1}}在development.rb中。我将其更改为false并解决了问题。
答案 7 :(得分:1)
我遇到了同样的问题,就我而言,我在答案模型本身的验证中使用了Answer.column_name
。由于测试数据库已经在我的本地计算机上创建,因此RAILS_ENV=test rails db:create
在本地计算机上可以正常工作,但在CI / CD管道中却给出了相同的错误
这是由于Rails在运行db:create
命令之前先将app /目录中的所有文件加载的原因,因为当时没有数据库Answer.column_names
失败。
我使用了这样的东西:
validates_uniqueness_of :content, scope: (Answer.column_names - %w[id created_at updated_at]).map(&:to_sym), message: 'Duplicate answer'
这是错误的。然后我更改为:
DUPLICATE_ANSWER_SCOPE = %i[content question_id session_id]
validates_uniqueness_of :content, scope: DUPLICATE_ANSWER_SCOPE, message: 'Duplicate answer'
答案 8 :(得分:0)
这是我用于rails项目的Dockerfile。 看看这部分:
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
捆绑器2.1.4,红宝石2.7.2,导轨6.0.3兼容。 Docker组成部分:
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
答案 9 :(得分:0)
这是我在每个部署(包括第一个部署)中运行的命令的列表
//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