database configuration does not specify adapter - ruby-on-rails

I'm getting this error when I'm trying to connect to a mysql database. The problem is that the application works for weeks, and then randomly I get this message. When I get this error message the application is not able to reconnect to the database until I restart it.
I'm using a configuration file to connect to the database, and the adapter is specified...the database configuration is not generated at runtime.
Do you have any idea on what is going on?

when I tried to run a command line script (let's say 'my_script' here), the same error happened. The reasons were:
There is only production environment there.
I missed to set RAILS_ENV for the command line.
So, the following is the solution in my case:
$ RAILS_ENV=production my_script

I just had this problem, and it was caused by a typo in my configration.yml.
I originally had this:
production:
adapter:mysql
When I meant to have this:
production:
adapter: mysql
That one little space between adapter: and mysql makes the difference.

Another possible cause:
In Rails 3.2.x, establish_connection has a default argument set from the environment:
From connection_specification.rb:
def self.establish_connection(spec = ENV["DATABASE_URL"])
resolver = ConnectionSpecification::Resolver.new spec, configurations
spec = resolver.spec
The way ConnectionSpecification::Resolver works depends on ENV['DATABASE_URL'] giving a nil if not set. (Normally, it would be something like 'postgres://...').
So, if you happen to have misconfigured DATABASE_URL such that ENV['DATABASE_URL'] == '', that will give you database configuration does not specify adapter.

I had this error when I mistakenly started rails server with
sudo rails s -e "Production" -p 80
and I should have started rails with
sudo rails s -e "production" -p 80

I found another thing that can cause this problem: "mixing in" another YAML node using & and *.
I was originally doing something like the following to facilitate local, per-develop, Git-ignored config files:
http://blog.lathi.net/articles/2006/03/02/config-database-yml-goodness-for-teams
But, after some debugging, I came to find out that establish_connection was for some reason being called with only the mixed-in key-value pairs and not the main ones. I.e. adapter, host, and database were not being passed in. I have no idea why, and this used to work for me.
Anyhow, instead of mixing in another YAML node, I now put the entire development and test hashes in the local config file, and establish_connection is once again being called correctly.

For me, this command resolved the issue.
rake db:migrate RAILS_ENV=production

I've found a couple of clues that this might be related to older library (ActiveRecord) or gem versions. For example, problems with fixtures even though rest of app seems okay (after an upgrade) or this trac ticket, which "stops gems from requiring an adapter from an old Active Record gem". Both of these are old, though, but it might be worth making sure your gems are up to date (if possible).
Are you using the native rails MySQL adapter by any chance? This is now deprecated under rails, but it's conceivable it's still limping along.
I've taken a very quick look at connection_specification.rb, too, which is where this error is coming from, and my best guess is that a reconnect is failing... but why (since it was obviously okay when you first started the app)? Are you doing something wild like calling ActiveRecord::Base.establish_connection in your application controller (or elsewhere)?
Or perhaps something like: script runner is called from cron in the dead of night when the connection has dropped. Unfortunately, the runner is invoked with an incorrect RAILS_ENV. Thus the wrong stanza is read from database.yml, and that stanza contains an invalid adapter:?

I got the same error, by typing in the following command:
db:migrate RAILS_ENV=product
Should've been:
db:migrate RAILS_ENV=production

If you get this error while deploying with Capistrano. Make sure you are setting the correct RAILS_ENV via
set :rails_env, 'production'
For example I was not explicitly setting the Rails environment in for the Capistrano staging deployment configuration. And thus Capistrano used 'staging' as the RAILS_ENV, resulting in the above error. Setting it to production like above in the staging.rb file solved the issue.

Do remember that RAILS_ENV=staging will look for a staging specification in your database.yml just as setting RAILS_ENV=production will look for a production specification in database.yml file.
Provide database configs, as shown below, for every rails environment you target using, for example
bundle exec cap staging deploy
production:
adapter: mysql2
encoding: utf8
database: rails
username: rails
password: pass
host: 127.0.0.1
port: 3306
pool: 5
timeout: 5000
staging:
adapter: mysql2
encoding: utf8
database: rails
username: rails
password: pass
host: 127.0.0.1
port: 3306
pool: 5
timeout: 5000

Remember to use the C-Based ruby gem for mysql. The ruby-based is unstable for production.
Try installing the gem
gem install mysql
Remember to copy libmySQL.dll into the ruby bin directory.

I had this error with another problem; I had specified 'development' twice, and 'test' not at all.

There are alot of bad tutorials out there on the internet that show yaml files like so:
development:
encoding: utf
database: dbname
...etc
YAML files are case sensitive and require TWO SPACES for the inner contents of each given db-type attribute. Like so:
development:
encoding: utf
database: dbname
...etc
UPDATE: I got this error again today. My VPS server had installed Rails 3.2.8 when my app was running Rails 3.2.6.
Definitely check your Gemfile and your database.yml file (of course). The problem here is clearly stated---Rails is not communicating with your database specifically due to an adapter (aka gem)

We had this issue with one of our older apps. Someone had created a boutique named environment, that even if RAIL_ENV was set to production, it was looking for a database configuration called legacy_<RAIL_ENV>, so I had to make a database environment called legacy_production for this app to work.
If you are maintain someone else's app, I would look for a copy of this app's database.yml that is working, perhaps it has some oddly named configuration. You can search your codebase for establish_connection to see if it is defining some strange variant.

I met this problem due to the 'multiple database support issue'. In my app/model folder, there is a file defined a redundant database connection:
class CacheCleanerActiveRecord < ActiveRecord::Base
establish_connection "cache_cleaner_#{Rails.env}"
self.abstract_class = true
end
but this database is not found in my database.yml ( because it's not used at all ).
so the solution is quit simple: remove this file and everything is fine !

You may have an error like:
RAILS_ENV= test
A space after the equals sign is not allowed, change it to:
RAILS_ENV=test

This happens to me, finally I found that RAILS_ENV is case sensitive, in my enviroment I set
RAILS_ENV=DEVELOPMENT , which is wrong, the value of RAILS_ENV must be lowercase.
$ RAILS_ENV=DEVELOPMENT rails server webrick
=> Booting WEBrick
=> Rails 4.2.5 application starting in DEVELOPMENT on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
config.eager_load is set to nil. Please update your config/environments/*.rb files accordingly:
* development - set it to false
* test - set it to false (unless you use a tool that preloads your test environment)
* production - set it to true
Exiting
/home/fangxing/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/connection_specification.rb:248:in `resolve_symbol_c
onnection': 'DEVELOPMENT' database is not configured. Available: ["default", "development", "test", "production"] (ActiveRecord::AdapterNotSpecified)
from /home/fangxing/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/connection_specification.rb:211:in `res
olve_connection'
$ RAILS_ENV=development rails server webrick
RubyDep: WARNING: Your Ruby is outdated/buggy. (To disable warnings, set RUBY_DEP_GEM_SILENCE_WARNINGS=1)
RubyDep: WARNING: Your Ruby is: 2.3.0 (buggy). Recommendation: install 2.3.1.
=> Booting WEBrick
=> Rails 4.2.5 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2016-07-20 16:41:09] INFO WEBrick 1.3.1
[2016-07-20 16:41:09] INFO ruby 2.3.0 (2015-12-25) [x86_64-linux]
[2016-07-20 16:41:09] INFO WEBrick::HTTPServer#start: pid=19881 port=3000

rails -e "production" is okay
only rails -e production returns error
database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)

call rake assets:precompile:all

This is probably not the most likely issue to cause this error, but here it is just in case.
My problem was that I was building the database settings in a Hash using symbols as keys and then serializing it with #to_yaml to database.yaml. ActiveRecord expects the environment names to be Strings, not Symbols, so it wasn't picking up the database settings when reading the generated file. I fixed it by using string keys in the hash.

For Rails4, commenting the line fetch(:default_env).merge!(rails_env: 'production') in production.rb and adding set :rails_env, :production fixed it.

You need to specify the environment when running the server or command as your database.yml file may have only production adapter while simply runnig rake db:migrate for example will take environment variable as development.

Just for the sake of completeness, I just got this error because I natively created a parametrised Rails runner script that takes an email address, and named the command line option -e -- which of course is the one the Rails runner uses for the environment. So it was trying to find an environment configuration that matched the email address!
Luckily, just before the ActiveRecord error mentioned in the title, it gave me an error message that helped me twig what the problem actually was:
You did not specify how you would like Rails to report deprecation notices for your test#example.com environment, please set config.active_support.deprecation to :log, :notify or :stderr at config/environments/test#example.com.rb

Check the spelling of adapter I had adaptor and got this error.

I had this error message when upgrading from Rails 4 to 5. I was calling
establish_connection "myconnection"
where "myconnection" is a valid key in my database.yml. However, passing this parameter as a string is apparently no longer supported. Using a symbol instead got rid of the problem.

I got this from copying and pasting sudo rails server webrick –e production -d from documentation into the CLI.
I'll shut the door on my way out 😔

Related

when running 'rake db:setup' getting deprecation warning

hope someone can help me here:
I have
* changed the database in my application from sqlit3 to postgresql,
* replaced the gemfile to gem 'pg',
* update database.yml
all this, in order to be able to deploy to heroku.
when I run
rake db:setup
I am getting these errors:
DEPRECATION WARNING: Single arity template handlers are deprecated. Template handlers must
now accept two parameters, the view object and the source for the view object.
Change:
>> Coffee::Rails::TemplateHandler.call(template)
To:
>> Coffee::Rails::TemplateHandler.call(template, source)
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
Couldn't create 'school-grades-project' database. Please check your configuration.
rake aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
I read that jbuilder should take care on the issue, so I upgraded jbuilder, but still the problem remains.
would appreciate some help here.
thanks,
It's a bit hard to diagnose this because you didn't specify the version of Rails that you're using or the contents of your database.yml file. But assuming it's a recent Rails version with support for database URLs it should work by following the descriptions in the Getting started guide for Rails.
# 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'] %>
#
Did you uncomment the production settings?
Here I have a quick solution.
This issue came into existence after updating the application to rails 6.x, To resolve it update the following gems:
Updating coffee-rails to 5.0.0 fixed this for me
Then run the bundle update and start the application server solved the warning issue.

How to run migrations on a different database server through Heroku?

We need to add the credentials of a database in database.yml file under a different environment like remote_database:
remote_database:
adapter:
encoding:
username:
...
And after adding all this, running the following command from the local terminal does the job:
RAILS_ENV=remote_database rails db:migrate
I'm trying to accomplish the same thing on Heroku. I have pushed the changes in config/database.yml, and I'm trying to execute the following command:
RAILS_ENV=remote_database heroku run rake db:migrate
# or
heroku run rake db:migrate RAILS_ENV=remote_database
Seems like Heroku is completely ignoring RAILS_ENV or the settings for remote_database env in config/database.yml file. Heroku always makes the changes in the regular database server connected with it that can be found at DATABASE_URL.
Is there a way to run the migrations on a different database server through Heroku?
Heroku injects database.yml and overrides it completely with Rails under 4.1 version or overrides partially and allows a way for us to prevent overriding from Rails 4.1. Check the complete explanation about Rails database connection behaviour on Heroku article
So, in your case
If you are using Rails 4.1+: you may try to add url key to your database.yml as described in Active Record 4.1+ Escape Valve section of above link.
If you are using Rails under 4.1 version: override database connection by an initializer. See Heroku article ("Otherwise if you are using an older version of Rails you will need to use an initializer" section)

Production entry for rails database.yml file on Heroku

Just looking at my database.yml file for my Rails 4 app and noticed there isn't any setting for the production database - only the test and development ones.
I don't actually have any issues but wanted to avoid other potential issues by posting this question.
Locally I use a PG database and I host my app on Heroku using a PG db too.
Should I have some settings in the yml for the production database file as I'm really not sure?
Whatever you place in database.yml will be overwritten by Heroku during production deployment, as it uses its own application configuration to effectively write a new one.
https://devcenter.heroku.com/articles/ruby-support#build-behavior
Unless you need to deploy the database.yml to other environments, you can include it in gitignore
The above answers are no longer valid for Rails 4.x on Heroku. Heroku no longer overwrites your database.yml. You need the following entry in your config/database.yml file:
production:
url: <%= ENV['DATABASE_URL'] %>
If you host your application on Heroku and use a Heroku Postgres DB you don't need a production entry in your database.yml.
Heroku will replace your database.yml entirely with one that uses the DATABASE_URL from heroku:config.
So no, you don't need a production entry in your database.yml for Heroku-hosted apps.

Create a new environment in Rails

I am not an experienced Rails developer, developing a Rails v2.3 application with MySQl v5.1 .
I am not sure how to achieve the following thing:
I need to create:
1. A new environment (that's a new environment besides development,
production and test environments) named 'special' environment
2. A new database environment for above created special environment , what I did is to add the following thing in config/database.yml
special:
adapter: mysql2
host: localhost
username:
database: special_db
encoding: latin1
3. A rake task to run under the special environment and the code in the rake task only deal with the special database.
To achieve this, I know I need to define some configurations, but not sure:
What/How exactly are needed to configure to create the special environment & database?
(As you saw above, I only defined the special database in database.yml file, but where & how to define and configuration for the new enverionment?)
How to run the rake task code under the special environment and only deal with the special database in Rails. Could someone please
P.S.
I need to run everything in a rake task not from command line. How to change environment and how to check the change?
--------------Found reason, but not sure the solution---------------------
Ok, I found the reason of this wired problem is because of the mysql2 gem which seems can not load the new "special" environment, if I switch to use mysql gem , the problem will be gone. But this probject has to use mysql2 for some other reason. How to get rid of this mysql2 problem?
Try the following:
Copy the config/environments/development.rb to config/environments/special.rb
Create the database using
$ RAILS_ENV=special rake db:create
$ RAILS_ENV=special rake db:migrate
$ RAILS_ENV=special rails s
Put this into your rake task:
RAILS_ENV = 'special'

Heroku App is Reading database.yml File

From what I can gather, Heroku is supposed to generate a database.yml file automatically, and ignore the local one. However, I am seeing an error where that is not true, and my changes to the local database.yml are affecting the Heroku app. This is problematic because I have no idea how I should setup production portion of the file so Heroku can find the right database.
For instance with the following
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
followed by the db:migration
$:~/Apps/DancingCupid/DancingCupid$ heroku rake --trace db:migrate
spits out
rake aborted!
unable to open database file
/app/.bundle/gems/ruby/1.8/gems/activerecord-3.0.5/lib/active_record/connection_adapters/sqlite3_adapter.rb:27:in `initialize'
...
I can get different errors depending on what type of database I sent for production.
Besides deleting the app and making a new one, is there a way to fix this problem?
Heroku definitely rewrite your database.yml on push so it doesn't matter what is in there in source control.
To confirm this do heroku run bash which will connect you to a bash session in your app then look do a cat config\database.yml and you will see how they have rewritten it.
The other answers are NOT TRUE anymore as of Rails 4.1.
config/database.yml won’t be overwritten anymore when a Rails 4.1.0
RC1 app is detected. Instead, it will be merged with DATABASE_URL so
additional options like pool size can be set in config/database.yml.
To double-check the contents of your database.yml on the Heroku server, you can run remote bash via heroku run bash and then cat config/database.yml to see its contents on the server and compare with your local one.
I don't think you are insane! ( But I thought I was )
I have been beating my way around this problem for a few days, and finally found this article:
http://article.gmane.org/gmane.comp.lang.ruby.rails.heroku/1003/match=database+yml
It led me to believe that maybe it wasn't my code at all!
I then simply destroyed my heroku app and created a new one, and pushed to it. Suddenly everything works fine! I don't know how or when or why, but I think it is possible to overwrite or corrupt the database.yml file that Heroku creates.
Hope this helps!
Try removing database.yml from version control. It's good practice to make a copy of database.yml into something like database.yml.example and adding database.yml to your .gitignore file.
That way when you push to Heroku it won't have any database configuration to refer to.
You probably also don't want the sqlite3 gem in production. Make sure it's in the development/test groups in your Gemfile.
database.yml should not be pushed to Heroku. It will try to connect to that database, timeout and then crash.
Add it to your .gitignore so it doesn't get up there.

Resources