datacube.drivers.common_psql.as_role#

datacube.drivers.common_psql.as_role(conn, role)[source]#

Context manager to temporarily switch database roles.

E.g.:

with engine.connect() as conn:
    # This is executed as the session role configured into the engine
    conn.execute(query1)
    with as_role(conn, "odc_admin") as conn:
        # This is executed as the "odc_admin" role
        conn.execute(query2)
    # This is executed as the session role configured into the engine
    conn.execute(query3)
Parameters:
  • conn (Connection) – The base SQLAlchemy connection object.

  • role (str | None) – The role to switch to, or None to continue using the currently active role

Return type:

Generator[Connection]

Returns:

The SQLAlchemy connection object, switched to the specified role.