Registry / devops / psqlformat

psqlformat

JSON →
library1.33.0jsnpmunverified

PostgreSQL SQL syntax beautifier wrapping the Perl-based pgFormatter. v1.33.0 (released 2023-01-15) provides a CLI and JavaScript API for formatting SQL files, with options for indentation, keyword casing, comma placement, and comment removal. Key differentiators: specifically targets PostgreSQL dialect, supports extensive formatting rules (keywordCase, functionCase, typeCase), and offers both library and CLI usage. Requires Perl runtime. Actively maintained with monthly releases.

npm install psqlformat
INSTALL
IMPORT
SIG · PSQLFORMAT
P
psqlformat
devopsjavascriptv1.33.0
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
✓ import psqlformat from 'psqlformat'
✗ const psqlformat = require('psqlformat')
Package ships ES module by default (since v1.0.0). CJS require() will work if using bundler that handles ESM, but direct require may fail.
format
✓ import { format } from 'psqlformat'
✗ const { format } = require('psqlformat')
Named export for formatting a single SQL string. Only available in ESM environment.
formatFromFile
✓ import { formatFromFile } from 'psqlformat'
Reads a file and formats its SQL content. Returns a Promise<string>.

Demonstrates formatting a SQL string with custom options (2 spaces, lowercase keywords) using the ESM module.

import { format } from 'psqlformat'; const sql = process.env.SQL ?? 'SELECT id, name FROM users WHERE id = 1'; const formatted = await format(sql, { spaces: 2, keywordCase: 'lowercase' }); console.log(formatted); // Output: // select id, // name // from users // where id = 1
Debug
Known issues
gotchaRequires Perl to be installed and available on PATH. Without Perl, formatting operations will fail with a spawn error.
fix
Install Perl via your package manager (e.g., 'sudo apt-get install perl' on Ubuntu, or 'brew install perl' on macOS).
affects: >=0.1.0
breakingIn version 1.0.0, the package switched from CommonJS to ESM-only. Calling require('psqlformat') will throw ERR_REQUIRE_ESM.
fix
Use dynamic import: const psqlformat = await import('psqlformat'); or import statement in an ESM context.
affects: >=1.0.0
deprecatedThe CLI option '--commaStart' is deprecated in favor of '--commaEnd' with false. It will be removed in a future version.
fix
Use '--commaEnd=false' instead.
affects: >=1.15.0
gotchaFormatting large files may block the event loop because the underlying Perl process is synchronous. Consider using formatFromFile with streaming for large files.
fix
For large files, use formatFromFile which reads the file in chunks.
affects: >=0.1.0
Errors
Common errors & fixes
Error: spawn perl ENOENT
Perl is not installed or not on PATH.
fix
Install Perl (e.g., sudo apt-get install perl) and ensure it's accessible.
ERR_REQUIRE_ESM
Using require() on an ES module.
fix
Use import instead of require, or convert your project to ESM.
Upgrade
Version history
1.33.0latest on npm
Audit
Dependencies
perloptionalThe underlying pgFormatter tool is written in Perl and must be installed on the system.
Agent activity
4 hits · last 30 days
node
4
Resources
psqlformat — npm install psqlformat · libregistry