jinjasql2
Generate SQL Queries and Corresponding Bind Parameters using a Jinja2 Template
What it is and what it does
JinjaSQL is a template engine that generates parameterized SQL queries from Jinja2 templates. It processes template variables and conditional logic, then outputs a query with placeholders and a separate dictionary or tuple of bind parameters, ensuring values are never inlined into the SQL string itself. The package supports multiple parameter styles (named, format, qmark, numeric, pyformat, asyncpg) to match different database drivers' conventions.
The package is designed for scenarios where raw SQL is necessary—reporting, aggregation, bulk migrations, and multi-table queries—rather than as an ORM replacement. It protects against SQL injection by binding all template variables as parameters, though templates themselves must be trusted code (the sqlsafe and identifier filters allow deliberate inlining when needed). You prepare the query and parameters, then execute them using your database driver of choice.
Use it for:
- Build dynamic reporting queries with conditional WHERE clauses, GROUP BY, and aggregations that an ORM cannot express cleanly
- Generate parameterized bulk update or migration scripts using Jinja macros and loops while maintaining SQL injection safety
- Construct multi-table SELECT queries with optional joins and filters based on runtime conditions
- Support IN clauses with the inclause filter to bind list elements as separate parameters for database drivers
- Escape table and column names safely using the identifier filter when dynamic schema references are required
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Generates parameterized SQL queries from Jinja2 templates, automatically binding variables to prevent SQL injection while supporting conditional logic, macros, and multiple parameter styles.
Yes, if you need to generate dynamic SQL queries safely in Python and your use case genuinely requires raw SQL (reporting, complex aggregations, bulk operations). The package is lightweight, actively maintained, has no known vulnerabilities, and supports modern Python versions. Not necessary if an ORM handles your queries adequately.
Install
jinjasql2 on PyPI
pip
pip install jinjasql2uv
uv add jinjasql2poetry
poetry add jinjasql2Installing jinjasql2
Before you install
Low friction: pure Python wheel with only Jinja2 as a runtime dependency. Actively maintained with a recent release and no known vulnerabilities.
License in practice
MIT license permits unrestricted use, modification, and distribution in both open-source and proprietary projects with minimal obligations.
Quickstart
from jinjasql import JinjaSql
j = JinjaSql()
template = "SELECT * FROM users WHERE id = {{ user_id }}"
query, bind_params = j.prepare_query(template, {"user_id": 123})
# query: "SELECT * FROM users WHERE id = :user_id_1"
# bind_params: {"user_id_1": 123}
Verify before relying
- Performance characteristics when handling large or deeply nested template contexts
- Compatibility with specific database drivers beyond the examples given (Django, asyncpg)
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.8) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — Jinja2 |
| Maintenance | actively maintained — 38 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 142,297/month — #11,214 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: jinjasql2-0.1.13-py3-none-any.whl
Keywords: Jinja2, SQL, Python, Template
Tags
More Database packages
psycopg2-binary is a PostgreSQL database…
copyleft · top 1,000 on PyPI
redisPython client library for connecting to and…
permissive · top 1,000 on PyPI
ydbYDB Python SDK is the official client library…
permissive · top 1,000 on PyPI
snowflake-connector-pythonConnects Python applications to Snowflake data…
permissive · top 1,000 on PyPI
sqlparsesqlparse tokenizes SQL text into a tree of…
permissive · top 1,000 on PyPI
dbt-adaptersProvides base adapter protocols and shared…
permissive · top 1,000 on PyPI
jinjasqlGenerates parameterized SQL queries from Jinja2…
permissive · top 15,000 on PyPI
Jinja2Jinja2 is a templating engine that renders…
permissive · top 100 on PyPI
sqlvalidatorFormats, validates, and checks SQL SELECT…
permissive · top 15,000 on PyPI
sqlparamsConverts SQL queries between different…
permissive · top 5,000 on PyPI
sqlfluffSQLFluff is a SQL linter and auto-fixer that…
permissive · top 5,000 on PyPI
buildpgbuildpg constructs PostgreSQL prepared…
permissive · top 15,000 on PyPI
sqlfluff-templater-dbtEnables SQLFluff to lint and auto-fix dbt…
permissive · top 5,000 on PyPI
jinja_partialsAdds a `render_partial()` function to Jinja2…
permissive · top 5,000 on PyPI
mkdocs-macros-pluginExtends MkDocs with Jinja2 templating to use…
permissive · top 5,000 on PyPI
python-sqlConstructs SQL queries programmatically using…
permissive · top 15,000 on PyPI