# pg-minify

> Minifies PostgreSQL scripts.

Latest version **1.8.0** (published 2025-07-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install pg-minify
pnpm add pg-minify
yarn add pg-minify
bun add pg-minify
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.8.0 |
| Published | 2025-07-06 |
| First published | 2016-01-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=16.0.0 |
| Dependencies | 0 |
| Unpacked size | 14.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 53 |
| Author | Vitaly Tomilov |
| Maintainers | vitaly.tomilov |
| Keywords | sql, postgresql, comments, minify, uglify, compress, strip, remove |

## Links

- npm: https://www.npmjs.com/package/pg-minify
- Repository: https://github.com/vitaly-t/pg-minify
- Issues: https://github.com/vitaly-t/pg-minify/issues
- npm.io page: https://npm.io/package/pg-minify

## Alternatives

- [lodash.startswith](https://npm.io/package/lodash.startswith.md) — 769.7K weekly downloads
- [@tarojs/service](https://npm.io/package/@tarojs/service.md) — 33.9K weekly downloads
- [io.extendreality.tilia.indicators.spatialtargets.unity](https://npm.io/package/io.extendreality.tilia.indicators.spatialtargets.unity.md) — 131 weekly downloads
- [@rtarojs/taro](https://npm.io/package/@rtarojs/taro.md) — 90 weekly downloads
- [node-branch-io](https://npm.io/package/node-branch-io.md) — 50 weekly downloads

## Recent versions

- 1.8.0 (latest) — 2025-07-06
- 0.0.1 (beta) — 2016-01-19
- 1.7.0 — 2025-03-09
- 1.6.5 — 2024-07-14
- 1.6.4 — 2024-04-06
- 1.6.3 — 2023-03-18
- 1.6.2 — 2020-12-20
- 1.6.1 — 2020-05-27
- 1.6.0 — 2020-05-09
- 1.5.2 — 2020-02-02
- 1.5.1 — 2019-09-08
- 1.5.0 — 2019-08-23
- 1.4.1 — 2019-05-15
- 1.4.0 — 2019-05-15
- 1.3.0 — 2019-05-14
- … 41 more at https://npm.io/package/pg-minify/versions

## README

pg-minify
=========

[![Build Status](https://github.com/vitaly-t/pg-minify/actions/workflows/ci.yml/badge.svg)](https://github.com/vitaly-t/pg-minify/actions/workflows/ci.yml)
[![Node Version](https://img.shields.io/badge/nodejs-16%20--%2024-green.svg?logo=node.js&style=flat)](https://nodejs.org)

Minifies PostgreSQL scripts, reducing the IO usage.

**Features:**

* 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 

## Installing

```
$ npm install pg-minify
```

## Usage

```js
const minify = require('pg-minify');

const sql = 'SELECT 1; -- comments';

minify(sql); //=> SELECT 1;
```

with compression (removes all unnecessary spaces):

```js
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:

```js
try {
    minify('SELECT \'1');
} catch (error) {
    // error is minify.SQLParsingError instance
    // error.message:
    // Error parsing SQL at {line:1,col:8}: Unclosed text block.
}
```

## API

### 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.

## Limitations

Double-dollar `$$` string escaping, which avoids escaping single quotes, is not supported.
See [#12](https://github.com/vitaly-t/pg-minify/issues/12).

## License

Copyright © 2025 [Vitaly Tomilov](https://github.com/vitaly-t);
Released under the MIT license.

[SQLParsingError]:https://github.com/vitaly-t/pg-minify/blob/master/lib/error.js#L22
[TypeScript]:https://github.com/vitaly-t/pg-minify/tree/master/typescript
[SQL Compression]:https://github.com/vitaly-t/pg-minify/wiki/SQL-Compression

---
_Source: https://npm.io/package/pg-minify · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
