Search code examples
airflow

How to get xcom as a PostgresOperator parameter?


I created a xcom and I would like to get the result as a PostgresOperator parameter. I tried this

my_task = PostgresOperator(
    task_id=‘my_task',
    postgres_conn_id=config.get(env, 'redshift_conn'),
    sql="my_task.sql",
    params={
            ‘my_parameter': {{ int(ti.xcom_pull(task_ids=‘previous_task')) }}
    },
    dag=dag
) 

Solution

  • You need to use templating when accessing xcom within an operator.

    my_task = PostgresOperator(
        task_id='my_task',
        postgres_conn_id=config.get(env, 'redshift_conn'),
        sql="my_task.sql",
        params={
                'my_parameter': "{{ti.xcom_pull(task_ids='previous_task')}}"
        },
        dag=dag
    )