ActiveRecord::ConnectionNotEstablished - No connection pool for X - ruby-on-rails

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

Related

Config exception launching web app in production mode Ruby

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

ActiveRecord connection requires reset?

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

Heroku Rails deploy fails with error "could not connect to server: Connection refused"

Everything works fine locally, but I get that error when I try to push to heroku:
Running: rake assets:precompile
Connecting to database specified by DATABASE_URL
rake aborted!
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
/tmp/build_1903c764-07ce-4d06-aa45-6e78dec68af8/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/connection_adapters/postgresql_adapter.rb:1216:in `initialize'
/tmp/build_1903c764-07ce-4d06-aa45-6e78dec68af8/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/connection_adapters/postgresql_adapter.rb:1216:in `new'
...
etc
I suspect it has to do something with database.yml, so here it is:
development:
adapter: mysql2
encoding: utf8
database: dev-db
pool: 5
timeout: 5000
socket: /tmp/mysql.sock
test:
adapter: mysql2
encoding: utf8
database: test-db
pool: 5
timeout: 5000
socket: /tmp/mysql.sock
production:
adapter: postgresql
encoding: utf8
database: prod-db
pool: 5
timeout: 5000
# socket: /tmp/mysql.sock
It might be something related with the fact that you haven't pre-provisioned your database before pushing to heroku. At least, I had a similar error and I solved it by running:
heroku addons:create heroku-postgresql
You can verify the database was added to your application by running:
heroku config
And you can read all about this question here: https://devcenter.heroku.com/articles/pre-provision-database
See this article. The solution is to set
config.assets.initialize_on_precompile = false
I know this is solved, but for others who see this, you might have to promote the database (in case you created a new one, restored to a new one or similar):
heroku pg:promote HEROKU_POSTGRESQL_IVORY(replace IVORY)

Working on a rails app locally with a remote Postgres connection?

Is there a way to configure the database.yml file to connect to Heroku's Postgres remotely?
I'm having trouble understanding how Heroku, Rails and PG gem work together.
It looks like during deployment, Heroku re-writes the database.yml file - is it possible to see the contents of this updated .yml file and use it locally?
Below are the steps to access Heroku db from local development:
Login to your heroku account.
Navigate to this URL https://postgres.heroku.com/databases/ OR you can get heroku db url by running this command in the terminal: heroku pg:credentials:url
Select your application database.
You will able to see your heroku pg credentials:
Host xxxxxxxxx.77777.amazonaws.com
Database 42feddfddeee
Username 44444444444
Port xxxx
Password 777sfsadferwefsdferwefsdf
collect above details and put in your databse.yml file development evn:
adapter: postgresql
host: < host > # HOST
port: < port > # Port
database: < database name > # Database Name
username: < user_name > # User Name
password: '< password >' # Password
Restart you application,
Best of luck..!!
I am not a postgres user but this should work.
You can alter your database.yml to include host and port
production:
adapter: postgresql
encoding: unicode
database: di_production
pool: 5
username: user
password:
host: heroku.host.name
port: <postgres listen port>
And of course, you should allow connection on the server side, both at the firewall level and database level.
A simple hack to see contents of #{Rails.root}/config/database.yml is to write code to load this yml into an object, then print it out on the UI
DB_CONFIG = YAML.load(File.read("#{Rails.root}/config/database.yml", __FILE__))
puts DB_CONFIG["production"].inspect # or what ever method you want to print it
I am a bit of a newbie and may have misunderstood your question - but. Developed a ROR application using postgress database. Then uploaded to Heroku. I ran DB/Migrate/schema.rb to set up the remote Postgresql database.
From memory heroku run rake db:init (but I could be wrong). Whenver I update the database in develpment to get update in Heroku I have to promote code and run heroku run rake db:migrate
From my config/dtabase.yml
development:
adapter: postgresql
encoding: unicode
database: di_development
pool: 5
username: davidlee
password:
test:
adapter: postgresql
encoding: unicode
database: di_test
pool: 5
username: davidlee
password:
production:
adapter: postgresql
encoding: unicode
database: di_production
pool: 5
username: user
password:
and it works. I can't remember doing anything else.
Pierre

Database connection on Heroku

Wow I've been stuck on this one for days. I'm having trouble connecting to database.yml on Heroku. I'm on Cedar and ruby 1.9.2. My dev and test dbs are sqlite3 and the prod db is postgreSQL to cope with Cedar rules.
Here is the code in my ruby script:
Rails.env.production? ? (env = "production") : (env = "development")
dbconfig = YAML::load(File.open('config/database.yml'))[env]
ActiveRecord::Base.establish_connection(dbconfig)
All goes well in local but when I push to Heroku, I get:
ArgumentError: syntax error on line 17, col 0: `adapter = uri.scheme'
from /usr/local/lib/ruby/1.9.1/syck.rb:135:in `load'
It looks like Heroku doesn't like my database.yml. Here's an overview:
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: postgresql
encoding: unicode
database: foo
port: 5432
host: foobar.amazonaws.com
username: foo
password: bar
First, Heroku overwrites your config/database.yml with its own Heroku-specific version. That's how Heroku automatically connects your application to its own postgresql databases. To tell Heroku about your own posgresql database, you should set up the correct config variables and you might as well omit the production database from your the config/database.yml in your repository because Heroku will ignore it anyway.
Second, the config/database.yml file is an ERB template for a YAML file. You must first run the file contents through Evaluated Ruby (ERB) before running the the output through YAML.

Resources