Apache Airflow Postgres Provider

6.6.2 · active · verified Fri Apr 10

The Apache Airflow Postgres Provider offers a comprehensive set of operators, hooks, and sensors for seamless interaction with PostgreSQL databases within Airflow DAGs. It enables tasks such as executing SQL queries, transferring data, and managing database connections. The current version is 6.6.2, and it follows a frequent release cadence to support new Airflow features and address issues.

Warnings

Install

Imports

Quickstart

This example DAG defines two tasks using the `PostgresOperator`. The first task creates a 'pet' table if it doesn't already exist, and the second task populates it with sample data. Ensure you have an Airflow connection named 'postgres_default' configured in the Airflow UI, pointing to your PostgreSQL instance.

import datetime
from airflow import DAG
from airflow.providers.postgres.operators.postgres import PostgresOperator

with DAG(
    dag_id="postgres_example_dag",
    start_date=datetime.datetime(2023, 1, 1),
    schedule_interval="@once",
    catchup=False,
    tags=["postgres", "example"]
) as dag:
    create_pet_table = PostgresOperator(
        task_id="create_pet_table",
        postgres_conn_id="postgres_default",
        sql="""
            CREATE TABLE IF NOT EXISTS pet (
            pet_id SERIAL PRIMARY KEY,
            name VARCHAR NOT NULL,
            pet_type VARCHAR NOT NULL,
            birth_date DATE NOT NULL,
            owner VARCHAR NOT NULL
            );
        """
    )

    populate_pet_table = PostgresOperator(
        task_id="populate_pet_table",
        postgres_conn_id="postgres_default",
        sql="""
            INSERT INTO pet (name, pet_type, birth_date, owner)
            VALUES ('Fido', 'Dog', '2020-01-01', 'Alice'),
                   ('Whiskers', 'Cat', '2019-05-15', 'Bob');
        """
    )

    create_pet_table >> populate_pet_table

view raw JSON →