PostgreSQL Connection

The Postgres connection type provides connection to a Postgres database.

Configuring the Connection

Host (required)

The host to connect to.

Database (optional)

Specify the name of the database to connect to.

Note

If you want to define a default database schema:

Login (required)

Specify the user name to connect.

Password (required)

Specify the password to connect.

Extra (optional)

Specify the extra parameters (as json dictionary) that can be used in Postgres connection. The following parameters out of the standard python parameters are supported:

  • sslmode - This option determines whether or with what priority a secure SSL TCP/IP connection will be negotiated with the server. There are six modes: disable, allow, prefer, require, verify-ca, verify-full.

  • sslcert - This parameter specifies the file name of the client SSL certificate, replacing the default.

  • sslkey - This parameter specifies the file name of the client SSL key, replacing the default.

  • sslrootcert - This parameter specifies the name of a file containing SSL certificate authority (CA) certificate(s).

  • sslcrl - This parameter specifies the file name of the SSL certificate revocation list (CRL).

  • application_name - Specifies a value for the application_name configuration parameter.

  • keepalives_idle - Controls the number of seconds of inactivity after which TCP should send a keepalive message to the server.

  • client_encoding: specifies client encoding(character set) of the client connection. Refer to Postgres supported character sets

  • cursor - Specifies the cursor type to use when querying the database. You can choose one of the following:

    • dictcursor: Returns query results as Python dictionaries using psycopg2.extras.DictCursor.

    • realdictcursor: Similar to DictCursor, but uses psycopg2.extras.RealDictCursor for slightly better performance.

    • namedtuplecursor: Returns query results as named tuples using psycopg2.extras.NamedTupleCursor.

    For more information, refer to the psycopg2 documentation on connection and cursor subclasses.

More details on all Postgres parameters supported can be found in Postgres documentation.

Example “extras” field:

{
   "sslmode": "verify-ca",
   "sslcert": "/tmp/client-cert.pem",
   "sslca": "/tmp/server-ca.pem",
   "sslkey": "/tmp/client-key.pem"
}

The following extra parameters use for additional Hook configuration:

  • iam - If set to True than use AWS IAM database authentication for Amazon RDS, Amazon Aurora or Amazon Redshift.

  • aws_conn_id - AWS Connection ID which use for authentication via AWS IAM, if not specified then aws_default is used.

  • redshift - Used when AWS IAM database authentication enabled. If set to True than authenticate to Amazon Redshift Cluster, otherwise to Amazon RDS or Amazon Aurora.

  • cluster-identifier - The unique identifier of the Amazon Redshift Cluster that contains the database for which you are requesting credentials. This parameter is case sensitive. If not specified than hostname from Connection Host is used.

Example “extras” field (Amazon RDS PostgreSQL or Amazon Aurora PostgreSQL):

{
   "iam": true,
   "aws_conn_id": "aws_awesome_rds_conn"
}

Example “extras” field (Amazon Redshift):

{
   "iam": true,
   "aws_conn_id": "aws_awesome_redshift_conn",
   "redshift": "/tmp/server-ca.pem",
   "cluster-identifier": "awesome-redshift-identifier"
}

When specifying the connection as URI (in AIRFLOW_CONN_{CONN_ID} variable) you should specify it following the standard syntax of DB connections, where extras are passed as parameters of the URI (note that all components of the URI should be URL-encoded).

For example:

export AIRFLOW_CONN_POSTGRES_DEFAULT='postgresql://postgres_user:XXXXXXXXXXXX@1.1.1.1:5432/postgresdb?sslmode=verify-ca&sslcert=%2Ftmp%2Fclient-cert.pem&sslkey=%2Ftmp%2Fclient-key.pem&sslrootcert=%2Ftmp%2Fserver-ca.pem'

Was this entry helpful?