Generate safe Postgres-compliant SQL using ES6 tagged template literals. Current stable version is 5.0.1, requiring Node >=22. It prevents SQL injection by forcing all values through allowed APIs like sql.value() and sql.identifier(). Built by the Graphile team, it is fast, ships TypeScript types, and is designed for highly dynamic query building. Unlike pg-promise or knex, it uses template literals for composable, compile-time safe SQL construction.
npm install pg-sql2No compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows safe dynamic SQL construction with tagged templates, identifiers, values, join, and compile.
Upgrade to Node >=22 and use import syntax; if CJS required, use dynamic import or stick with v4.
Use sql.literal with trusted strings instead of sql.raw; for JSON, use sql.value with JSON.stringify.
Always use sql.value for untrusted or sensitive data; only use sql.literal with trusted constants.
Update imports: import sql from 'pg-sql2' and use sql`...` as before, but now call sql.compile(query).
Always wrap raw values with sql.value() and identifiers with sql.identifier().
Use sql.join for lists of fragments separated by commas, AND, OR, etc. For SQL JOINs, build manually.
Use import sql from 'pg-sql2' (ESM) or const { default: sql } = require('pg-sql2') (CJS).Wrap the value: sql`... ${sql.value(42)} ...`sql.identifier expects string or Symbol arguments; for multiple parts, pass as separate args: sql.identifier('schema', 'table').npm install pg-sql2; ensure tsconfig.json includes 'node' resolution and skipLibCheck is false if needed.
Upgrade to v4+ and use import sql from 'pg-sql2', then sql.compile(query).
No dependency data recorded yet.