{"id":41711,"library":"pg-query-formatter","title":"pg-query-formatter","description":"Lightweight query string formatter for PostgreSQL in Node.js (v0.0.5, last updated 2014). Provides a printf-like syntax for safely building SQL queries with escaping for identifiers, literals, and subqueries. Supports parameterized queries via toParam() for use with node-postgres. Currently in maintenance mode; no updates since 2014. Alternative libraries like pg-promise, squel, or knex may be more actively maintained.","status":"maintenance","version":"0.0.5","language":"javascript","source_language":"en","source_url":"git://github.com/naturalatlas/pg-query-formatter","tags":["javascript","pg","db","database","query","postgres"],"install":[{"cmd":"npm install pg-query-formatter","lang":"bash","label":"npm"},{"cmd":"yarn add pg-query-formatter","lang":"bash","label":"yarn"},{"cmd":"pnpm add pg-query-formatter","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"CommonJS only; no ESM support. Use require().","wrong":"import Query from 'pg-query-formatter';","symbol":"Query","correct":"const Query = require('pg-query-formatter');"},{"note":"Query must be instantiated with new.","wrong":"const sql = Query('SELECT %L', 'hello');","symbol":"new Query(fmt, ...values)","correct":"const sql = new Query('SELECT %L', 'hello');"},{"note":"Query.List is a constructor; must use new.","wrong":"const list = Query.List(' AND ');","symbol":"Query.List","correct":"const list = new Query.List(' AND ');"}],"quickstart":{"code":"const Query = require('pg-query-formatter');\n\n// Basic select with identifier and literal\nconst sql = new Query('SELECT * FROM %I WHERE name = %L', 'teachers', 'George');\nconsole.log(sql.toString());\n// Output: SELECT * FROM teachers WHERE name = 'George'\n\n// Parameterized query for safe database execution\nconst param = sql.toParam();\nconsole.log(param.text); // SELECT * FROM teachers WHERE name = $1\nconsole.log(param.values); // [ 'George' ]\n\n// Array expansion for IN clause\nconst sql2 = new Query('SELECT * FROM teachers WHERE name IN (%L)', ['George', 'Jorge', 'Georgio']);\nconsole.log(sql2.toString());\n// Output: SELECT * FROM teachers WHERE name IN ('George', 'Jorge', 'Georgio')\n\n// Using Query.List for dynamic AND clauses\nconst where = new Query.List(' AND ');\nwhere.append('age > %L', 20);\nwhere.append('age < %L', 30);\nconst sql3 = new Query('SELECT %I FROM teachers WHERE %Q', ['id', 'name'], where);\nconsole.log(sql3.toString());\n// Output: SELECT id, name FROM teachers WHERE age > 20 AND age < 30","lang":"javascript","description":"Demonstrates basic construction, parameterized output, array expansion, and Query.List for dynamic SQL building."},"warnings":[{"fix":"Migrate to a maintained library like pg-promise (https://github.com/vitaly-t/pg-promise) or knex (https://knexjs.org).","message":"Package is no longer maintained; last update 2014. Use pg-promise, squel, or knex instead.","severity":"deprecated","affected_versions":">=0.0.0"},{"fix":"Always use new Query(fmt, ...values).","message":"The constructor requires the 'new' keyword; calling Query(...) without new will return undefined.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Use %L for literals and %I for identifiers to safely escape input. Avoid %s with user-supplied data.","message":"Untrusted input passed to %s (unescaped string) can lead to SQL injection. Only use %s for trusted values.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Always call .toString() or .toParam() to get the formatted output.","message":"Query.List requires call to .toString() or toParam(); simply logging the object will not show the query.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"search_vec":"'2014':16,53 'activ':66 'altern':54 'build':25 'current':46 'databas':71 'db':70 'escap':29 'formatt':4,8 'identifi':31 'javascript':68 'knex':62 'last':14 'librari':55 'lightweight':5 'like':21,56 'liter':32 'maintain':67 'mainten':48 'may':63 'mode':49 'node':44 'node-postgr':43 'node.js':12 'parameter':36 'pg':2,58,69 'pg-promis':57 'pg-query-formatt':1 'postgr':45,73 'postgresql':10 'printf':20 'printf-lik':19 'promis':59 'provid':17 'queri':3,6,27,37,72 'safe':24 'sinc':52 'sql':26 'squel':60 'string':7 'subqueri':34 'support':35 'syntax':22 'toparam':39 'updat':15,51 'use':41 'v0.0.5':13 'via':38","created_at":"2026-06-04T18:53:44.532306+00:00","updated_at":"2026-06-04T18:53:44.532306+00:00","problems":[{"fix":"Use const Query = require('pg-query-formatter'); and then new Query(...);","cause":"Importing with import statement or forgetting 'new'","error":"TypeError: Query is not a constructor"},{"fix":"Ensure you use new Query(fmt, ...).","cause":"Calling .toString() on a Query instance that was not instantiated with new.","error":"Cannot read property 'toString' of undefined"},{"fix":"Pass a string format as first argument: new Query('SELECT %L', value);","cause":"First argument to new Query() is not a string.","error":"TypeError: fmt.replace is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/naturalatlas/pg-query-formatter","github":"git://github.com/naturalatlas/pg-query-formatter","docs":null,"changelog":null,"pypi":null,"npm":"pg-query-formatter","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-04","next_check":"2026-09-02","install_tag":null}}