【问题标题】:Unable to remotely connect to port 5432 on centOS 7centOS 7无法远程连接5432端口
【发布时间】:2021-11-20 07:26:06
【问题描述】:

我正在尝试远程连接到 Postgresql (v9.6),但似乎无法连接。我的 Postgres 驻留在 CentOS 7 服务器上。从该服务器上,我为各种网站提供服务(在端口 80xx 上),并且我还在成功地使用端口 5050 上的服务。似乎只有端口 5432 有问题。例如:

$ lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 7.4.1708 (Core)
Release:        7.4.1708
Codename:       Core

$ sudo firewall-cmd --zone=public --add-port=5432/tcp --permanent
Warning: ALREADY_ENABLED: 5432:tcp
success

$ sudo firewall-cmd --reload
success

$ sudo iptables -S | grep "5432"
-A IN_public_allow -p tcp -m tcp --dport 5432 -m conntrack --ctstate NEW -j ACCEPT

$ netstat -nlp | grep 5432
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      -
tcp6       0      0 ::1:5432                :::*                    LISTEN      -
unix  2      [ ACC ]     STREAM     LISTENING     516044892 -                    /var/run/postgresql/.s.PGSQL.5432
unix  2      [ ACC ]     STREAM     LISTENING     516044894 -                    /tmp/.s.PGSQL.5432

这就是我的工作:

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

这是我的 pg_hba.conf

host    all             all             127.0.0.1/32            ident
host    all             all             ::1/128                 ident
host    all             all             172.17.0.0/24           trust
host    all             all             0.0.0.0/0               md5

我还修改了postgresql.conf,在5432端口监听'*'并重启了服务器。

我错过了什么?

PS:我可以在本地连接,因为我的一些 Web 应用程序正在使用此服务器来存储数据。

编辑:这样可以吗?

$ netstat -tulpn | awk 'NR==2 || /:5432/'
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      -
tcp6       0      0 ::1:5432                :::*                    LISTEN      -

【问题讨论】:

    标签: postgresql centos


    【解决方案1】:

    看来我终于找到了解决办法。

    在 CentOS 7 中,两个 postgresql 配置文件可以在两个地方找到。

    我用的那些是错误的:

    vim /var/lib/pgsql/data/pg_hba.conf
    vim /var/lib/pgsql/data/postgresql.conf
    

    正确的可以在这个目录中找到

    vim /var/lib/pgsql/9.6/data/postgresql.conf
    vim /var/lib/pgsql/9.6/data/pg_hba.conf
    

    我不知道为什么会发生这种情况,但现在它正在工作。我正在写答案以防有人需要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-27
      • 2016-11-22
      • 1970-01-01
      • 2019-08-15
      • 2020-09-15
      • 2018-01-31
      • 2023-04-02
      • 2013-11-23
      相关资源
      最近更新 更多