airflow.hooks.postgres_hook

Module Contents

class airflow.hooks.postgres_hook.PostgresHook(*args, **kwargs)[source]

Bases: airflow.hooks.dbapi_hook.DbApiHook

Interact with Postgres.

You can specify ssl parameters in the extra field of your connection as {"sslmode": "require", "sslcert": "/path/to/cert.pem", etc}. Also you can choose cursor as {"cursor": "dictcursor"}. Refer to the psycopg2.extras for more details.

Note: For Redshift, use keepalives_idle in the extra connection parameters and set it to less than 300 seconds.

Note: For AWS IAM authentication, use iam in the extra connection parameters and set it to true. Leave the password field empty. This will use the “aws_default” connection to get the temporary token unless you override in extras. extras example: {"iam":true, "aws_conn_id":"my_aws_conn"} For Redshift, also use redshift in the extra connection parameters and set it to true. The cluster-identifier is extracted from the beginning of the host field, so is optional. It can however be overridden in the extra field. extras example: {"iam":true, "redshift":true, "cluster-identifier": "my_cluster_id"}

conn_name_attr = postgres_conn_id[source]
default_conn_name = postgres_default[source]
supports_autocommit = True[source]
_get_cursor(self, raw_cursor)[source]
get_conn(self)[source]
copy_expert(self, sql, filename, open=open)[source]

Executes SQL using psycopg2 copy_expert method. Necessary to execute COPY command without access to a superuser.

Note: if this method is called with a “COPY FROM” statement and the specified input file does not exist, it creates an empty file and no data is loaded, but the operation succeeds. So if users want to be aware when the input file does not exist, they have to check its existence by themselves.

bulk_load(self, table, tmp_file)[source]

Loads a tab-delimited file into a database table

bulk_dump(self, table, tmp_file)[source]

Dumps a database table into a tab-delimited file

static _serialize_cell(cell, conn)[source]

Postgresql will adapt all arguments to the execute() method internally, hence we return cell without any conversion.

See http://initd.org/psycopg/docs/advanced.html#adapting-new-types for more information.

Parameters
  • cell (object) – The cell to insert into the table

  • conn (connection object) – The database connection

Returns

The cell

Return type

object

get_iam_token(self, conn)[source]

Uses AWSHook to retrieve a temporary password to connect to Postgres or Redshift. Port is required. If none is provided, default is used for each service