需要SSL时将Python /熊猫连接到Redshift

mea*_*ngs 5 python ssl psycopg2 pandas amazon-redshift

我的公司最近更改了我们的Redshift集群,现在它们需要SSL连接。过去,我通过此处详细介绍的方法将Python / pandas连接到Redshift:http : //measureallthethin.gs/blog/connect-python-and-pandas-to-redshift/

从SQLAlchemy文档看来,我需要做的就是添加connect_args={'sslmode':'require'}create_engine()调用中,正如该线程指出的那样:如何使用来自SqlAchemy + pg8000的SSL连接到Postgresql?

但是,我现在收到此错误:

OperationalError:(psycopg2.OperationalError)sslmode值“ require”在未编译SSL支持时无效

我将Anaconda发行版用于许多软件包,并且发现我需要按照以下说明更新psycopg2软件包:https : //groups.google.com/a/continuum.io/d/msg/conda/Fqv93VKQXAc/mHqfNK8xZWsJ

但是,即使在更新了psycopg2之后,我仍然遇到相同的错误,并且在如何进行进一步调试方面不知所措。我想弄清楚这一点,以便可以将Redshift数据直接输入到熊猫中。

Gon*_*ica 1

AWS 开发了一个适用于 Python 的 Amazon Redshift 连接器(这里是 GitHub 存储库)),可以帮助完成此过程。

\n

为了安装它可以从源安装

\n
git clone https://github.com/aws/amazon-redshift-python-driver.git\ncd redshift_connector\npip install .\n
Run Code Online (Sandbox Code Playgroud)\n

或者使用PyPi从二进制文件中

\n
pip install redshift_connector\n
Run Code Online (Sandbox Code Playgroud)\n

或者康达

\n
conda install -c conda-forge redshift_connector\n
Run Code Online (Sandbox Code Playgroud)\n

这是一个例子

\n
import redshift_connector\n\n# Connects to Redshift cluster using AWS credentials\nconn = redshift_connector.connect(\n    host=\'examplecluster.abc123xyz789.us-west-1.redshift.amazonaws.com\',\n    database=\'dev\',\n    user=\'awsuser\',\n    password=\'my_password\'\n )\n\ncursor: redshift_connector.Cursor = conn.cursor()\ncursor.execute("create Temp table book(bookname varchar,author varchar)")\ncursor.executemany("insert into book (bookname, author) values (%s, %s)",\n                    [\n                        (\'One Hundred Years of Solitude\', \'Gabriel Garc\xc3\xada M\xc3\xa1rquez\'),\n                        (\'A Brief History of Time\', \'Stephen Hawking\')\n                    ]\n                  )\ncursor.execute("select * from book")\n\nresult: tuple = cursor.fetchall()\nprint(result)\n>> ([\'One Hundred Years of Solitude\', \'Gabriel Garc\xc3\xada M\xc3\xa1rquez\'], [\'A Brief History of Time\', \'Stephen Hawking\'])\n
Run Code Online (Sandbox Code Playgroud)\n

请注意,可以传递的连接参数之一是 SSL(如果启用了 SSL)。默认值为TRUE

\n