首页 > 解决方案 > psycopg2.OperationalError:无法连接到服务器:连接超时

问题描述

我有一个脚本:

import psycopg2
from sshtunnel import SSHTunnelForwarder

with SSHTunnelForwarder(
      ('3.120.198.220', 22),
      ssh_password='MYPASSWORD',
      ssh_username='MYUSER',
      ssh_pkey='MYPATH',
      remote_bind_address=('192.168.122.61', 5432)) as server:

     connection = psycopg2.connect(database='MYDB',user='MYUSER',password='MYPASS',host='192.168.122.61')
     cursor = connection.cursor()

我有那个输出:

psycopg2.OperationalError: could not connect to server: Connection timed out (0x0000274C/10060)
        Is the server running on host "192.168.122.61" and accepting
        TCP/IP connections on port 5432?

我检查了我的 postgresql.conf,所以它听所有的:

听地址 = '*'

还有我的 pg_hba.conf:

    # IPv4 local connections:
host    all             all             127.0.0.1/32            scram-sha-256
host    all             all             127.0.0.1/32            scram-sha-256
host    all             all             192.168.122.61/32       scram-sha-256
# IPv6 local connections:
host    all             all             ::1/128                 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     scram-sha-256
host    replication     all             127.0.0.1/32            scram-sha-256
host    replication     all             ::1/128                 scram-sha-256

有任何想法吗?

标签: pythonpostgresqlsshpsycopg2ssh-tunnel

解决方案


推荐阅读