【问题标题】:Attempting to establish a connection to Amazon Redshift from Python Script尝试从 Python 脚本建立与 Amazon Redshift 的连接
【发布时间】:2021-06-24 01:44:03
【问题描述】:

我正在尝试连接到 Amazon redshift 表。我使用 SQL 创建了表,现在我正在编写一个 Python 脚本来将数据框附加到数据库中。我无法连接到数据库,并且觉得我的语法或其他问题有问题。我的代码如下。

from sqlalchemy import create_engine
conn = create_engine('jdbc:redshift://username:password@localhost:port/db_name')

这是我遇到的错误。

sqlalchemy.exc.ArgumentError: Could not parse rfc1738 URL from string 

谢谢!

【问题讨论】:

标签: python database amazon-web-services amazon-redshift


【解决方案1】:

使用 Python 连接到 Amazon Redshift 基本上有两种选择。

选项 1:JDBC 连接

这是与数据库的传统连接。流行的选择往往是使用psycopg2 来建立连接,因为 Amazon Redshift 类似于 PostgreSQL 数据库。你可以download specific JDBC drivers for Redshift

此连接要求进行查询的计算机可以访问 Redshift 数据库,并且安全组需要允许端口 5439 上的访问。如果您尝试从 Internet 上的计算机进行连接,则数据库需要位于公共子网中并设置为Publicly Accessible = Yes

见:Establish a Python Redshift Connection: A Comprehensive Guide - Learn | Hevo

选项 2:Redshift 数据 API

您可以使用Boto3 library for Python 直接查询Amazon Redshift 数据库,包括调用execute_statement() 来查询数据和调用get_statement_result() 来检索结果。这也适用于 IAM 身份验证,而不必创建额外的“数据库用户”。

无需为此方法配置安全组,因为请求是向 AWS(在 Internet 上)发出的。它还适用于私有子网中的 Redshift 数据库。

【讨论】:

    猜你喜欢
    • 2011-03-04
    • 1970-01-01
    • 2017-01-26
    • 2017-06-24
    • 2018-06-24
    • 2018-12-22
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多