【问题标题】:ActiveRecord::AdapterNotSpecified database configuration does not specify adapterActiveRecord::AdapterNotSpecified 数据库配置未指定适配器
【发布时间】:2014-06-13 17:50:58
【问题描述】:

当我使用 heroku 打开我的网络应用程序时工作正常,但是当我使用 rails s (localhost) 时,我遇到了这个错误:

ActiveRecord::AdapterNotSpecified database configuration does not specify adapter

这是为什么?

这是我的数据库.yml

# PostgreSQL. Versions 8.2 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On OS X with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 5

这是我的 gemfile:

source 'https://rubygems.org'


gem 'pg'

gem 'bootstrap-sass', '~> 3.1.1'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.3'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

group :production do
  gem 'rails_12factor', '0.0.2'
end

# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'

# Use unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]

【问题讨论】:

  • 那是你的整个 database.yml 吗?
  • 是的,就是这样
  • 我从这里复制的:devcenter.heroku.com/articles/…
  • 你有像productiondevelopmenttest这样的环境,然后是rails s这个尝试加载开发环境,但是因为找不到而失败。
  • 尝试将development:\n <<: *default 添加到database.yml

标签: ruby postgresql activerecord heroku ruby-on-rails-4


【解决方案1】:

要让您的应用在本地工作,您需要:

  1. Install Postgresql 在你的机器上
  2. Create a database 满足您的开发需求(我们称之为my_app_development
  3. 将您的 database.yml 更改为:

    default: &default
      adapter: postgresql
      encoding: unicode
      # For details on connection pooling, see rails configuration guide
      # http://guides.rubyonrails.org/configuring.html#database-pooling
      pool: 5
    
    development:
      <<: *default
      database: my_app_development
    
  4. 运行rake db:migrate

【讨论】:

  • 你知道我如何知道我的机器上是否安装了 postgresql 吗?我很确定我这样做了,但应该仔细检查一下。
  • 运行which psql - 如果你得到一个空响应 - 你没有安装一个
  • 我运行它并没有得到任何响应,我重新下载了 postgresql 并且同样的事情仍在发生..
  • 安装了吗?
  • 是的,我安装了
【解决方案2】:

您没有显示导致此查询的命令,但是如果您传递的是字符串而不是符号,则可能会发生这种情况。

例如:

irb(main):001:0> ActiveRecord::Base.establish_connection("#{Rails.env}")
ActiveRecord::AdapterNotSpecified: database configuration does not specify adapter

但是如果你使用一个符号,它就会起作用。

irb(main):001:0> ActiveRecord::Base.establish_connection("#{Rails.env}".to_sym)
=> #<ActiveRecord::ConnectionAdapters::ConnectionPool:0x007f2f484a32a0 #....

【讨论】:

    【解决方案3】:

    您的 database.yml 应该如下所示:

    default: &default
      adapter: postgresql
      encoding: unicode
      # For details on connection pooling, see rails configuration guide
      # http://guides.rubyonrails.org/configuring.html#database-pooling
      pool: 5
      username: my_username
      password: my_password
    
    development:
      <<: *default
      database: "development_database_name"
    
    test:
      <<: *default
      database: "test_database_name"
    
    production:
      <<: *default
      database: "production_database_name"
    

    将 development_database_name 编辑为您的本地数据库名称。 还将 my_username 和 my_password 编辑为正确的数据库用户名和密码。

    【讨论】:

    • 你这样说是什么意思?我是新手,所以这对我来说很模糊。我应该更改其中的哪些部分?
    • 很好 - 让我的答案看起来很不错
    • @user3408293 检查我编辑的答案。最后一行说明您需要编辑哪些元素。
    • development_database_name 是在我的 db/migrate 文件夹下创建的带有随机数的文件吗?另外,我在哪里可以获得我的数据库用户名和密码?是 Heroku 名称和密码还是其他?
    • 您必须设置本地 postgres 安装,在其中创建一个 db,在此配置 database.yml 文件中使用其用户名和密码。这与您的 Heroku 设置无关。有很多教程可以帮助您根据自己的操作系统设置 postgresql,并帮助您了解如何在 Rails 中使用它。
    【解决方案4】:

    删除标签而已,就完美了,比如:

    # PostgreSQL. Versions 8.2 and up are supported.
    #
    # Install the pg driver:
    #   gem install pg
    # On OS X with Homebrew:
    #   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
    # On OS X with MacPorts:
    #   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
    # On Windows:
    #   gem install pg
    #       Choose the win32 build.
    #       Install PostgreSQL and put its /bin directory on your path.
    #
    # Configure Using Gemfile
    # gem 'pg'
    #
    default: &default
      adapter: postgresql
      encoding: utf8
      pool: 5
      host: 192.168.0.121
      username: postgres
      password: passpostgres
    
    development:
      <<: *default
      database: DBPOSTGRES
     # Warning: The database defined as "test" will be erased and
     # re-generated from your development database when you run "rake".
     # Do not set this db to the same as development or production.
    test:
      <<: *default
      database: DBPOSTGRES
    # As with config/secrets.yml, you never want to store sensitive information,
    # like your database password, in your source code. If your source code is
    # ever seen by anyone, they now have access to your database.
    #
    # Instead, provide the password as a unix environment variable when you boot
    # the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
    # for a full rundown on how to provide these environment variables in a
    # production deployment.
    #
    # On Heroku and other platform providers, you may have a full connection URL
    # available as an environment variable. For example:
    #
    #   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
    #
    # You can use this database configuration with:
    #
    #   production:
    #     url: <%= ENV['DATABASE_URL'] %>
    #
    production:
      <<: *default
      database: DBPOSTGRES
      password: <%= ENV['passpostgres'] %>
    

    【讨论】:

    • 正如@JAGj 所说,如果你没有一个好的身份,那么它就会失败
    【解决方案5】:

    如果您尝试使用没有 rails 的 activerecord,您可能会在具有多个环境设置的 database.yml 中遇到此问题。因此,您需要将环境密钥传递到配置设置中,如下所示:

    DB_ENV ||= 'development'
    connection_details = YAML::load(File.open('config/database.yml'))
    ActiveRecord::Base.establish_connection(connection_details[DB_ENV])
    

    【讨论】:

      【解决方案6】:

      为什么在database.yml 中使用yml node reference

      你应该有这样的东西:

      #config/database.yml
      development:
        adapter: mysql2
        encoding: utf8
        database: ****
        pool: 5
        username: ****
        password: ****
        host: ***.***.***.*** #-> only for third party db server
      
      production:
        adapter: postgresql
        encoding: utf8
        database: ****
        pool: 5
        username: ****
        password: ****
        host: ***.***.***.*** #-> only for third party db server
      

      更新

      Rails 使用数据库运行。您必须连接到数据库以使其工作,为此您必须在 database.yml 中定义不同的连接详细信息

      要定义正确的信息,您需要了解 Rails 在多个 environments - developmentproduction 中运行,这是最常用的两个

      要让 Rails 在您的本地(开发)环境中工作,您需要定义正确的数据库详细信息。这意味着您需要一个数据库来连接 - 这通常是通过setting up a local mysql / pgsql server

      底线是您使用以下方式连接到数据库:

      • 主机名
      • 用户名
      • 密码
      • 数据库名称

      您需要在 config/database.yml 文件中定义这些

      如果您在本地环境中运行服务器,您的 database.yml 文件将如下所示:

      #config/database.yml
      development:
        adapter: mysql2
        encoding: utf8
        database: db_name
        pool: 5
        username: username
        password: password
      

      【讨论】:

      • 本地主机只需要开发。
      • 我是否包含 **** 或者应该是我提供的信息。因为如果我应该放一些东西我不知道放什么
      • @user,是的,您需要输入自己的详细信息。因为您不知道该放什么,所以我认为您还没有为您的应用程序设置数据库服务器?如果没有,我会用一些信息修改我的答案
      • 你的意思是像 Amazon S3?不,我还没有这样做
      • Amazon S3 是一个文件存储系统(它的名字是简单的存储服务)。您需要在本地设置一个数据库进行开发,或者使用来自主机或其他东西的第三方。我会为你修改我的答案
      【解决方案7】:

      就我而言,原因在于我的 Rakefile。

      当我运行rake db:migrate 时,我得到了这个:

      rake db:migrate
      rake aborted!
      ActiveRecord::AdapterNotSpecified: The `default_env` database is not configured for the `default_env` environment.
      
      Available databases configurations are:
      
      development
      test
      production
      

      我在我的 Rakefile 中找到了这一行:

      ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'])

      并更改为默认值: ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'] || 'postgres://localhost/db_name')

      现在它工作正常。详情可以找here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-25
        • 1970-01-01
        • 2015-11-26
        • 2022-08-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多