DEV Community

Joe Auty
Joe Auty

Posted on

1

How to Create Prepared Statements with the Airflow PostgresOperator

Airflow's PostgresOperator includes a field called parameters for providing SQL parameters for prepared statements.

We wanted to use named parameters since our parameter order would vary, which meant understanding the syntax. In their provided example: SELECT * FROM pet WHERE birth_date BETWEEN SYMMETRIC %(begin_date)s AND %(end_date)s, the format here was unclear - for example the trailing s (we guessed this meant string, but queries with numerical inputs were failing without the s.

After some research we realized that the underlying dependency here was Psycopg. From this page:

Named arguments are supported too using %(name)s placeholders in the query and specifying the values into a mapping. Using named arguments allows to specify the values in any order and to repeat the same value in several places in the query:
Enter fullscreen mode Exit fullscreen mode

and:

The variables placeholder must always be a %s, even if a different placeholder (such as a %d for integers or %f for floats) may look more appropriate for the type.
Enter fullscreen mode Exit fullscreen mode

So, follow the above format for your variable placeholders, and ensure that the variable passed in is cast to the correct format (i.e. don't quote numbers).

Top comments (0)

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay