我试图连接到本地IP上的Postgres实例,我可以连接到使用psql -h localhost命令没有问题。 然而,试图通过rails运行,我得到这个:
$ rails c production /home/avishai/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `initialize': could not connect to server: Connection refused (PG::Error) Is the server running on host "10.61.99.194" and accepting TCP/IP connections on port 5432?
这是我的/etc/postgresql/9.1/main/pg_hba.conf :
# DO NOT DISABLE! # If you change this first entry you will need to make sure that the # database superuser can access the database using some other method. # Noninteractive access to all databases is required during automatic # maintenance (custom daily cronjobs, replication, and similar tasks). # # Database administrative login by Unix domain socket local all postgres peer # TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all peer # IPv4 local connections: host all all 127.0.0.1/32 trust host all all 10.80.85.130/32 trust host all all 10.80.85.130/32 md5 # IPv6 local connections: host all all ::1/128 md5
我如何确保Postgres可以接受来自本地主机的传入连接以及选定的内部IP?
请尝试检查这两点:
1)在文件/etc/postgresql/9.1/main/postgresql.conf你必须有这一行:
listen_addresses = '*'
代替
listen_addresses = '127.0.0.1'
你可以通过命令netstat -tpln | grep 5432来检查它 netstat -tpln | grep 5432 。 你会在输出中看到像这样的东西:
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 1150/postgres
如果第一个IP地址是0.0.0.0 ,postgres正在监听所有的接口,这就是你想要的。 如果您将在第一个IP地址字段中仅看到127.0.0.1 ,则postgresql仅在localhost接口上进行侦听。
2)尝试通过iptables -L -n来检查你的防火墙规则。 在INPUT链中,必须有规则接受来自服务器的IP地址的端口5432的连接,运行轨是哪里。 您可以通过以下命令添加此规则: iptables -I INPUT -p tcp -s 10.80.85.130 --dport 5432 -j ACCEPT
但我认为,这个问题将在1)点。
我有同样的麻烦。
在我的情况下,出于某种原因使用了IPV6 Localhost。
在你的pg_hba.conf中试试这个:
host all all ::1/128 md5