PostgreSQL(远程)连接拒绝



我的错误:

# psql -U postgres -h 10.230.5.51
psql: could not connect to server: Connection refused
        Is the server running on host "10.230.5.51" and accepting
        TCP/IP connections on port 5432?

在Postgresql.conf中:

listen_addresses = '*'
port = 5432                             # (change requires restart)

我在pg_hba.conf中添加了客户端服务器(用户名/数据库被X替换)

host    X          X          10.230.5.21             md5
host    X          X          10.230.5.22             md5

在我在pg_hba.conf中添加这些错误之前,它给了我这个错误:

# psql -U postgres -h 10.230.5.51
psql: FATAL:  no pg_hba.conf entry for host "10.230.5.22", user "X", database "X", SSL on
FATAL:  no pg_hba.conf entry for host "10.230.5.22", user "X", database "X", SSL off

所以我认为我采取的第一步是正确的?

我觉得I May 缺少的最后一步可能是iptables。他们看起来像这样:

-A INPUT -i lo -j ACCEPT
-A INPUT -d 127.0.0.0/8 ! -i lo -j REJECT --reject-with icmp-port-unreachable
-A INPUT -s 10.230.4.0/22 -j LOCAL
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8010 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 10443 -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -p gre -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-port-unreachable
-A LOCAL -p udp -m state --state NEW -m udp --dport 53 -j ACCEPT
-A LOCAL -p tcp -m state --state NEW -m tcp --dport 53 -j ACCEPT
-A LOCAL -p udp -m state --state NEW -m udp --dport 123 -j ACCEPT
-A LOCAL -p tcp -m state --state NEW -m tcp --dport 2181 -j ACCEPT
-A LOCAL -p tcp -m state --state NEW -m tcp --dport 2888 -j ACCEPT
-A LOCAL -p tcp -m state --state NEW -m tcp --dport 3888 -j ACCEPT
-A LOCAL -p tcp -m state --state NEW -m tcp --dport 5432 -j ACCEPT
-A LOCAL -p tcp -m state --state NEW -m tcp --dport 7000 -j ACCEPT
-A LOCAL -p tcp -m state --state NEW -m tcp --dport 7001 -j ACCEPT
-A LOCAL -p tcp -m state --state NEW -m tcp --dport 9042 -j ACCEPT
-A LOCAL -p tcp -m state --state NEW -m tcp --dport 9160 -j ACCEPT

iptables还可以吗?我可以看到端口5432在列表中。在此之前,连接是否被另一个规则拒绝?可用命令很奇怪。

一般信息

两台计算机都在运行Linux。我最初没有设置数据库。数据库运行良好,但仅在本地运行,即使使用Postgres数据库在服务器上运行命令时,这也是如此:

# psql -U postgres -h localhost
psql: could not connect to server: Connection refused
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?
could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?

我在postgresql 9.6中遇到了相同的问题,我以下面的方式解决了。

  1. sudo nano /etc/postgresql/9.6/main/postgresql.conf

  2. put'*',而不是以下行上的本地主机,并删除了评论符号(#)

    listen_addresses = '*'
    
  3. pg_hba.conf

    中添加以下行

    #type数据库用户cidr-address方法

    host  all  all 0.0.0.0/0 md5
    
  4. 重新启动服务:
    (ubuntu)sudo服务postgresql restart

做以下

update /var/lib/pgsql/<version>/data/postgresql.conf

更改#listen_addresses = 'localhost' to listen_addresses = '*'

重新启动服务

检查是否未使用用户名切换数据库。

这也发生在我身上,这修复了。正确的语法是:

host DATABASE USERNAME ADDRESS METHOD

最新更新