如何将我的ruby应用程序连接到Heroku postgres数据库?

时间:2018-01-25 22:35:19

标签: ruby postgresql heroku pg heroku-postgres

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

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()函数提供用户/密码吗?

我在网上搜索时能找到的一切都是关于在轨道上使用红宝石遇到类似问题的人

2 个答案:

答案 0 :(得分:2)

Heroku在set.seed(1L) df1 <- data.frame(a1 = sample(c(TRUE, FALSE), 5, TRUE), a2 = sample(c(TRUE, FALSE), 5, TRUE), a3 = sample(c(TRUE, FALSE), 5, TRUE), a4 = sample(c(TRUE, FALSE), 5, TRUE), a5 = sample(c(TRUE, FALSE), 5, TRUE)) colSums(df1) # a1 a2 a3 a4 a5 # 3 1 3 2 3 sum(df1[, 5]) # [1] 3 环境变量中公开了他们的postgres连接字符串。所以你只是DATABASE_URL

有关详细信息,请参阅https://devcenter.heroku.com/articles/connecting-to-heroku-postgres-databases-from-outside-of-heroku#credentials

答案 1 :(得分:1)

解决了它,有点儿。 PG.connect()接受一个哈希,它可以包含主机/端口/用户/密码信息。所有这些信息都可以使用以下方法从heroku cli中检索:

heroku pg:credentials:url database

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

相关问题