Skip to content

Tera

Render Tera templates from SQL β€” Rust's Jinja2-style templating engine.

96,431
extension loads Β· last 90 days
On this page

Technical Overview

One rendered string per row, without leaving SQL

What it is

How it works

  • β€’ Templates are cached: A template body is compiled once and reused across the rows of a query rather than re-parsed per row, which is why a single tera_render over a large table stays cheap. Inline template literals and named templates loaded from a template_path glob both go through the same compile-and-cache path.
  • β€’ Autoescape defaults to HTML-safe: HTML special characters in context values are escaped by default β€” correct for HTML and email, but it turns ampersands and apostrophes into & / ' when you're emitting configs, SQL, or JSON. Disable it for non-HTML output. See Tera's autoescape behavior.
  • β€’ Interpolation is not parameter-safe: Tera substitution is not an injection-safe parameterization mechanism. A template body assembled from untrusted input can render arbitrary SQL or shell strings. Keep the template surface fixed and authored by trusted code; only the JSON context should carry query data or user input, and bind any externally-sourced value as a real query parameter.

Tera vs MiniJinja

  • β€’ Pick Tera for the bigger feature surface: Tera ships a wider built-in filter and tester library, template inheritance via template_path, and macros β€” choose Tera when your templates lean on Jinja2's richer authoring features.
  • β€’ Pick MiniJinja for speed and Jinja2 fidelity: MiniJinja targets strict Python-Jinja2 semantics with a tiny, fast core. Choose minijinja when you want behavior that matches an existing Python Jinja codebase, or when render-rate per row matters most.
  • β€’ Either is fine for everyday substitution: For {{ name }} / {{ x | upper }} / {% for x in xs %}, both produce identical output. Lock in whichever syntax matches your team's existing templates.

Deep Dive

Technical Details

Install

INSTALL tera FROM community;
LOAD tera;

Quick Start

Variable substitution

SELECT tera_render('Hello {{ name }}!', '{"name":"World"}');

For loop

SELECT tera_render(
  '{% for x in xs %}{{ x }}{% if not loop.last %}, {% endif %}{% endfor %}',
  '{"xs":["A","B","C"]}'
);

Render a template file from disk

SELECT tera_render('email.html', '{"name":"Alice"}', template_path := './templates/*.html');

Reference

Extension Contents

Quick reference to all available functions and settings organized by category.

Name Description
Render
tera_render() Render a Tera template with JSON context.

API Reference

Function Documentation

Practical Examples

Cookbook

Real-world recipes and patterns for common use cases.

Platform Support

Compatibility

Extension availability may vary by platform and DuckDB version. Check below to ensure this extension supports your environment before installation.

Quick Facts

Release status Stable
Software License MIT
Pricing Free
Written In Rust
Source Available Yes
View on GitHub
Usage
96,431
loads Β· last 90 days

Platforms

  • Linux x86_64 aarch64
  • Linux (musl) Not available
  • macOS Intel Apple Silicon
  • Windows x86_64
  • WASM eh mvp threads
Compiled binary sizes
Platform Architecture Size
Linux x86_64 5.43 MB
Linux aarch64 4.97 MB
macOS Intel 3.32 MB
macOS Apple Silicon 2.93 MB
Windows x86_64 8.54 MB
WASM eh 577.9 KB
WASM mvp 964.0 KB
WASM threads 828.8 KB

Compressed download size from the Haybarn extension repository.

DuckDB & Haybarn

Release calendar