我将示例 Rails 应用程序部署到远程服务器,并在同一台服务器上托管了 postgres。 database.yml 文件是这样的。
production:
adapter: postgresql
encoding: unicode
database: remotepg_production
pool: 5
host: localhost
username: mrmann
password: secret
一切正常。然后我进入 database.yml 文件并将 host: localhost
替换为另一台服务器上的 postgres 数据库的 ip 地址 host: 178.XXX.XXX
像这样
production:
adapter: postgresql
encoding: unicode
database: remotepg_production
pool: 5
host: 178.XXX.XXX.XXX #ip address of server with other postgres database
username: mrmann
password: secret
当我使用 Rails 应用程序在服务器上重新启动 postgres 时,示例应用程序现在为我提供了 Rails 的“出错了”页面。
两个数据库上的用户名和用户名密码相同。你能建议问题可能是什么吗?谢谢
更新
这些是我要连接的数据库所在服务器上的 pg_hba.conf 中的设置。
local all postgres peer
local all all peer
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
假冒 Rails 应用 IP:192.241.XXX.X
假数据库IP:192.34.XX.XXX
最佳答案
您可以访问数据库日志吗?如果您的连接达到了尝试连接到数据库的程度,那么那里应该有一些消息。我的 WAG 是您需要将该 IP 添加到服务器的 pg_hba.conf 文件并重新加载 Postgres。
更清楚地说,添加到服务器 pg_hba.conf 的 IP 是您的应用程序连接的 IP,您还应该检查数据库的 listen_addresses
设置的值。如果后者未设置为“*”或应用程序的 IP,则需要在 postgresql.conf 中更改它并重新启动数据库集群。
[编辑,提供的新信息]
您的 pg_hba.conf 中的行应该是
host all all 192.241.XXX.X md5
重新加载(而不是重新启动)postgres(例如 pg_ctl reload
),然后再次尝试连接。如果失败,请找到数据库日志并查看它显示的内容。如果没有消息,则可能是防火墙问题。如果怀疑是后者,请在 iptables -nvL
关于ruby-on-rails - 在不同的服务器上设置数据库的IP地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17863445/