Amazon Redshift Python Connector
redshift-connector is the official Amazon Redshift connector for Python, implementing the Python Database API Specification 2.0. It provides easy integration with data science libraries like pandas and numpy, and supports Redshift-specific features such as IAM and Identity Provider (IdP) authentication. The library is actively maintained with frequent minor releases, currently at version 2.1.12.
Warnings
- breaking A security vulnerability (CVE-2025-5279) existed in versions 2.0.872 through 2.1.6, where the `BrowserAzureOAuth2CredentialsProvider` plugin skipped SSL certificate validation for the Identity Provider (IdP). This could allow an actor to intercept token exchange.
- gotcha Autocommit is `OFF` by default, adhering to the Python DB-API specification. Transactions must be explicitly committed for changes to persist.
- gotcha Network connectivity issues, including timeouts, are common, especially when connecting from outside an Amazon EC2 instance or running long-duration queries. This can be due to firewall rules, VPC configurations, or client-side TCP/IP timeout settings.
- gotcha For loading large amounts of data into Redshift, using individual `INSERT` statements can be prohibitively slow and expensive.
- deprecated Amazon Redshift is ending support for creating new Python User-Defined Functions (UDFs) starting with Patch 198. Existing Python UDFs will continue to function but will be fully unsupported after June 30, 2026.
Install
-
pip install redshift-connector -
pip install redshift-connector[full]
Imports
- redshift_connector
import redshift_connector
Quickstart
import redshift_connector
import os
try:
# Connect to Redshift cluster using environment variables for credentials
conn = redshift_connector.connect(
host=os.environ.get('REDSHIFT_HOST', 'your-redshift-cluster.example.com'),
database=os.environ.get('REDSHIFT_DB', 'dev'),
port=int(os.environ.get('REDSHIFT_PORT', '5439')),
user=os.environ.get('REDSHIFT_USER', 'awsuser'),
password=os.environ.get('REDSHIFT_PASSWORD', 'your_password_here')
)
# Create a Cursor object
cursor = conn.cursor()
# Execute a query
cursor.execute("SELECT 1 as id, 'Hello, Redshift!' as message;")
# Retrieve the query result set
result = cursor.fetchall()
print("Query Result:", result)
# Close the cursor and connection
cursor.close()
conn.close()
except Exception as e:
print(f"An error occurred: {e}")