Registry / testing / prettier-plugin-postgresql

prettier-plugin-postgresql

JSON →
library0.2.3jsnpmunverified

A Prettier plugin that uses the official libpg_query parser to format PostgreSQL SQL. Currently in beta (v0.2.3) with support for core DML and DDL constructs including SELECT, INSERT, UPDATE, DELETE, CTEs, window functions, CREATE TABLE, ALTER TABLE, indexes, views, functions, triggers, and more. Compared to generic SQL formatters, this plugin uses PostgreSQL's own parser for accurate syntax understanding and respects optional parentheses in WHERE clauses (default: minimal). Release cadence is monthly. Node >=20 and Prettier >=3 required. Ships TypeScript types.

npm install prettier-plugin-postgresql
INSTALL
IMPORT
SIG · PRETTIER-PLUGIN-PO
P
prettier-plugin-postgresql
testingjavascriptv0.2.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
// In .prettierrc.yaml: plugins: - prettier-plugin-postgresql
require('prettier-plugin-postgresql')
This is a Prettier plugin, not a programmatic API. Configure in .prettierrc or pass --plugin. No JavaScript import needed.
Configuration
// .prettierrc { "plugins": ["prettier-plugin-postgresql"], "pgFunctionNameCase": "lowercase", "pgHavingClauseOnNewLine": true }
Options: pgFunctionNameCase (lowercase|uppercase|none), pgHavingClauseOnNewLine, pgWhereClauseOnNewLine, pgIndentWidth. See documentation.
TypeScript usage
// tsconfig.json or prettier.config.ts import type { Config } from 'prettier'; const config: Config = { plugins: ['prettier-plugin-postgresql'] }; export default config;
const config = require('prettier-plugin-postgresql');
TypeScript types are shipped, but the plugin itself is used via config only. No import statement for the plugin itself.

Install, configure, and run prettier on a SQL file to format PostgreSQL queries.

// 1. Install // npm install --save-dev prettier prettier-plugin-postgresql // 2. Configure .prettierrc { "plugins": ["prettier-plugin-postgresql"], "parser": "sql", "pgFunctionNameCase": "lowercase", "pgIndentWidth": 2 } // 3. Format a SQL file // echo "SELECT a, b FROM t WHERE c > 1" > query.sql // npx prettier --write query.sql // Example output: // SELECT // a, // b // FROM // t // WHERE // c > 1
Debug
Known issues
breakingThis plugin is in beta – some advanced constructs may be formatted as /* unknown */ placeholder comments, causing SQL to become invalid if executed without review.
fix
Review formatted output; file issues for unknown constructs; consider using a different formatter for unsupported SQL.
affects: >=0.0.0
breakingParser misalignment: libpg_query is based on PostgreSQL 16, so syntax from PostgreSQL 17+ might not parse correctly.
fix
Ensure your SQL uses PostgreSQL 16 compatible syntax; upgrade plugin when libpg_query updates.
affects: >=0.0.0
deprecatedOptions pgFunctionNameCase default changed from 'upper' to 'lowercase' in v0.2.0 for consistency with PostgreSQL convention.
fix
Set pgFunctionNameCase: 'uppercase' explicitly if you want uppercase function names.
affects: <0.2.0
gotchaThe parser is strict PostgreSQL – non-standard SQL (e.g., SQLite, MySQL) will throw parsing errors.
fix
Only use this plugin for PostgreSQL SQL files; use file-based parser overrides in Prettier.
affects: >=0.0.0
gotchaPlugin must be added to the plugins array in Prettier config; it does not auto-detect SQL files without explicit parser setting.
fix
Set parser: 'sql' in Prettier config, or use file extensions like .sql mapped via overrides.
affects: >=0.0.0
gotchaWhitespace and indentation behavior may change between minor versions during beta – pin exact version.
fix
Pin to exact version: "prettier-plugin-postgresql": "0.2.3" in package.json.
affects: >=0.0.0
Errors
Common errors & fixes
[error] Invalid configuration for file "query.sql". Unexpected parser "sql" specified.
Prettier does not know the 'sql' parser without the plugin loaded.
fix
Add the plugin to your Prettier config: { "plugins": ["prettier-plugin-postgresql"] }
Error: Cannot find module 'prettier-plugin-postgresql'
Plugin not installed or not found in node_modules.
fix
Run: npm install --save-dev prettier-plugin-postgresql (ensure prettier is also installed)
TypeError: prettier.resolveConfig.sync is not a function
Using the plugin with Prettier v2 (requires Prettier >=3).
fix
Upgrade Prettier to v3: npm install --save-dev prettier@latest
sql:9:3 - Error: syntax error at or near "/* unknown */"
A construct in your SQL is not supported by the plugin and was replaced with a placeholder.
fix
Edit the output to replace the unknown placeholder with valid SQL or file an issue on GitHub.
Upgrade
Version history
0.2.3latest on npm
Audit
Dependencies
prettierrequiredpeer dependency – requires Prettier >=3.0.0 to function as a plugin
Agent activity
10 hits · last 30 days
node
10
Resources
prettier-plugin-postgresql — npm install prettier-plugin-postgresql · libregistry