38

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]
user3408293
  • 1,377
  • 6
  • 18
  • 26

8 Answers8

23

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

Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
22

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 #....
yekta
  • 3,363
  • 3
  • 35
  • 50
7

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.

cavpollo
  • 4,071
  • 2
  • 40
  • 64
SreekanthGS
  • 988
  • 5
  • 12
  • What do you mean by something like this? I am new to this so that is vague for me. What parts of this should I alter? – user3408293 Apr 28 '14 at 09:08
  • Nice - makes my answer look n00b – Richard Peck Apr 28 '14 at 09:10
  • @user3408293 Check my edited answer. The last line says what elements you need to edit. – SreekanthGS Apr 28 '14 at 09:14
  • Is development_database_name the file with random numbers that was created under my db/migrate folder? Also, where can I get my db username & password? Is it the Heroku name & password or something else? – user3408293 Apr 28 '14 at 09:18
  • You have to setup a local postgres installation, create a db in that, use its username and password in this configuration database.yml file. This has got nothing to do with your Heroku setup. There are plenty of tutorials out there which helps you setup postgresql based on your OS and help you understand how it can be used in rails. – SreekanthGS Apr 28 '14 at 09:26
3

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'] %>
JAGJ jdfoxito
  • 747
  • 5
  • 5
2

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])
lacostenycoder
  • 10,623
  • 4
  • 31
  • 48
0

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
Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
0

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

Ruslan Valeev
  • 1,529
  • 13
  • 24
0

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'
Mike Slinn
  • 7,705
  • 5
  • 51
  • 85