ruby - "PG::ConnectionBad - could not connect to server: Connection refused"Heroku 上的 Sinatra 服务器

标签 ruby postgresql heroku sinatra sinatra-activerecord

我的 Mad Libs 程序(codeHeroku appearlier version on repl.it)在我的 Ubuntu VirtualBox 中运行良好,在将代码推送到 Heroku 几分钟后,它实际上也在 Heroku 上运行。然后大概 15 到 30 分钟后,我做了一些事情,或者服务器上发生了一些事情,自从应用程序页面显示“内部服务器错误”并且服务器日志的相关部分显示为:

2018-04-14T02:03:45.717579+00:00 heroku[web.1]: Starting process with command `bundle exec ruby app.rb -p 7330`
2018-04-14T02:03:48.443468+00:00 app[web.1]: [2018-04-14 02:03:48] INFO  ruby 2.4.0 (2016-12-24) [x86_64-linux]
2018-04-14T02:03:48.443866+00:00 app[web.1]: == Sinatra (v2.0.1) has taken the stage on 7330 for production with backup from WEBrick
2018-04-14T02:03:48.443437+00:00 app[web.1]: [2018-04-14 02:03:48] INFO  WEBrick 1.3.1
2018-04-14T02:03:48.444166+00:00 app[web.1]: [2018-04-14 02:03:48] INFO  WEBrick::HTTPServer#start: pid=4 port=7330
2018-04-14T02:03:48.855874+00:00 heroku[web.1]: State changed from starting to up
2018-04-14T02:04:04.372745+00:00 heroku[router]: at=info method=GET path="/" host=youmadlibs.herokuapp.com request_id=118d681f-3402-44aa-9d04-75d930461195 fwd="74.135.49.116" dyno=web.1 connect=0ms service=20ms status=500 bytes=854 protocol=https
2018-04-14T02:04:04.368607+00:00 app[web.1]: 2018-04-14 02:04:04 - PG::ConnectionBad - could not connect to server: Connection refused
2018-04-14T02:04:04.368644+00:00 app[web.1]:    Is the server running on host "localhost" (127.0.0.1) and accepting
2018-04-14T02:04:04.368648+00:00 app[web.1]:    TCP/IP connections on port 5432?
2018-04-14T02:04:04.368651+00:00 app[web.1]: :
2018-04-14T02:04:04.368653+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.4.0/gems/pg-1.0.0/lib/pg.rb:56:in `initialize'
2018-04-14T02:04:04.368654+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.4.0/gems/pg-1.0.0/lib/pg.rb:56:in `new'
2018-04-14T02:04:04.368656+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.4.0/gems/pg-1.0.0/lib/pg.rb:56:in `connect'
2018-04-14T02:04:04.368659+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:684:in `connect'
etc.

相关行似乎是:

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?

我已经尝试了很多东西,比如重新启动服务器,将 dynos 减少到 0 然后重新增加到 1,然后运行 ​​$ heroku run psql -h HEROKU_POSTGRESQL_SILVER_URL -U deploy youmadlibs .我阅读了有关将 Ruby 连接到数据库的 Heroku 文档,对于 database.yml,它具有 host: localhost。我试过了(尽管它以前曾使用我从教程中获取的下面的设置工作过)。我还尝试了来自 this tutorial 的一组不同的 database.yml 设置.没有骰子!我在这里和其他地方阅读了很多答案,但无法弄清楚。如何重新建立数据库与服务器的连接?

数据库.yml:

development:
  adapter: sqlite3
  database: db/madlibs.sqlite
  host: localhost
  pool: 5
  timeout: 5000

production:
  url: <%= ENV['DATABASE_URL'] %>
  adapter: postgresql
  database: mydb
  host: <%= ENV['DATABASE_HOST'] %>
  database: <%= ENV['DATABASE_NAME'] %>
  username: <%= ENV['DATABASE_USER'] %>
  password: <%= ENV['DATABASE_PASSWORD'] %>
  pool: 5
  timeout: 5000

