无法使用 Ruby 连接到 Heroku 上的 PostgreSQL 数据库 - 无法翻译主机名 [英] Can't connect to PostgreSQL database on Heroku using Ruby - could not translate host name

查看:73
本文介绍了无法使用 Ruby 连接到 Heroku 上的 PostgreSQL 数据库 - 无法翻译主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Ruby(不是 Rails)并连接 PostgreSQL 数据库.我一直在尝试在 Heroku 上进行设置,但是在启动应用程序时遇到了问题.在本地运行应用程序可以正常工作.

我的本​​地 .env 看起来像:

postgres://DATABASE_URL=localhost

和 Ruby connect 连接到数据库看起来像:

@@db = PGconn.open(:hostaddr => ENV['DATABASE_URL'], :dbname => '(dbname)', :password => '(password)')

当我推送到 Heroku 时,应用程序在该行崩溃,并将此错误写入日志:

 无法将主机名postgres://(my heroku db address)"转换为地址:名称或服务未知(PG::Error)

那里的数据库地址与我的 heroku:config 中的 DATABASE_URL 匹配.我使用的是共享数据库.

我尝试使用 :host =>ENV['DATABASE_URL'](与 :hostaddr 相反)但结果相同.我猜我遗漏了一些简单的东西,但我没有任何好的想法.

解决方案

您需要解析出DATABASE_URL 的特定部分.请参阅 https://devcenter.heroku.com/articles/rack#database_access>

I'm using Ruby (not Rails) and connecting a PostgreSQL database. I've been trying to get set up on Heroku, but I'm having problems starting the application. Running the application locally works fine.

My local .env looks like:

postgres://DATABASE_URL=localhost

And the Ruby connect to connect to the database looks like:

@@db = PGconn.open(:hostaddr => ENV['DATABASE_URL'], :dbname => '(dbname)', :password => '(password)')

When I push to Heroku the app crashes on that line, and writes this error to the logs:

could not translate host name "postgres://(my heroku db address)" to address: Name or service not known (PG::Error)

The database address there matches the DATABASE_URL in my heroku:config. I'm using a shared database.

I tried using :host => ENV['DATABASE_URL'] (as opposed to :hostaddr) but had the same result. I'm guessing there's something simple I'm missing but I haven't had any good ideas.

解决方案

You need to parse out the specific parts of the DATABASE_URL. Please see https://devcenter.heroku.com/articles/rack#database_access

这篇关于无法使用 Ruby 连接到 Heroku 上的 PostgreSQL 数据库 - 无法翻译主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