【问题标题】:Cannot connect remotely to postgresql database hosted on amazon ec2 instance无法远程连接到托管在亚马逊 ec2 实例上的 postgresql 数据库
【发布时间】:2020-10-16 07:31:30
【问题描述】:

我的 ec2 实例上托管了一个 postgresql 数据库。我正在尝试使用 Python 从本地计算机连接到它。

import psycopg2

try:
 connection = psycopg2.connect(user="postgres",
                               password="<password>",
                               host="ec2-***-***-***-***.***-***-1.compute.amazonaws.com",
                               port="5432",
                               database="<db_name>")

 cursor = connection.cursor()

 cursor.execute(f"SELECT * FROM <tablename>;")
 record = cursor.fetchall()
 print(record, "\n")

except (Exception, psycopg2.Error) as error:
 print("Error while connecting to PostgreSQL", error)
finally:
 try:
     if connection:
         cursor.close()
         connection.close()
         print("PostgreSQL connection is closed")
 except:
     print("no work")

但我明白了

Error while connecting to PostgreSQL could not connect to server: Connection timed out (0x0000274C/10060)
    Is the server running on host "ec2-***-***-***-***.***-***-1.compute.amazonaws.com" (**.**.**.***) and accepting
    TCP/IP connections on port 5432?

no work

我的 pg_hba.conf 文件看起来像这样

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
host    all             all             0.0.0.0/0               md5
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            ident
#host    replication     postgres        ::1/128                 ident

我的 postgresql.conf 文件看起来像

#------------------------------------------------------------------------------
# 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
                                        # (change requires restart)
port = 5432                             # (change requires restart)

我有一个 ec2 安全组: 我究竟做错了什么?我是一个完整的新手,任何帮助表示赞赏!

【问题讨论】:

    标签: python postgresql amazon-web-services amazon-ec2


    【解决方案1】:

    连接超时通常表示您无法访问主机或被防火墙阻止。

    首先尝试从您的计算机 ping 主机。如果 ping 正常,那么您和您的 EC2 实例之间可能存在防火墙。您的 EC2 安全屏幕截图对我来说很合适。

    您是否位于可能阻止从本地计算机到 Internet 的出站会话的防火墙后面?

    在对聊天进行一些故障排除后,我们发现允许 TCP/5432 入站的 AWS 安全组未分配给 EC2 实例。

    【讨论】:

    • 我尝试ping它ping ec2-***-***-***-***.***-***-1.compute.amazonaws.com,它说Request timed out.
    • 您是否能够从您所在的位置与该主机建立 ssh 连接?在排除与服务器的数据库连接问题之前,请确定您是否可以访问服务器。
    • 我可以使用 PuTTY 和 ec2 密钥对通过 SSH 连接到实例。
    • 好的。您是否使用相同的 DNA 名称?它是否解析为您的 PuTTY 会话使用的同一 IP 地址?如果是这样,那么您是在控制本地网络,还是在工作?
    • 我使用相同的 DNS 但没有 ec2-user@。我将如何检查IP?这是我的家庭网络。
    【解决方案2】:

    有很多可能的原因,所以我会从像 DBeaver 这样的受信任的数据库客户端开始,并尝试从您的本地计算机建立连接以排除 python 问题。

    根据您的设置,您的 ec2 实例中可能有第二个传入防火墙(iptables 等)运行,需要配置或禁用。

    登录 ec2 控制台,看看是否可以连接到服务器。在 ec2 中加载 pgsql 之类的 db 客户端,并尝试以 localhost:5432 作为目标连接到服务器。

    您可能需要更改 pg_hba.conf 以便服务器生成日志文件,但这可以告诉您是否正在访问服务器以及问题所在。

    【讨论】:

    • 我可以从 ec2 实例访问数据库,但不能远程访问。我尝试使用 pgAdmin,但它也说“超时已过期”。我将尝试使用日志文件,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2013-07-24
    • 2016-01-12
    • 2018-05-28
    • 2010-12-28
    • 2014-09-04
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多