.环境:

DATABASE_URL=<...my database name...>

配置/环境.rb:

configure :development do
  set :database, {adapter: "sqlite3", database: "db/madlibs.sqlite3"}
  set :show_exceptions, true
end

configure :production do
  db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/mydb')

  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

gem 文件:

source 'https://rubygems.org'
ruby '2.4.0'

gem 'sinatra', '2.0.1'
gem 'pg', '1.0.0'
gem 'activerecord', '5.2.0'
gem 'sinatra-activerecord', '2.0.13'
gem 'sinatra-flash', '0.3.0'                # Probably not nec.
gem 'sinatra-redirect-with-flash', '0.2.1'  # Probably not nec.
gem 'rake', '10.4.2'
gem 'rack', '2.0.3'
gem 'sass', '3.4.24'
gem 'json', '2.1.0'

group :development do
  gem 'sqlite3', '1.3.13'
  gem 'tux', '0.3.0'                        # Maybe unnec.
end

group :production do
end

app.rb 的顶部:

require 'sinatra'
require 'sinatra/activerecord'
require './config/environment'
enable :sessions

class Madlib < ActiveRecord::Base
end

最佳答案

如果你有 db url,你不需要其他行,它可以是 1 行,并且让 postgres 超时它是默认的,不需要设置它。您可能希望产品中的池大小更大。转到您应该在 Heroku 中添加到您的应用程序的 postgres 数据库。单击查看凭据按钮。将数据从那里复制到您的本地 .env 文件,并将 .env 添加到您的 .gitignore 文件,因为您不想将您的私有(private)凭证提交到您的公共(public)存储库。

然后在heroku上

database.yml:

development:
  adapter: sqlite3
  database: db/madlibs.sqlite
  host: localhost
  pool: 5
  timeout: 5000

production: 
  adapter: postgresql
  encoding: unicode
  host: <%= ENV['DB_HOST'] %>
  username: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  port: <%= ENV['DB_PORT'] %>
  database: <%= ENV['DB_NAME'] %>
  pool: 25

或者,您可以只使用 1 个环境变量,它将所有这些连接成一个 URI,看起来像这样

#postgresql://username:password@db-shared-us-east-1-or-something.blah:1234/db
#If you use this format you can use just a single environment variable somthinglike

production: 
  adapter: postgresql
  encoding: unicode
  url: <%= ENV['DATABASE_URI'] %>
  pool: 25

在任何一种情况下,您都需要设置您将在 Heroku 仪表板中设置的 database.yml 文件中使用的任何环境变量:https://dashboard.heroku.com/apps/yourappname/settings并单击显示配置变量。这是您设置所需变量的地方,因为这是您的应用程序将从中加载它们的地方。在我的第一个示例中设置 vars,或者在我的第二个示例中设置一个。无论哪种方式都应该工作。

确保在 Gemfile 的生产组中有 pg gem。并确保你已经在你的 heroku 服务器上设置了你的环境变量。

关于ruby - "PG::ConnectionBad - could not connect to server: Connection refused"Heroku 上的 Sinatra 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49827381/

相关文章:

git - Heroku 推送被拒绝,子模块安装失败

ruby-on-rails - 别名方法链未定义的方法

postgresql - postgres 10 对 varchars 使用什么哈希算法?

ruby-on-rails - Rails 4 - best_in_place gem 和多对多关联

database - 在数据库中创建百万表?

sql - 从 CSV 文件将数据导入 SQL 表并考虑外键

javascript - 使用 python Flask 应用程序在 Heroku 上未显示 highcharts

ruby-on-rails - 设置 SSL 时出现问题,不断收到 "no valid, non-passphrase-protected keys given"错误

ruby-on-rails - 为 Postgres Ruby-on-Rails 应用程序设置本地开发

ruby - '& :' usage with ' is_a(整数)'