ActiveRecord::AdapterNotSpecified database configuration does not specify adapter
Asked Answered
A

8

39

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]
Archaeology answered 28/4, 2014 at 9:0 Comment(9)
Is that your entire database.yml?Henhouse
yes that is all of itArchaeology
I copied it from here: devcenter.heroku.com/articles/…Archaeology
you haven env like production, development, test, then rails s this try load development env, but fail because can't find.Dagny
try adding development:\n <<: *default to the database.ymlDunagan
I have a config/environments/development.rb & config/environments/production.rb & config/environments/test.rb files would you like me to include the code for them?Archaeology
@UriAgassi where in the file should I add it, does it matter? I added it to the end and it did not workArchaeology
You did notice that it is two lines, right? with a two-space indentation before the <<:Dunagan
I get the following error when I do that: Psych::BadAlias Unknown alias: defaultArchaeology
D
24

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

Dunagan answered 28/4, 2014 at 10:42 Comment(9)
Do you know how I can find out if I have postgresql installed locally on my machine? I'm pretty sure I do but should just double check.Archaeology
run which psql - if you get an empty response - you do not have one installedDunagan
I ran that and got no response, I re-dowloaded postgresql and the same thing is still happening..Archaeology
did you install it?Dunagan
Yes, I did the installationArchaeology
I think it's just not connecting... Idk what I can do now.. Maybe just use sql for local environment then? and pg for production to heroku?Archaeology
You mean sqlite? definitely a possibility.Dunagan
yeah.. Damn I wanted to do pg. Do you have any other idea of what might be happening? I'm thinking this might be a windows related thing...Archaeology
let us continue this discussion in chatDunagan
C
23

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 #....
Cruiser answered 16/8, 2016 at 8:42 Comment(0)
H
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.

Henhouse answered 28/4, 2014 at 9:5 Comment(5)
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?Archaeology
Nice - makes my answer look n00bMachree
@Archaeology Check my edited answer. The last line says what elements you need to edit.Henhouse
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?Archaeology
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.Henhouse
D
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'] %>
Delphiadelphic answered 9/7, 2015 at 19:57 Comment(1)
As @JAGj said if you don't have a good identation then it will failRonna
M
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])
Maun answered 4/9, 2018 at 0:57 Comment(0)
M
1

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
Machree answered 28/4, 2014 at 9:4 Comment(8)
only development need for localhost.Dagny
Do i include the **** or is that supposed to be information I provide. Because if I'm supposed to put something there I have no idea what to putArchaeology
@user, yes you need to put your own details. Because you don't know what to put, I take it you've not set up a db server for your app? If not, I'll amend my answer with some infoMachree
You mean like Amazon S3? No I have not done that yetArchaeology
Amazon S3 is a file storage system (it's name is simple storage service). You'll need to set up a db locally for development, or use a third party from a host or something. I'll amend my answer for youMachree
I have a db/migrate/***bunchofrandomnumbers***_create_premails.rb file is that what you are referring to?Archaeology
Nope, that's a migration. You really need to look up how database servers work with rails :)Machree
@RichPeck rails doesnt set up a db server.Henhouse
T
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

Tolyl answered 14/8, 2020 at 16:7 Comment(0)
K
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'
Knish answered 26/4, 2023 at 12:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.