Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby &引用;PG::ConnectionBad-无法连接到服务器:连接被拒绝;Heroku上的Sinatra服务器_Ruby_Postgresql_Heroku_Sinatra_Sinatra Activerecord - Fatal编程技术网

Ruby &引用;PG::ConnectionBad-无法连接到服务器:连接被拒绝;Heroku上的Sinatra服务器

Ruby &引用;PG::ConnectionBad-无法连接到服务器:连接被拒绝;Heroku上的Sinatra服务器,ruby,postgresql,heroku,sinatra,sinatra-activerecord,Ruby,Postgresql,Heroku,Sinatra,Sinatra Activerecord,我的Mad Libs程序(,;)在我的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 73

我的Mad Libs程序(,;)在我的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\U POSTGRESQL\U SILVER\U URL-U deploy youmadlibs
。我阅读了有关将Ruby连接到数据库的Heroku文档,对于database.yml,它有
host:localhost
。我试过了(尽管事实上它以前在我下面的设置中是有效的,这是我从一个教程中获得的)。我还尝试了来自的一组不同的database.yml设置。不要掷骰子!我在这里和其他地方读到了很多答案,但都弄不明白。如何重新建立数据库服务器连接

database.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
档案:

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
需要“sinatra”
需要“sinatra/activerecord”
需要“./config/environment”
启用:会话
类Madlib
如果有db url,则不需要其他行,它可以是1行,也可以让postgres超时。这是默认设置,无需设置。你可能想在prod中使用更大的池大小。转到你应该在Heroku中添加到应用程序中的postgres数据库。单击查看凭据按钮。将数据从那里复制到本地.env文件,并将.env添加到.gitignore文件,因为您不想将私人凭据提交到公共repo

然后是赫罗库

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
database.yml:
发展:
适配器:sqlite3
数据库:db/madlibs.sqlite
主机:本地主机
游泳池:5
超时:5000
制作:
适配器:postgresql
编码:unicode
主持人:
用户名:
密码:
端口:
数据库:
游泳池:25
或者,您可以只使用1个环境变量,将所有这些连接到一个URI中,该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
#postgresql://username:password@db-shared-us-east-1-or-something.废话:1234/db
#如果你使用这种格式,你可以只使用一个环境变量
制作:
适配器:postgresql
编码:unicode
网址:
游泳池:25
在这两种情况下,您都需要在Heroku dashboard的database.yml文件中设置要使用的环境变量,并单击Reveal Config Vars。这是您设置所需变量的地方,因为这是您的应用程序加载它们的地方。在第一个示例中设置变量,或者在第二个示例中设置单个变量。任何一种方法都应该有效


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

如果有db url,则不需要其他行,它可以是1行,也可以让postgres超时。这是默认设置,无需设置。你可能想在prod中使用更大的池大小。转到你应该在Heroku中添加到应用程序中的postgres数据库。单击查看凭据按钮。将数据从那里复制到本地.env文件,并将.env添加到.gitignore文件,因为您不想将私人凭据提交到公共repo

然后是赫罗库

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
database.yml:
发展:
适配器:sqlite3
数据库:db/madlibs.sqlite
主机:本地主机
游泳池:5
超时:5000
制作:
适配器:postgresql
编码:unicode
主持人:
用户名:
密码:
端口:
数据库:
游泳池:25
或者,您可以只使用1个环境变量,将所有这些连接到一个URI中,该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
#postgresql://username:password@db-shared-us-east-1-or-something.废话:1234/db
#如果你使用这种格式,你可以只使用一个环境变量
制作:
适配器:postgresql
编码:unicode
网址:
游泳池:25
在这两种情况下,您都需要在Heroku dashboard的database.yml文件中设置要使用的环境变量,并单击Reveal Config Vars。这是您设置所需变量的地方,因为这是您的应用程序加载它们的地方。在第一个示例中设置变量,或者在第二个示例中设置单个变量。任何一种方法都应该有效


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

嗯,我试过使用你推荐的database.yml设置…没有骰子。我还尝试将
.env
(在顶级目录中)从
数据库\u URL=postgresql-curly-35865
更改为
数据库\u URL=HEROKU\postgresql\u SILVER\u URL
。我还想也许能解释怎么做,所以我尝试了
heroku config:set
。。。使用上面的两个数据库URL。还没有运气。还有其他想法吗?不要在生产中使用.env
postgresql-curly-35865
不是URL,只是Heroku分配给您的postgres db实例的随机容器名。在幕后,它只是一个docker容器。看我更新的答案。好的,就是这样!我使用了
heroku-config:set-DATABASE\u-URL=
(不是URI),后跟我在heroku-app-settings页面上找到的postgresql://URL。我还使用了更简单的production
database.yml
设置。我没有其他可用的信息。多亏了你的帮助,你现在可以把它保存到我的Postgres数据库了!这是我第一次自己建立自己的活动记录系统和Postgres数据库。所以,谢谢!现在,对于用户身份验证和其他功能…@globewalldesk很高兴这有帮助。我想说你可以使用heroko cli设置环境变量,但看起来你自己解决了。我试过了