Amazon Redshift connector for Python implementing Python Database API Specification 2.0
—
Essential database connectivity functionality that forms the foundation of the DB-API 2.0 interface. These operations provide connection establishment, query execution, result fetching, and transaction management capabilities required for all database interactions.
Creates database connections with comprehensive configuration options supporting various authentication methods, networking configurations, and Redshift-specific features.
def connect(
user: str = None,
database: str = None,
password: str = None,
port: int = None,
host: str = None,
source_address: str = None,
unix_sock: str = None,
ssl: bool = None,
sslmode: str = None,
timeout: int = None,
max_prepared_statements: int = None,
tcp_keepalive: bool = None,
tcp_keepalive_idle: int = None,
tcp_keepalive_interval: int = None,
tcp_keepalive_count: int = None,
application_name: str = None,
replication: str = None,
# IAM Authentication Parameters
idp_host: str = None,
db_user: str = None,
app_id: str = None,
app_name: str = None,
preferred_role: str = None,
principal_arn: str = None,
access_key_id: str = None,
secret_access_key: str = None,
session_token: str = None,
profile: str = None,
credentials_provider: str = None,
region: str = None,
cluster_identifier: str = None,
iam: bool = None,
# Identity Provider Parameters
client_id: str = None,
idp_tenant: str = None,
client_secret: str = None,
partner_sp_id: str = None,
idp_response_timeout: int = None,
listen_port: int = None,
login_to_rp: str = None,
login_url: str = None,
auto_create: bool = None,
db_groups: list[str] = None,
force_lowercase: bool = None,
allow_db_user_override: bool = None,
# Protocol and Configuration Parameters
client_protocol_version: int = None,
database_metadata_current_db_only: bool = None,
ssl_insecure: bool = None,
web_identity_token: str = None,
role_session_name: str = None,
role_arn: str = None,
iam_disable_cache: bool = None,
auth_profile: str = None,
endpoint_url: str = None,
provider_name: str = None,
scope: str = None,
numeric_to_float: bool = False,
# Serverless Parameters
is_serverless: bool = False,
serverless_acct_id: str = None,
serverless_work_group: str = None,
group_federation: bool = None,
identity_namespace: str = None,
idc_client_display_name: str = None,
idc_region: str = None,
issuer_url: str = None,
token: str = None,
token_type: str = None,
) -> Connection:
"""
Establishes a Connection to an Amazon Redshift cluster.
Parameters:
- user: Username for authentication
- database: Database name to connect to
- password: Password for authentication
- host: Hostname of the Redshift cluster
- port: Port number (default 5439)
- ssl: Enable SSL (default True)
- iam: Enable IAM authentication (default False)
- cluster_identifier: Redshift cluster identifier for IAM
- And 60+ additional parameters for various authentication and configuration options
Returns:
Connection object for database operations
"""Database connection objects providing transaction control, cursor creation, and connection lifecycle management.
class Connection:
"""Database connection object implementing DB-API 2.0 interface."""
def cursor(self) -> Cursor:
"""Create and return a new Cursor object."""
def commit(self) -> None:
"""Commit the current transaction."""
def rollback(self) -> None:
"""Roll back the current transaction."""
def close(self) -> None:
"""Close the connection."""
def run(self, sql: str, stream=None, **params) -> tuple:
"""Execute SQL statement and return results as tuple."""
# Context manager support
def __enter__(self) -> 'Connection': ...
def __exit__(self, exc_type, exc_value, traceback) -> None: ...
# Connection properties
@property
def autocommit(self) -> bool:
"""Get/set autocommit mode."""
@autocommit.setter
def autocommit(self, value: bool) -> None: ...
# Metadata properties
def client_os_version(self) -> str:
"""Return client OS version string."""
def is_single_database_metadata(self) -> bool:
"""Return whether metadata queries are limited to current database."""
@property
def description(self) -> list | None:
"""Return description of last query result."""Cursor objects for executing SQL statements, managing prepared statements, and retrieving query results.
class Cursor:
"""Database cursor for executing queries and fetching results."""
def execute(self, operation: str, args=None, stream=None, merge_socket_read: bool = False) -> 'Cursor':
"""
Execute a SQL statement.
Parameters:
- operation: SQL statement to execute
- args: Parameters for the SQL statement
- stream: Optional stream for result processing
- merge_socket_read: Optimize socket reading for large results
Returns:
Self for method chaining
"""
def executemany(self, operation: str, param_sets) -> 'Cursor':
"""
Execute a SQL statement multiple times with different parameter sets.
Parameters:
- operation: SQL statement to execute
- param_sets: Sequence of parameter tuples/dicts
Returns:
Self for method chaining
"""
def callproc(self, procname: str, parameters=None):
"""
Call a stored procedure.
Parameters:
- procname: Name of the stored procedure
- parameters: Procedure parameters
Returns:
Procedure result
"""
# Context manager support
def __enter__(self) -> 'Cursor': ...
def __exit__(self, exc_type, exc_value, traceback) -> None: ...
def close(self) -> None:
"""Close the cursor."""Methods for retrieving query results in various formats with support for large result sets and streaming.
class Cursor:
def fetchone(self) -> list | None:
"""
Fetch the next row of a query result set.
Returns:
Next row as a list, or None if no more rows available
"""
def fetchmany(self, num: int = None) -> tuple:
"""
Fetch a specified number of rows from the query result.
Parameters:
- num: Number of rows to fetch (default: arraysize)
Returns:
Tuple of rows, each row as a list
"""
def fetchall(self) -> tuple:
"""
Fetch all remaining rows of a query result set.
Returns:
Tuple of all remaining rows, each row as a list
"""
# Iterator support
def __iter__(self) -> 'Cursor': ...
def __next__(self) -> list: ...
# Cursor properties
@property
def connection(self) -> Connection | None:
"""Reference to the associated Connection object."""
@property
def rowcount(self) -> int:
"""Number of rows affected by the last execute() call."""
@property
def redshift_rowcount(self) -> int:
"""Redshift-specific row count information."""
@property
def description(self) -> list[tuple] | None:
"""
Description of query result columns.
Returns:
List of 7-tuples (name, type_code, display_size, internal_size, precision, scale, null_ok)
"""
@property
def arraysize(self) -> int:
"""Number of rows to fetch at a time with fetchmany()."""
@arraysize.setter
def arraysize(self, size: int) -> None: ...High-performance bulk data operations for efficient data loading and processing.
class Cursor:
def insert_data_bulk(
self,
table: str,
column_names: list[str],
data_rows: list[list]
) -> None:
"""
Perform bulk insert operation.
Parameters:
- table: Target table name
- column_names: List of column names
- data_rows: List of data rows to insert
"""Distributed transaction support for applications requiring two-phase commit protocol.
class Connection:
def xid(self, format_id: int, global_transaction_id: str, branch_qualifier: str) -> tuple:
"""Create a transaction ID for two-phase commit."""
def tpc_begin(self, xid: tuple) -> None:
"""Begin a two-phase commit transaction."""
def tpc_prepare(self) -> None:
"""Prepare the current two-phase commit transaction."""
def tpc_commit(self, xid: tuple = None) -> None:
"""Commit a prepared two-phase commit transaction."""
def tpc_rollback(self, xid: tuple = None) -> None:
"""Roll back a prepared two-phase commit transaction."""
def tpc_recover(self) -> list[tuple]:
"""Return a list of pending transaction IDs."""Configurable parameter binding styles for SQL statement parameterization.
# Module-level paramstyle configuration
paramstyle: str = "format" # Default parameter style
# Set paramstyle at module level (affects all new cursors)
redshift_connector.paramstyle = 'qmark'
# Set paramstyle on individual cursor
cursor.paramstyle = 'named'
# Supported parameter styles:
# - 'format': WHERE name=%s
# - 'qmark': WHERE name=?
# - 'numeric': WHERE name=:1
# - 'named': WHERE name=:param_name
# - 'pyformat': WHERE name=%(param_name)sUtility functions for validating connection parameters and testing connectivity.
def validate_keepalive_values(idle: int, interval: int, count: int) -> None:
"""
Validate TCP keepalive parameter values.
Parameters:
- idle: Seconds before sending keepalive probes
- interval: Seconds between keepalive probes
- count: Number of failed probes before considering connection dead
Raises:
ValueError: If any parameter is invalid
"""Install with Tessl CLI
npx tessl i tessl/pypi-redshift-connector