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?
PG.connect :host => "localhost"in yourdbdebmethod, but the Posgres-Server is not on localhost :)