2015-12-21 95 views
1

当我尝试从我的Sinatra应用程序连接到我的PostgreSQL数据库时,出现“连接不良”。我使用Heroku来部署应用程序,而我的工作站目前是Windows。我试过了,还是同样的错误信息:Sinatra Heroku PostgreSQL连接不好

heroku addons:create heroku-postgresql:hobby-dev 

从日志:

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? 

从配置/ 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' 
) 

这是主要的app.rb文件:

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 

这是测试Pos tgreSQL:

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

这是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' 

我做了什么错?

+0

那么你调用'PG.connect:主机=> “本地主机”'在'dbdeb'方法,但Posgres-服务器不在本地主机** ** :) – mhutter

回答

2

看起来好像您没有为您的Heroku应用程序定义的DATABASE_URL环境变量。

尝试运行以下命令来查看您的Heroku应用程序设置了什么样的环境变量:

$ heroku config 

这应该列出哪些变量是可用的。如你所见,没有DATABASE_URL变量集。

要设置此变量,您需要创建的Heroku Postgres数据库:

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

这会在你的Heroku应用程序创建两个环境变量:

  • DATABASE_URL
  • HEROKU_POSTGRES_COLOR

你可以找到更多关于这个在Heroku的Postgres的文档:https://devcenter.heroku.com/articles/heroku-postgresql#create-a-new-database

+0

我按照你告诉我的方式,但我仍然得到相同的错误信息。 – d98867eb

+0

当您在命令行上运行'heroku config'时,您是否看到列出的'DATABASE_URL'环境变量? – rdegges

+0

是的,我确实看到DATABASE变量,仍然有相同的错误信息。 @rdegges – d98867eb