无法使用Ruby连接到Heroku上的PostgreSQL数据库 - 无法翻译主机名 [英] Can't connect to PostgreSQL database on Heroku using Ruby - could not translate host name
问题描述
我使用Ruby(而不是Rails)并连接PostgreSQL数据库。我一直试图在Heroku上设置,但我在启动应用程序时遇到问题。
我的本地.env如下所示:
postgres:// DATABASE_URL = localhost
并且连接到数据库的Ruby连接看起来像:
@@ db = PGconn.open(:hostaddr => ENV ['DATABASE_URL'],:dbname => '(dbname)',:password =>'(password)')
Heroku应用程序在该行崩溃,并将此错误写入日志:
无法翻译主机名postgres:// (我的heroku数据库地址)地址:名称或服务未知(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屋!