postgresql(远程)连接被拒绝

标签 postgresql connection

我的错误:

# 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

所以我假设我采取的第一步是正确的?

我觉得我可能缺少的最后一步可能是 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 在列表中。在此之前连接是否被另一条规则拒绝? iptable 命令很奇怪。

一般信息:

两台电脑都运行 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. 在下一行中用“*”代替 localhost 并删除注释符号 (#)

    listen_addresses = '*'
    
  3. pg_hba.conf

    中添加以下行

    #TYPE 数据库用户 CIDR 地址方法

    host  all  all 0.0.0.0/0 md5
    
  4. 重启服务:
    (Ubuntu) sudo service postgresql restart

关于postgresql(远程)连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37809663/

相关文章:

java - 从 PostgreSQL 函数获取数据到 java

python - 更新现有表/模型列/字段?

php - connect.php 无法连接

java - 如何在Java中的两个混合器之间创建 "connection"?

linux - 如何检测网线/连接器的物理连接状态?

database - 杀死 autovacuum 的危险 : VACUUM queries (to prevent wraparound)

ruby-on-rails - 验证有条件时数据库中的唯一性验证

sql - 如果先前的 CTE 确实访问关系或错误字符串不包含参数,PostgreSQL 11 返回 null 而不是异常

MySQL:我可以更新当前结果集中的记录还是需要 2 个并行查询/连接

java - 什么时候应该在 GRPC 上关闭 channel ?