<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/how-to-use-a-list-tuple-array-in-django-with-a-raw-sql-cursor-vkedwliuu" -->

---
title: How to use a list/tuple/array in Django with a raw SQL...
description: When using Django's raw SQL cursor with psycopg v3, the old pattern of passing a tuple with the IN operator no longer works and throws a ProgrammingError. The...
canonical: https://daily.dev/posts/how-to-use-a-list-tuple-array-in-django-with-a-raw-sql-cursor-vkedwliuu
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: How to use a list/tuple/array in Django with a raw SQL cursor | daily.dev
og:description: When using Django's raw SQL cursor with psycopg v3, the old pattern of passing a tuple with the IN operator no longer works and throws a ProgrammingError. The...
og:url: https://daily.dev/posts/how-to-use-a-list-tuple-array-in-django-with-a-raw-sql-cursor-vkedwliuu
og:image: https://api.daily.dev/og/posts/vKEDWliuU.png
og:image:alt: How to use a list/tuple/array in Django with a raw SQL cursor
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# How to use a list/tuple/array in Django with a raw SQL cursor

**[Planet Python](https://daily.dev/sources/planetpython)** · 2 min read · 0 upvotes · 0 comments

## Summary

When using Django's raw SQL cursor with psycopg v3, the old pattern of passing a tuple with the IN operator no longer works and throws a ProgrammingError. The fix is to use the ANY(%s) operator with a list instead of a tuple. For lists of strings, ANY(%s) also fails with a type error; the workaround is to dynamically build the SQL string with individual %s placeholders joined by OR, passing the list directly as parameters.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <http://www.peterbe.com/plog/how-to-use-a-listtuplearray-in-django-with-a-raw-sql-cursor>

## Questions this post answers

### Why does using IN %s with a tuple in a Django raw SQL cursor query now throw a syntax error in Postgres?

Psycopg3 no longer supports passing a tuple as a parameter for IN %s, which worked under psycopg2. It now raises django.db.utils.ProgrammingError with a syntax error near the stringified tuple, because psycopg3 adapts tuples differently. The fix is to replace IN %s with the ANY(%s) operator and pass a plain list instead of a tuple as the parameter.

_daily.dev surfaces changes like the psycopg2-to-psycopg3 parameter shift so Django upgrades don't break in production._

### How do I pass a list of strings into a Postgres ANY() clause from Django raw SQL without a type error?

Passing a Python list of strings directly to ANY(%s) can raise DataError: invalid input syntax for type integer, since Postgres tries to interpret the array against the column's type. A safe workaround is to dynamically build the SQL with one OR-joined 'column = %s' comparison per value and pass the list as individual parameters to cursor.execute, avoiding array casting issues entirely.

_developers debugging raw SQL parameter binding in Django can track these gotchas via daily.dev._

## Similar posts on daily.dev

- [PostgreSQL: Psycopg 3.3 released](https://daily.dev/posts/postgresql-psycopg-3-3-released-ewfhnirnz) · PostgreSQL · 1 upvotes · 0 comments
- [PostgreSQL adapter for Python](https://daily.dev/posts/postgresql-adapter-for-python-7mss4n5wr) · Psycopg · 0 upvotes · 0 comments
- [Introducing Apache Arrow Support in mssql-python](https://daily.dev/posts/introducing-apache-arrow-support-in-mssql-python-yq9micn57) · Planet Python · 0 upvotes · 0 comments
- [I made PostgreSQL INSERT operations 36 times faster in Python](https://daily.dev/posts/i-made-postgresql-insert-operations-36-times-faster-in-python-mnsasltcx) · ITNEXT · 0 upvotes · 0 comments
- [Write SQL Server Your Way: Dual Parameter Style Benefits- Microsoft for Python Developers Blog](https://daily.dev/posts/write-sql-server-your-way-dual-parameter-style-benefits--microsoft-for-python-developers-blog-nio0f5hxj) · Planet Python · 0 upvotes · 0 comments

---

Tags: [#python](https://daily.dev/tags/python), [#postgresql](https://daily.dev/tags/postgresql), [#django](https://daily.dev/tags/django)

[View this post on daily.dev](https://daily.dev/posts/how-to-use-a-list-tuple-array-in-django-with-a-raw-sql-cursor-vkedwliuu)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"How to use a list/tuple/array in Django with a raw SQL cursor","url":"https://daily.dev/posts/how-to-use-a-list-tuple-array-in-django-with-a-raw-sql-cursor-vkedwliuu","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/how-to-use-a-list-tuple-array-in-django-with-a-raw-sql-cursor-vkedwliuu"},"datePublished":"2026-07-14T19:50:11.097Z","dateModified":"2026-09-14T06:25:21.591Z","description":"When using Django's raw SQL cursor with psycopg v3, the old pattern of passing a tuple with the IN operator no longer works and throws a ProgrammingError. The...","image":"https://media.daily.dev/image/upload/s--58gMhC4P--/f_auto/v1722860399/public/Placeholder%2012","thumbnailUrl":"https://media.daily.dev/image/upload/s--58gMhC4P--/f_auto/v1722860399/public/Placeholder%2012","isAccessibleForFree":true,"articleSection":"Planet Python","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Planet Python","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/eac3cc584cb043b5a242d13752bd3d0c","url":"https://daily.dev/sources/planetpython"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/how-to-use-a-list-tuple-array-in-django-with-a-raw-sql-cursor-vkedwliuu","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"python,postgresql,django","timeRequired":"PT2M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Planet Python","item":"https://daily.dev/sources/planetpython"},{"@type":"ListItem","position":3,"name":"How to use a list/tuple/array in Django with a raw SQL cursor"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/how-to-use-a-list-tuple-array-in-django-with-a-raw-sql-cursor-vkedwliuu#faq","mainEntity":[{"@type":"Question","name":"Why does using IN %s with a tuple in a Django raw SQL cursor query now throw a syntax error in Postgres?","acceptedAnswer":{"@type":"Answer","text":"Psycopg3 no longer supports passing a tuple as a parameter for IN %s, which worked under psycopg2. It now raises django.db.utils.ProgrammingError with a syntax error near the stringified tuple, because psycopg3 adapts tuples differently. The fix is to replace IN %s with the ANY(%s) operator and pass a plain list instead of a tuple as the parameter. daily.dev surfaces changes like the psycopg2-to-psycopg3 parameter shift so Django upgrades don't break in production."}},{"@type":"Question","name":"How do I pass a list of strings into a Postgres ANY() clause from Django raw SQL without a type error?","acceptedAnswer":{"@type":"Answer","text":"Passing a Python list of strings directly to ANY(%s) can raise DataError: invalid input syntax for type integer, since Postgres tries to interpret the array against the column's type. A safe workaround is to dynamically build the SQL with one OR-joined 'column = %s' comparison per value and pass the list as individual parameters to cursor.execute, avoiding array casting issues entirely. developers debugging raw SQL parameter binding in Django can track these gotchas via daily.dev."}}]}
```

