首页 > 解决方案 > 尝试连接到数据库 postgres

问题描述

这是我得到它拒绝连接到我的数据库的错误

Traceback (most recent call last):
  File "db.py", line 2, in <module>
    conn = psycopg2.connect(host="176.58.104.XXX",database="XXXX", user="XXXXX", password="XXXXX")
  File "c://addr", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused (0x0000274D/10061)
        Is the server running on host "176.58.104.XXX" and accepting
        TCP/IP connections on port 5432?

我已经尽可能多地诊断出原因。我的 postgresql.conf 文件在这里,所以我更新了它们,除了示例

locate postgresql.conf
/etc/postgresql/9.5/main/postgresql.conf
/usr/lib/tmpfiles.d/postgresql.conf
/usr/share/postgresql/9.5/postgresql.conf.sample

#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -

#listen_addresses = '*'         # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all

然后更新了 pg_hba.conf

host    all             all              0.0.0.0/0                       md5
host    all             all              ::/0                            md5

并重新启动 sudo systemctl restart postgresql

我按照这些说明https://zaiste.net/postgresql_allow_remote_connections/

但我仍然收到错误

这是我的 netstat -nlt,我仍然可以看到我的连接与 127.0.0.1 绑定

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN

我错过了一些步骤吗?

标签: python-3.xpostgresql

解决方案


默认情况下,# 在 #listen_addresses = '*' 前面。

没看到。


推荐阅读