【发布时间】:2014-04-24 07:11:25
【问题描述】:
我正在尝试从 ruby 脚本访问数据库。目前我有这个代码:
config = YAML.load(ERB.new(File.read(File.join("config","database.yml"))).result)
environment = ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
return PGconn.connect( :hostaddr=>config[environment]["host"], :port=>config[environment]["port"], :dbname=>config[environment]["database"], :user=>config[environment]["username"], :password=>config[environment]["password"]);
我的 database.yml 如下:
发展: 适配器:postgresql 主机:本地主机 用户名:用户名 密码:密码 数据库:数据库名 端口:5432
然后我的脚本抱怨说:
/app/scripts/crawler.rb:54:in `initialize': 无法翻译主机 将“localhost”命名为地址:名称或 服务未知
我将“localhost”更改为 127.0.0.1,它起作用了。
当我上传到 Heroku 时,我得到以下信息:
/app/scripts/crawler.rb:54:in `initialize': 无法翻译主机 将“ec2-184-72-231-67.compute-1.amazonaws.com”命名为地址:姓名或 服务未知
我是否需要导入一些 gem 才能将主机名转换为 IP?我已经搜索过但没有太多信息,我唯一发现的是这个相似但不一样的问题:Occasional Postgres error on Heroku: could not translate host name "<pg URL>" to address: Name or service not known (PG::Error),但我不知道答案是否相同,因为我有同样的问题在本地主机上,因为他的问题是偶然的,而我的不是。
谢谢
【问题讨论】:
-
您好,您能帮我连接 heroku postres 和我的本地机器 postgres 吗?