1

I get "connection bad" when I try to connect to my PostgreSQL database from my Sinatra application. I'm using Heroku to deploy the app and my workstation is currently Windows. i tried, still same error message:

heroku addons:create heroku-postgresql:hobby-dev

From the log:

PG::ConnectionBad - could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?

From config/enviironments.rb:

db = URI.parse(ENV["DATABASE_URL"] || "postgres://localhost")

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

This is the main app.rb file:

require 'sinatra'
require 'haml'
require 'sass/plugin/rack'
require 'pony'
require 'active_record'
require 'pg'
require 'postgresql'
require './vars.rb'
require './includes/functions'
require './config/environments'
Sass::Plugin.options[:style] = :compressed
use Sass::Plugin::Rack
set :haml, :format => :html5
get '/admin' do
  admin_haml :'admin/index'
end
get '/admin/users' do
  admin_haml :'admin/users'
end

This is the function to test PostgreSQL:

def dbdeb
conn = PG.connect :user => "postgres"
  output = conn.server_version
  conn.close
  return output
end

This is the gemfile:

source 'http://rubygems.org'
ruby '2.2.3'
gem 'sinatra', '1.1.0'
gem 'haml', '4.0.7'
gem 'sass', '3.4.20'
gem 'activerecord','4.2.5'
gem 'sinatra-activerecord'
gem 'pg'
gem 'postgresql'
gem 'pony','1.11'

What did I do wrong?

1
  • Well you ARE calling PG.connect :host => "localhost" in your dbdeb method, but the Posgres-Server is not on localhost :) Commented Dec 22, 2015 at 14:28

1 Answer 1

2

It looks as if you do not have a DATABASE_URL environment variable defined for your Heroku app.

Try running the following command to see what environment variables are set on your Heroku app:

$ heroku config

This should list what variables are available. As you will see, there is no DATABASE_URL variable set.

To set this variable, you need to create a Heroku Postgres database:

$ heroku addons:create heroku-postgresql:hobby-basic

This will create two environment variables in your Heroku app:

  • DATABASE_URL
  • HEROKU_POSTGRES_COLOR

You can find more about this in the Heroku postgres docs: https://devcenter.heroku.com/articles/heroku-postgresql#create-a-new-database

Sign up to request clarification or add additional context in comments.

6 Comments

i did as you told me but I still get the same error message.
Do you see a DATABASE_URL environment variable listed when you run heroku config on the command line?
yes, i do see the DATABASE variable and still have same error message. @rdegges
In this case, there is likely some other code in your ruby app which is connecting exclusively to localhost. The error you're receiving shows that you're trying to connect to localhost, which implies that your DATABASE_URL variable is not being used properly. I'd look at the code for something like that.
I am pretty sure it is the environments.rb or the functions.rb that causes the error. but I don't what to change.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.