user3408293
user3408293

Reputation: 1395

ActiveRecord::AdapterNotSpecified database configuration does not specify adapter

When I use heroku open my web app works fine but when I'm using rails s (localhost) I am running into this error:

ActiveRecord::AdapterNotSpecified database configuration does not specify adapter

Why is this?

This is my database.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

And this is my 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]

Upvotes: 40

Views: 88423

Answers (8)

Mike Slinn
Mike Slinn

Reputation: 8403

In case you're trying to use Active Record without Rails (for example, if you are working with Sinatra, and you are using sinatra-activerecord), add the following to Rakefile:

require 'sinatra/activerecord'

Upvotes: 0

Ruslan Valeev
Ruslan Valeev

Reputation: 1907

In my case the reason was in my Rakefile.

when I run rake db:migrate, I got this:

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

I've found this row in my Rakefile:

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

and changed with default value: ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'] || 'postgres://localhost/db_name')

and now it works fine. Details you can find here

Upvotes: 0

lacostenycoder
lacostenycoder

Reputation: 11226

In case you're trying to use activerecord without rails you may run into this problem with a database.yml with multiple environment setups. So you'll need to pass the environment key into the config setup like this:

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

Upvotes: 2

yekta
yekta

Reputation: 3433

You didn't show the command causing this query, but this could happen if you pass a string and not a symbol.

For example:

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

But then if you use a symbol, it will work.

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

Upvotes: 23

SreekanthGS
SreekanthGS

Reputation: 988

Your database.yml should look something like this:

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"

Edit development_database_name to your local database name. Also edit my_username and my_password to your correct db username and password.

Upvotes: 7

JAGJ jdfoxito
JAGJ jdfoxito

Reputation: 777

Delete tabs nothing more, ident perfect, such as:

# 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'] %>

Upvotes: 3

Uri Agassi
Uri Agassi

Reputation: 37409

For you app to work locally you need to:

  1. Install Postgresql on your machine
  2. Create a database for your development needs (let's call it my_app_development)
  3. Change your database.yml to:

    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. run rake db:migrate

Upvotes: 24

Richard Peck
Richard Peck

Reputation: 76774

Why are you using a yml node reference in your database.yml?

You should have something like this:

#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

Update

Rails runs using a database. You have to connect to a db to make it work, and to do that you have to define the different connection details in database.yml

To define the right information, you need to appreciate that Rails operates in several environments - development & production being the two most used

To get Rails working in your local (development) environment, you need to define the correct db details. This means you need a database to connect to - which is typically done setting up a local mysql / pgsql server

Bottom line is you connect to a db using:

  • hostname
  • username
  • password
  • db name

You need to define these in your config/database.yml file

If you have a server running in your local environment, your database.yml file will look like this:

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

Upvotes: 1

Related Questions