【问题标题】:How can I connect my ruby app to Heroku postgres database?如何将我的 ruby​​ 应用程序连接到 Heroku postgres 数据库?
【发布时间】:2018-07-05 07:12:54
【问题描述】:

我有一个在 heroku 上运行的 ruby​​ 应用程序,我正在尝试将它连接到 Heroku postgres 数据库。我已经安装了附加组件并捆绑了“pg”gem。我试着跑了

PG.connect(dbname:d6td9jdn7irk0u)

只会导致:

PG::ConnectionBad: could not connect to server: No such file or directory. Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

我的问题是:我怎样才能正确建立连接?我应该向 connect() 函数提供用户/密码吗?

我在网上搜索时能找到的所有内容都是关于使用 ruby​​ on rails 遇到类似问题的人

【问题讨论】:

标签: ruby postgresql heroku pg heroku-postgres


【解决方案1】:

Heroku 在 DATABASE_URL 环境变量中公开他们的 postgres 连接字符串。所以你只需PG.connect(ENV['DATABASE_URL'])

详情请见https://devcenter.heroku.com/articles/connecting-to-heroku-postgres-databases-from-outside-of-heroku#credentials

【讨论】:

    【解决方案2】:

    解决了,有点。 PG.connect() 采用哈希值,其中可以包含主机/端口/用户/密码信息。所有这些信息都可以从 heroku cli 中检索到:

    heroku pg:credentials:url database
    

    不幸的是,数据库凭据不是永久性的。因此,每次部署时,我都必须确保应用已成功部署。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-18
      • 2013-01-10
      • 2016-01-27
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 2012-06-08
      相关资源
      最近更新 更多