awswrangler.postgresql.connect

awswrangler.postgresql.connect(connection: str | None = None, secret_id: str | None = None, catalog_id: str | None = None, dbname: str | None = None, boto3_session: boto3.Session | None = None, ssl_context: bool | SSLContext | None = None, timeout: int | None = None, tcp_keepalive: bool = True) pg8000.Connection

Return a pg8000 connection from a Glue Catalog Connection.

https://github.com/tlocke/pg8000

Note

You MUST pass a connection OR secret_id. Here is an example of the secret structure in Secrets Manager: { “host”:”postgresql-instance-wrangler.dr8vkeyrb9m1.us-east-1.rds.amazonaws.com”, “username”:”test”, “password”:”test”, “engine”:”postgresql”, “port”:”3306”, “dbname”: “mydb” # Optional }

Parameters:
  • connection (str, optional) – Glue Catalog Connection name.

  • secret_id (str, optional) – 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, optional) – The ID of the Data Catalog. If none is provided, the AWS account ID is used by default.

  • dbname (str, optional) – Optional database name to overwrite the stored one.

  • boto3_session (boto3.Session(), optional) – Boto3 Session. The default boto3 session will be used if boto3_session receive None.

  • ssl_context (bool or SSLContext, optional) – This governs SSL encryption for TCP/IP sockets. This parameter is forward to pg8000. https://github.com/tlocke/pg8000#functions

  • timeout (int, optional) – 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 pg8000. https://github.com/tlocke/pg8000#functions

  • tcp_keepalive (bool) – If True then use TCP keepalive. The default is True. This parameter is forward to pg8000. https://github.com/tlocke/pg8000#functions

Returns:

pg8000 connection.

Return type:

pg8000.Connection

Examples

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