Error when attempting to migrate to postgres database on Heroku from Sinatra app

Viewed 34

I have deployed my Sinatra application to Heroku. I have a sqlite database but since Heroku requires postgres, I needed to change this. To do this I setup a Postgres database on Heroku and specified separate development and production databases in my database.yml file.

 development:
  adapter: sqlite3
  database: db/development.sqlite
  pool: 5

production:
  adapter: postgresql
  database: production
  pool: 5

I also changed the code in my environment file:

require 'bundler/setup'
Bundler.require
 
  configure :development do
  ENV['SINATRA_ENV'] ||= "development"
 
  require 'bundler/setup'
  Bundler.require(:default, ENV['SINATRA_ENV'])
 
  ActiveRecord::Base.establish_connection(
    :adapter => "sqlite3",
    :database => "db/#{ENV['SINATRA_ENV']}.sqlite"
   )
  end
 
  configure :production do
   db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/production')
  
   puts "db is: #{db}"

   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'
   )
  end

require_all 'app'

The error message that I receive is as follows: PG::ConnectionBad: connection to server at "127.0.0.1", port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections?

I'm not sure why it appears to be attempting to connect to localhost? I have tried specifying the host listed in my heroku database credentials but I still receive the same message. I can see the application deployed in heroku, I just can't log in. This application does work locally.

Any advice is appreciated.

0 Answers
Related