首页 > 解决方案 > 使用 Python 访问 AWS Redshift

问题描述

我正在尝试运行 aws redshift 代码以便使用 python 连接数据库。我正在尝试运行 aws redshift 文档中给出的给定示例代码。在运行代码时我仍然遇到一些问题。

代码 :

import numpy 
import redshift_connector
import socket
socket.getaddrinfo('localhost', 8080)

conn = redshift_connector.connect(
     host='examplecluster.abc123xyz789.us-west-1.redshift.amazonaws.com',
     database='dev',
     user='awsuser',
     password='my_password'
  )

cursor: redshift_connector.Cursor = conn.cursor()
cursor.execute("create Temp table book(bookname varchar,author‎ varchar)")
cursor.executemany("insert into book (bookname, author‎) values (%s, %s)",
                    [
                        ('One Hundred Years of Solitude', 'Gabriel García Márquez'),
                        ('A Brief History of Time', 'Stephen Hawking')
                    ]
                  )
cursor.execute("select * from book")

result: numpy.ndarray = cursor.fetch_numpy_array()
print(result)
cursor.execute("drop table book")
conn.commit()
conn.close()

错误 :

在此处输入图像描述

标签: pythonamazon-web-servicesamazon-redshift

解决方案


推荐阅读