I am trying to launch a web app in production mode in ruby from an Oracle VM Ubuntu but apparently my database is not configured.
I wrote this snippet in order to try launching it in production mode:
export RAILS_SERVE_STATIC_FILES=true
export SECRET_KEY_BASE='the randomly generated secret key'
rails server --environment production
But I am getting this exception:
=> Booting Puma
> Rails 5.1.7 application starting in production
=> Run `rails server -h` for more startup options
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
.log). The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.bucket_list/log/production
Exiting
Traceback (most recent call last):
90: from bin/rails:3:in `<main>'
...
/home/vagrant/.rvm/gems/ruby-2.6.5/gems/activerecord-5.1.7/lib/active_record/connection_adapters/connection_specification.rb:246:
in `resolve_symbol_connection':' database is not configured. Available: ["default", "development", "test", "production"]
(ActiveRecord::AdapterNotSpecified)
My database.yml
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
The config.eager_load is set as asked in the error message in my environments/* files and I don't understand why it says it is set to nil.
Do you have any clue on what could be wrong?
Thanks
Related
An application in production mode is returning content as expected. It is assumed it is connecting to the configured database. The database.yml file is as follows
default: &default
adapter: postgresql
user: deploy
password: 888xxx888xxx
schema_search_path: public
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: 5
timeout: 10000
development:
<<: *default
database: fort_development
<<: *default
database: fort_test
production:
<<: *default
database: fort_production
However, when connecting to the database via the console, whether in development (where the db does not exist) or in production, the console is connecting to the wrong database. It is also referencing a username which was removed from the database.yml file (and app subsequently restarted).
note: at one time the string myapp was referenced in the yml file for the schema_search_path attribute, however it was a commented out line (There is no certainty as to whether it was ever activated.'
> bundle exec rails c
irb(main):001:0> ActiveRecord::Base.connection_config
=> {:adapter=>"postgresql", :user=>"deploy", :password=>"888xxx888xxx", :schema_search_path=>"public", :pool=>5, :timeout=>10000, :database=>"myapp", :username=>"deploy", :host=>"127.0.0.1"}
> bundle exec rails c production
irb(main):001:0> ActiveRecord::Base.connection_config
=> {:adapter=>"postgresql", :user=>"deploy", :password=>"888xxx888xxx", :schema_search_path=>"public", :pool=>5, :timeout=>10000, :database=>"myapp", :username=>"deploy", :host=>"127.0.0.1"}
The console and the app are not in synch. What can make the console work with different values and how can it be reset to work with the proper configuration data?
For anyone stumbling upon this, there is another place where rake can take instructions for connecting via ActiveRecord.
.rbenv-vars
this file within the directory for the application was mistakenly set as:
DATABASE_URL=postgresql://deploy:PASSWORD#127.0.0.1/myapp
Another potential sources of problems (not clear why as the files properly parses in yaml) is to have the database.yml file only reference the production environment
production:
adapter: postgresql
database: fort_production
password: [...]
pool: 5
schema_search_path: public
timeout: 10000
user: deploy
I am using MAC OS X along with postgresql installed via Homebrew. I am developing using Rails 4.2.1 and ruby 2.2.0. The connection with postgresql server is fine but for some reason every application accesses the database "kstavrou" which is my system username, as a development database and creates the rest as defined by database.yml. That is troublesome if you have more than 1 rails app.
rake db:create output:
Konstantinoss-MacBook-Pro:ecomm-intel kstavrou$ rake db:create
kstavrou already exists
ecomm_intel_test already exists
strange thing is that if I empty database.yml still connects fine to postgresql and tries to create again the database "kstavrou" executing there all the migrations, without trying to create the test database.
rake db:create output:
Konstantinoss-MacBook-Pro:ecomm-intel kstavrou$ rake db:create
kstavrou already exists
database.yml
default: &default
adapter: postgresql
host: localhost
encoding: utf8
username: pguser
password: 123456
pool: 5
production:
<<: *default
database: ecomm_intel_prod
development:
<<: *default
database: ecomm_intel_dev
test:
<<: *default
database: ecomm_intel_test
Well the problem was that the ENV['DATABASE_URL'] was set (by some install script) so it was overwriting the database.yml configuration, as noted by steve klein, so I just removed it.
`export DATABASE_URL=postgres:///$(whoami)`
I can't make my sinatra/ruby app hosted on heroku works as desired. I fiddled with some setup trying to resolve this issue but so far no results.
ActiveRecord::ConnectionNotEstablished - No connection pool for User:
2015-06-25T14:26:11.736854+00:00 app[web.1]: /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:566:in `retrieve_connection'
2015-06-25T14:26:11.736856+00:00 app[web.1]: /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.1/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
2015-06-25T14:26:11.736858+00:00 app[web.1]: /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.1/lib/active_record/connection_handling.rb:87:in `connection'
User is one of my ActiveRecords table and the app fails because I try to query it.
I use sinatra with puma backup. Here is my Procfile:
web: ruby app/my-server.rb -s puma
I was also checking how many open connections there is using:
select count(*) from pg_stat_activity where pid <> pg_backend_pid() and usename = current_user;
but its says 0 every time.
I'm hosting the app on free plan and dev plan of herokupostgres.
I also noticed that the problem occurs when there are 2 quick calls to api at short interval of time. Like there was only 1, not 5 connections available, because 1st call succeds and the second one fails. In my database.yml I setup pool to 5.
I'm on Rails 4.2.1 and Postgres 9.4
Here is my database.yml aswell:
default: &default
adapter: postgresql
encoding: utf8
pool: 5
timeout: 5000
production:
<<: *default
host: my_db_address
port: 5432
database: my_db_name
username: my_db_user_name
password: my_db_password
< test and development ommited >
Do I miss some configuration or does free heroku plan chokes on it?
Please check how your sinatra app established the connection
configure :production, :development do
db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/mydb')
pool = ENV["DB_POOL"] || ENV['MAX_THREADS'] || 5
ActiveRecord::Base.establish_connection(
adapter: db.scheme == 'postgres' ? 'postgresql' : db.scheme,
host: db.host,
username: db.user,
password: db.password,
database: db.path[1..-1],
encoding: 'utf8',
pool: pool
)
end
Make sure you have proper settings for pool, also make sure you have a heroku config for DB_POOL or MAX_THREADS.
heroku config:set DB_POOL=5
or
heroku config:set MAX_THREADS=5
I'm confused about how to configure the DB in a Rails 4.2 app that uses Postgres and Heroku.
Following the advice in this Heroku guide, you'll get a config/database.yml like this:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
development:
<<: *default
database: app_name_development
test:
<<: *default
database: app_name_test
production:
<<: *default
database: app_name_production
But when I tried this, my development and test environment were using the same DB as the staging environment (note the file has no configuration for staging). That's no good.
This Heroku guide for connecting to the DB in Ruby mentions that any Rails apps before 4.2 would have their database.yml file overwritten by Heroku. Heroku will parse the DATABASE_URL environment variable and create a new database.yml file.
So I guess it's possible to just leave out the configuration in database.yml for any environments that you have on Heroku, such as staging and production. Your database.yml file could essentially look like Hound's (note the lack of a production configuration).
development: &default
adapter: postgresql
encoding: unicode
database: app_development
pool: 5
test:
<<: *default
database: app_test
But since we're using Rails 4.2, I don't think Heroku will override the database.yml file. In that case, do you have to specify DB configuration in database.yml for our environments on Heroku? Or is it still safe to leave them out? If we do need to specify the configuration for Heroku environments, will the following be sufficient?
staging:
url: <%= ENV['DATABASE_URL'] %>
production:
url: <%= ENV['DATABASE_URL'] %>
I'm also confused about the proper configuration for the development and test environments. As I mentioned above, the first configuration shown has those environments using the staging DB on Heroku, instead of a local DB.
This Heroku guide says to export the DATABASE_URL environment variable for your app to connect (once Postgres is installed and you can connect to it).
Assuming you export the DATABASE_URL env var as specified in the article, what does your configuration for development and test have to look like? Do we go with the configuration as shown in the first guide, e.g.
default: &default
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
development:
<<: *default
database: app_name_development
test:
<<: *default
database: app_name_test
Or do we use a configuration as shown in this Heroku guide (which uses host and username)
development:
adapter: postgresql
host: localhost
username: user
database: app-dev
Update 1: Here's what I now know. A staging and production config isn't necessary in config/database.yml if you deploy to Heroku, no matter your Rails version. Prior to 4.2, Heroku would generate it's own database.yml file based on the value of the DATABASE_URL environment variable, overwriting your config file (if it exists). Since Rails 4.2, your app will use the DATABASE_URL environment variable directly (bypassing the database.yml file), so Heroku doesn't need to (and won't) generate a config file.
I also figured out why my development and test environments were using the remote staging DB from the Heroku app instead of the local DBs specified in their database.yml config. It was because my local .env file for development is based off of my staging .env file which contains environment variables for connecting to the database such as DATABASE_URL. Because DATABASE_URL was present in my development .env file, my Rails 4.2 app was using it and thus connecting to the staging DB. To fix it, I removed those environment variables from the development .env file and created the local DBs (and ran the migrations) with bundle exec rake db:setup.
Update 2: This section of the Rails Guides goes into more detail about how to configure the DB, worth a read: http://guides.rubyonrails.org/configuring.html#configuring-a-database
Most of your assumptions are correct. The following is a reasonable database.yml configuration file.
default: &default
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
development:
<<: *default
database: app_name_development
test:
<<: *default
database: app_name_test
staging:
url: <%= ENV['DATABASE_URL'] %>
production:
url: <%= ENV['DATABASE_URL'] %>
Make sure that the RAILS_ENV is properly set on Heroku (either staging or production), or Rails will default to development.
Locally, the test will pick the test environment. By default, the app will start in development mode, using the development environment.
In fact, many developers choose to ignore the database.yml in version control and never publish it into the repo. The reason for that is, that databases may be different on different machines, that's a reason not to keep the configuration common.
I'm working on a Rails 4.2 project right now, and Heroku has no problem with having no database.yml at all (both with PostgreSQL and MySQL, we tested both). Why? Because DATABASE_URL provides all the information necessary to access the database, even adapter name. How? Here's the formula:
adapter://username:password#hostname:port/database?options
Locally, I'm using Postgres with peer authentication: the database server assumes the same username that I'm using in my OS, username is covered, password is useless. Local machine is assumed when no host is given, although I can't tell if it tries to communicate via TCP/IP or Unix domain sockets, so I'm fine without host.
So the configuration you refer to as "shown in the first guide" is reasonable: it contains a minimum amount of settings and allows you to create more environments quite easily.
Heroku doesn't create a database.yml on rails 4.2 because as of that version rails will detect the presence of that environment variable and use it to configure the database connection.
Adding
production:
url: <%= ENV['DATABASE_URL'] %>
Makes it a little more obvious what is happening for those who might not be aware of it but won't change the behaviour. The configuring rails guide has more info in interactions between database.yml and DATABASE_URL if you need it.
To connect with ActiveRecord without Rails (e.g. sinatra):
url = URI.parse(ENV['DATABASE_URL'])
ActiveRecord::Base.establish_connection(
encoding: 'unicode',
pool: 5,
timeout: 5000,
reconnect: true,
adapter: url.scheme,
host: url.host,
database: url.path.sub(%r{^/}, ''),
username: url.user,
password: url.password
)
rake aborted! Cannot load Rails.application.database_configuration: can't convert nil into Hash
I am trying to deploy ruby on rails on openshift, and then i try to run rake db:migrate. Then i face this problem.
I create the app with https://github.com/kohjx/TestYourCode.git on openshift with ruby on rails + mysql 5.1
Then i configure the config/database.yml
default: &default
#adapter: sqlite3
adapter: mysql2
pool: 5
timeout: 5000
database: "<%=ENV['OPENSHIFT_APP_NAME']%>"
username: "<%=ENV['OPENSHIFT_MYSQL_DB_USERNAME']%>"
password: "<%=ENV['OPENSHIFT_MYSQL_DB_PASSWORD']%>"
host: <%=ENV['OPENSHIFT_MYSQL_DB_HOST']%>
port: <%=ENV['OPENSHIFT_MYSQL_DB_PORT']%>
development:
<<: *default
#database: db/development.sqlite3
test:
<<: *default
#database: db/test.sqlite3
database: testyourcode_test
production:
<<: *default
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
#database: db/production.sqlite3
I setup the RAILS_ENV=development, but i can't figure out how to solve. Any help would be greatly appreciated!
At first glance it looks like you're using the Openshift DB as your default and then all your Rails environments are using that default setup. Your local machine (where I'm assuming you're running the rake db:migrate) isn't able to use those connection details to the OPenshift DB.
Instead of having the Openshift DB configuration in the default section move it down to the production setting and switch the RAILS_ENV=production with rhc set-env RAILS_ENV=production. Then configure the development with settings that your localmachine can use. After thats all done, try running your db:migrate once more.