Database Error: syntax error at or near" dbt postgres
Fixes Postgres syntax errors in dbt models by reading the compiled SQL in target/compiled, finding the exact token Postgres rejects, and fixing the Jinja or SQL that produced it. Use when dbt run fails on Postgres with a syntax error. Not for compile-time Jinja errors, which fail before any SQL reaches Postgres.
TL;DR
Postgres rejected the rendered SQL. The model SQL you wrote is not what ran; Jinja rendering produced the bad syntax. Open the compiled file under target/compiled/, find the token named in the error, fix the model or macro that generated it, and rerun.
Error
"Database Error: syntax error at or near" dbt postgresSteps
- Run
dbt compile --select [MODEL NAME]to regenerate the compiled SQL. Expected: compilation succeeds, proving the problem is in the rendered SQL. - Open
target/compiled/[PROJECT]/models/[PATH]/[MODEL].sqland find the token from the error message (the word after "at or near"). Expected: you see the malformed SQL around that token. - Trace the bad fragment back to the model or macro: look for a Jinja expression that rendered empty, a missing comma, or an unquoted reserved word. Expected: you identify the source lines.
- Fix the model SQL or macro (quote the identifier, handle the empty variable, add the comma). Expected: the source no longer generates the bad fragment.
- Run
dbt run --select [MODEL NAME]. Expected: the model builds without a syntax error.
When to use
dbt runon Postgres fails with "syntax error at or near".- The error appeared after editing Jinja in the model.
When not to use
- dbt compile itself fails (a Jinja or parsing problem, not a Postgres problem).
- The same SQL runs fine directly in psql (then the issue is how dbt renders or quotes it).
Tool compatibility
- dbt Core 1.0 and later with dbt-postgres. The technique (read compiled SQL) works on every adapter.
Variant phrasings
syntax error at or near ","
A Jinja loop or conditional emitted a stray comma; check the generated select list.
syntax error at or near "select"
Often an empty Jinja expression left a dangling keyword; check for variables that rendered empty.
Why it happens
dbt sends fully rendered SQL to Postgres. Anything Jinja generates, including empty strings from undefined variables or misplaced commas from loops, becomes part of the statement Postgres parses.
Edge cases
- Reserved words as column names need quoting; dbt does not quote them automatically.
- A macro returning an empty string inside a select list produces
select , col, which Postgres rejects. {{ config(...) }}blocks do not render into SQL, but stray text after them does.
Provenance
Resolved from the public thread: https://vectle.com/posts/pstrFhqr9rOPFDCVuYprGcrA
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.