Skip to content
Minifies PostgreSQL scripts.
JavaScript TypeScript
Branch: master
Clone or download
Latest commit dbc0f44 Nov 16, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib retiring strict mode Sep 8, 2019
typescript docs Sep 8, 2019
.eslintrc.json improving linting May 15, 2019
.gitignore dependencies update Oct 19, 2018
.travis.yml updating all TypeScript stuff Aug 23, 2019 updating all TypeScript stuff Aug 23, 2019
package.json DEV deps updated Nov 16, 2019


Minifies PostgreSQL scripts, reducing the IO usage.

Build Status Coverage Status Join the chat at


  • Removes /*multi-line*/ (including nested) and --single-line comments
  • Preserves special/copyright multi-line comments that start with /*!
  • Concatenates multi-line strings into a single line with \n
  • Fixes multi-line text, prefixing it with E where needed
  • Removes redundant line gaps: line breaks, tabs and spaces
  • Provides basic parsing and error detection for invalid SQL
  • Flattens the resulting script into a single line
  • Optionally, compresses SQL for minimum space


$ npm install pg-minify


const minify = require('pg-minify');

const sql = 'SELECT 1; -- comments';

minify(sql); //=> SELECT 1;

with compression (removes all unnecessary spaces):

const sql = 'SELECT * FROM "table" WHERE col = 123; -- comments';

minify(sql, {compress: true});
//=> SELECT*FROM"table"WHERE col=123;

The library's distribution includes TypeScript declarations.

Error Handling

SQLParsingError is thrown on failed SQL parsing:

try {
    minify('SELECT \'1');
} catch (error) {
    // error is minify.SQLParsingError instance
    // error.message:
    // Error parsing SQL at {line:1,col:8}: Unclosed text block.


minify(sql, [options]) ⇒ String

Minifies SQL into a single line, according to the options.

options.compress ⇒ Boolean

Compresses / uglifies the SQL to its bare minimum, by removing all unnecessary spaces.

  • false (default) - keeps minimum spaces, for easier read
  • true - removes all unnecessary spaces

See also: SQL Compression.

options.removeAll ⇒ Boolean

Removes everything, i.e. special/copyright multi-line comments that start with /*! will be removed as well.


First, clone the repository and install DEV dependencies.

$ npm test

Testing with coverage:

$ npm run coverage


Copyright © 2019 Vitaly Tomilov; Released under the MIT license.

You can’t perform that action at this time.