awswrangler.redshift.connect

awswrangler.redshift.connect(connection: str | None = None, secret_id: str | None = None, catalog_id: str | None = None, dbname: str | None = None, boto3_session: Session | None = None, ssl: bool = True, timeout: int | None = None, max_prepared_statements: int = 1000, tcp_keepalive: bool = True, **kwargs: Any) redshift_connector.Connection

Return a redshift_connector connection from a Glue Catalog or Secret Manager.

Note

You MUST pass a connection OR secret_id. Here is an example of the secret structure in Secrets Manager: { “host”:”my-host.us-east-1.redshift.amazonaws.com”, “username”:”test”, “password”:”test”, “engine”:”redshift”, “port”:”5439”, “dbname”: “mydb” }

https://github.com/aws/amazon-redshift-python-driver

Parameters:
  • connection (str | None) – Glue Catalog Connection name.

  • secret_id (str | None) – Specifies the secret containing the connection details that you want to retrieve. You can specify either the Amazon Resource Name (ARN) or the friendly name of the secret.

  • catalog_id (str | None) – The ID of the Data Catalog. If none is provided, the AWS account ID is used by default.

  • dbname (str | None) – Optional database name to overwrite the stored one.

  • boto3_session (Session | None) – The default boto3 session will be used if boto3_session is None.

  • ssl (bool) – This governs SSL encryption for TCP/IP sockets. This parameter is forward to redshift_connector. https://github.com/aws/amazon-redshift-python-driver

  • timeout (int | None) – This is the time in seconds before the connection to the server will time out. The default is None which means no timeout. This parameter is forward to redshift_connector. https://github.com/aws/amazon-redshift-python-driver

  • max_prepared_statements (int) – This parameter is forward to redshift_connector. https://github.com/aws/amazon-redshift-python-driver

  • tcp_keepalive (bool) – If True then use TCP keepalive. The default is True. This parameter is forward to redshift_connector. https://github.com/aws/amazon-redshift-python-driver

  • **kwargs (Any) – Forwarded to redshift_connector.connect. e.g. is_serverless=True, serverless_acct_id='...', serverless_work_group='...'

Return type:

Connection

Returns:

redshift_connector connection.

Examples

Fetching Redshift connection from Glue Catalog

>>> import awswrangler as wr
>>> with wr.redshift.connect("MY_GLUE_CONNECTION") as con:
...     with con.cursor() as cursor:
...         cursor.execute("SELECT 1")
...         print(cursor.fetchall())

Fetching Redshift connection from Secrets Manager

>>> import awswrangler as wr
>>> with wr.redshift.connect(secret_id="MY_SECRET") as con:
...     with con.cursor() as cursor:
...         cursor.execute("SELECT 1")
...         print(cursor.fetchall())