简体   繁体   English

Ruby Sinatra/Postgres - 无法使用 PG.connect 连接到 heroku 数据库?

[英]Ruby Sinatra/Postgres - can't connect to heroku database with PG.connect?

I am trying to get a site set up on Heroku using Sinatra and PostgreSQL.我正在尝试使用 Sinatra 和 PostgreSQL 在 Heroku 上建立一个站点。 It worked locally (connecting to local database), but after pushing it to Heroku and changing my PG.connect to reflect that, I get an Internal Server Error the moment a page tries to access the database.它在本地工作(连接到本地数据库),但是在将它推送到 Heroku 并更改我的PG.connect以反映这一点后,当页面尝试访问数据库时,我收到内部服务器错误

require 'uri'
require 'pg'

uri = URI.parse(ENV['DATABASE_URL'])

def db(uri)
  begin
    connection = PG.connect(uri.hostname, uri.port, nil, nil, uri.path[1..-1], uri.user, uri.password)
    yield(connection)
  ensure
    connection.close
  end
end

I am pretty sure these are parsing correctly, because ENV['DATABASE_URL'] displays the full postgres://user:password@host:port/database information that I'm expecting, and if I do the same in IRB uri.hostname, ui.port, etc all return what's expected .我很确定这些解析正确,因为ENV['DATABASE_URL']显示了我期望的完整postgres://user:password@host:port/database信息,如果我在 IRB uri.hostname 中做同样的事情, ui.port 等都返回预期的内容。

This is my first time trying to get a site working on Heroku, so I am not even sure how to troubleshoot this.这是我第一次尝试让网站在 Heroku 上运行,所以我什至不知道如何解决这个问题。 (And I googled for about all of yesterday.) (我昨天在谷歌上搜索了大约所有内容。)

Results for heroku pg : heroku pg结果:

=== DATABASE_URL
Plan:        Hobby-dev
Status:      Available
Connections: 0/20
PG Version:  9.4.2
Created:     2015-05-30 19:24 UTC
Data Size:   17.7 MB
Tables:      5
Rows:        9320/10000 (In compliance, close to row limit)
Fork/Follow: Unsupported
Rollback:    Unsupported

And all the tables show up when when I do heroku pg:psql <database> from the cli.当我从 cli 执行heroku pg:psql <database>时,所有表都会显示出来。

Some answers I've seen said to add database.yml to my root app directory, so:我看到的一些答案说要将database.yml添加到我的根应用程序目录中,因此:

production:
  adapter: 'postgresql'
  database: '<database>'
  host: ENV['DATABASE_URL']
  username: '<username>'

There's probably something simple I'm missing, but I haven't seen a complete guide for Sinatra/PSQL on Heroku - nothing that goes specifically into setting up and connecting to your database.我可能遗漏了一些简单的东西,但我还没有在 Heroku 上看到 Sinatra/PSQL 的完整指南——没有专门涉及设置和连接到数据库的内容。 (Everything seems Rails-related.) (一切似乎都与 Rails 相关。)

In your database.yml file you need to specify the correct host for the host entry.在您的database.yml文件中,您需要为host条目指定正确的host You are passing what is stored in DATABASE_URL (something like postgres://user:password@host:port/database ) but it should just be the host.您正在传递DATABASE_URL存储的内容(类似于postgres://user:password@host:port/database ),但它应该只是主机。

You will also need to specify a port if it isn't the default for PostgreSQL.如果它不是 PostgreSQL 的默认端口,您还需要指定一个端口。

Edit: should also point out if you plan to store the host (or anything else - you definitely should for username and password) in an environment variable you'll need to wrap it, eg <%= ENV['HOST'] %> , not just ENV['HOST'] (ie how you have in the database.yml excerpt above)编辑:还应该指出您是否打算将主机(或其他任何东西 - 您肯定应该存储用户名和密码)在您需要包装它的环境变量中,例如<%= ENV['HOST'] %> ,而不仅仅是ENV['HOST'] (即你在上面的 database.yml 摘录中的内容)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用“ pg” gem连接到postgres? - Can't connect to postgres with 'pg' gem? ruby on rails无法连接到postgres得到PG :: Error:fe_sendauth:没有密码 - ruby on rails can't connect to postgres got PG::Error: fe_sendauth: no password supplie Ruby / Sinatra / Postgres 应用程序“PG::ConnectionBad”的 Heroku 部署 - Heroku deployment for Ruby / Sinatra / Postgres app "PG::ConnectionBad" 无法使用宝石“ pg”从我的Ruby Script连接到Postgres(pg)数据库(这不是rails,仅仅是纯红宝石) - cannot connect to Postgres (pg) database from my Ruby Script using gem “pg”(This is not rails, just pure ruby) Heroku上的“ PG :: ConnectionBad-无法连接到服务器:连接被拒绝” - “PG::ConnectionBad - could not connect to server: Connection refused” Sinatra server on Heroku 如何通过Ruby的pg gem连接到Postgres数据库(Rails外部) - How to connect to Postgres database through Ruby's pg gem (outside of Rails) 无法使用Ruby连接到Heroku上的PostgreSQL数据库 - 无法翻译主机名 - Can't connect to PostgreSQL database on Heroku using Ruby - could not translate host name Docker:Docker-compose ruby​​ + postgres - PG::ConnectionBadcould 无法连接到服务器 - Docker: Docker-compose ruby + postgres - PG::ConnectionBadcould not connect to server 无法从Sinatra连接到Redis - Can't connect to Redis from Sinatra 无法连接到数据库 - 无法将PostgreSQL数据库推送到heroku - Failed to connect to database - can't push PostgreSQL database to heroku
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM