[英]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.