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]
For you app to work locally you need to:
Install Postgresql on your machine
Create a database for your development needs (let's call it 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
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 #....
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.
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'] %>
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])
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
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
Related
I'm trying to set up a SQLite Db for my sinatra app and am running into an error I can't figure out. I've looked up related questions but none of them have helped.
Up to this point I've run bundle exec rake db:create_migration NAME=create_budget
which created a migration in my db folder.
However when I run bundle exec rake db:migrate I get the following error message:
ActiveRecord::ConnectionNotEstablished: No connection pool with 'primary' found.
I am following the tutorial https://learn.co/lessons/sinatra-activerecord-setup
database.yml - the instructions did not say to create this but in researching the error this seemed to keep coming up. This is still magic to me as a learn so I'm not sure if activerecord requires this or not.
# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# 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:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
Migrate: created with the db:create_migration
# Database Migration
class CreateBudget < ActiveRecord::Migration[5.1]
def change
create_table :budget do |t|
t.string :description
t.string :amount
end
end
end
GemFile:
# frozen_string_literal: true
source 'https://rubygems.org'
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
# gem "rails"
gem 'activerecord'
gem 'rake'
gem 'require_all'
gem 'sinatra'
gem 'sinatra-activerecord'
gem 'slim'
gem 'thin'
group :development do
gem 'pry'
gem 'shotgun'
gem 'sqlite3'
gem 'tux'
end
File structure - not sure if this will be helpful or not
bundle exec rake db:create:all
Please help me to solve the following error. When I typed rake db:create in ruby cmd i got such type of error.
Error:
rake aborted!
NoMethodError: undefined method `each' for #<String:0x1a1af20>
Tasks: TOP => db:create => db:load_config
(See full trace by running task with --trace)
My database.yml and gem file is given below.
config/database.yml
# MySQL. Versions 5.0+ are recommended.
#
# Install the MYSQL driver
# gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
# gem 'mysql2'
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
#
default:&default
adapter:mysql2
encoding:utf8
pool:5
username:root
password:pass
host:localhost
development:<<:*default
database:mysqlapp_development
# 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:mysqlapp_test
# 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="mysql2://myuser:mypass#localhost/somedatabase"
#
# You can use this database configuration with:
#
# production:
# url: <%= ENV['DATABASE_URL'] %>
#
production:<<:*default
database:mysqlapp_production
username:mysqlapp
password:<%= ENV['MYSQLAPP_DATABASE_PASSWORD'] %>
Gemfile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.0'
# Use mysql as the database for Active Record
gem 'mysql2'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.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', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'debugger' anywhere in the code to stop execution and get a debugger console
gem 'debugger'
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
As I am new to ROR please help to solve this error and run the command successfully.I am using ruby version-1.9.3 and Rails version 4.0.2.
For me, it was a mistakenly uncommented comment line in the database.yml. For you, the format of your database.yml seems not right. Try and change the format of the following sections.
Replace
development:<<:*default
database:mysqlapp_development
with
development:
<<:*default
database:mysqlapp_development
I note that there have been many other similar questions, but my context seems to be different.
Error
The following is the error I am getting, trying to run/restart a Rails Application (Redmine). Redmine was running fine, but there have been some server updates by the shared host, thus seeing these problems.
mysql2 is listed in the gem/bundle list. mysql2.so exists in the location it is looking for, and has the required (executable) permissions. Thank you any suggestions in addressing this issue.
Error message:
Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (libruby.so.1.8: cannot open shared object file: No such file or directory - /home/test/gems/gems/mysql2-0.3.11/lib/mysql2/mysql2.so)
Exception class:
LoadError
Enviornment
# ruby -v
ruby 1.9.3p429 (2013-05-15 revision 40747) [x86_64-linux]
# rails -v
Rails 3.2.6
# gem -v
1.8.23
# mysql --version
mysql Ver 14.14 Distrib 5.5.34, for Linux (x86_64) using readline 5.1
#gem list
...
mysql2 (0.3.11)
...
#bundle show
...
mysql2 (0.3.11)
...
#vim database.yml
production:
adapter: mysql2
database: database_name
host: localhost
username: dbuser
password: dbpwd
encoding: utf8
Answer to this question solved my issue.
gem uninstall mysql2
bundle install
I am attempting to follow this Redmine setup tutorial. When I get to the point of starting the server, I type sudo rails server and I get the following error:
Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (mysql2 is not part of the bundle. Add it to Gemfile.) (LoadError)
The relevent section of my Gemfile reads:
....
if File.exist?(database_file)
database_config = YAML::load(ERB.new(IO.read(database_file)).result)
adapters = database_config.values.map {|c| c['adapter']}.compact.uniq
if adapters.any?
adapters.each do |adapter|
case adapter
when /mysql/
gem "mysql", "~> 2.8.1", :platforms => [:mri_18, :mingw_18]
gem "mysql2", "~> 0.3.11", :platforms => [:mri_19, :mingw_19]
gem "activerecord-jdbcmysql-adapter", :platforms => :jruby
...
and Gemfile.lock contains mysql2:
...
multi_json (1.5.0)
mysql (2.8.1)
mysql2 (0.3.11)
net-ldap (0.3.1)
...
and my database.yml file includes the following:
...
production:
adapter: mysql2
database: redmine
host: localhost
username: ****
password: ****
development:
adapter: mysql2
database: redmine_development
host: localhost
username: ****
password: ****
encoding: utf8
...
Running bundle install seems to succeed, but mysql2 is not listed in the output, which mysql2 returns nothing, and bundle show mysql2 returns Could not find gem 'mysql2' in the current bundle.
I have uninstalled and reinstalled the mysql2 gem, with apparent success each time.
I have read through descriptions of similar-sounding problems on other stackoverflow questions, but none of their solutions turned out to solve my problem.
You will get this error if mysql2 is not included in the Gemfile.lock file that's generated by bundle. While the Gemfile is used to manage dependencies, the lock file is what is actually loaded by the rails app.
That's a more verbose Gemfile than I have seen before (I'm not generally familiar with Redmine)- where is this from? It doesn't appear to be from the most recent stable source. If you know you'll be using mysql2, I don't think there's any reason you need your Gemfile to parse your database config. For whatever reason, it's not properly reading the database.yml file.
Try adding gem 'mysql2' outside any blocks or loops and run bundle again.
I have generated an app using 'install bundle' and am getting the error ActiveRecord::ConnectionNotEstablished in the "About your application’s environment" page of the welcome aboard app.
The Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.0'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
any ideas?
This is the generated gemfile + changes to the version numbers of rails & sqlite-ruby selected by process of elimination to get the bundler to install everything.
Some Background
I am going through a book so suggestions about different versions are fine.
I don't know what I'm doing.
I'm using OSX 10.5 (leopard)
Let me know if there is any more information I should add.
Bonus question: If I need to start from scratch with a different sqlite version, should I create a new app or can I user the Bundler again?
edit
Kleber S. has suggested configuring the configure your config/database.yml file the file looks like:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# 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:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
any ideas what's wrong?
you have probably to proper configure your config/database.yml file.
It may be necessary to create a new database on SQLite to fix that error.
bonus answer: you don't have to start it from scratch, just configure the database.yml file.
And what about just replacing this line :
gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'
By this line :
gem 'sqlite3'
In your gemfile?
EDIT : ok, forget about this, MAC OS 10.5 can't use sqlite3. So the gemfile seems to be correct.