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
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
For you app to work locally you need to:
my_app_development
)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
run rake db:migrate
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'] %>
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
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.
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])