# postgresql-service

> A simple wrapper around `node-pg`

Latest version **11.0.1** (published 2026-04-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install postgresql-service
pnpm add postgresql-service
yarn add postgresql-service
bun add postgresql-service
```

## Health

**Score 60/100 (C)** — status: active.

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

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 11.0.1 |
| Published | 2026-04-17 |
| First published | 2018-09-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=24.14.0 |
| Dependencies | 6 |
| Unpacked size | 44.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Nicolas Froidure |
| Maintainers | nfroidure |
| Keywords | pg, knifecycle, service |

## Links

- npm: https://www.npmjs.com/package/postgresql-service
- Repository: https://github.com/nfroidure/postgresql-service
- Homepage: https://github.com/nfroidure/postgresql-service#readme
- Issues: https://github.com/nfroidure/postgresql-service/issues
- Funding: https://github.com/sponsors/nfroidure
- npm.io page: https://npm.io/package/postgresql-service

## Dependencies (6)

- [pg](https://npm.io/package/pg.md) ^8.20.0
- [yerror](https://npm.io/package/yerror.md) ^11.0.0
- [@types/pg](https://npm.io/package/@types/pg.md) ^8.20.0
- [knifecycle](https://npm.io/package/knifecycle.md) ^21.1.0
- [common-services](https://npm.io/package/common-services.md) ^20.0.0
- [pg-connection-string](https://npm.io/package/pg-connection-string.md) ^2.12.0

## Recent versions

- 11.0.1 (latest) — 2026-04-17
- 2.0.0-beta.5 (beta) — 2020-08-19
- 11.0.0 — 2026-04-07
- 10.1.0 — 2026-04-01
- 10.0.0 — 2026-03-27
- 9.0.1 — 2025-11-06
- 9.0.0 — 2024-12-04
- 8.0.5 — 2024-07-16
- 8.0.4 — 2024-05-28
- 8.0.3 — 2023-11-09
- 8.0.2 — 2023-08-20
- 8.0.1 — 2023-08-17
- 8.0.0 — 2023-08-17
- 7.0.0 — 2023-08-12
- 6.0.4 — 2023-01-05
- … 25 more at https://npm.io/package/postgresql-service/versions

## README

[//]: # ( )
[//]: # (This file is automatically generated by a `metapak`)
[//]: # (module. Do not change it  except between the)
[//]: # (`content:start/end` flags, your changes would)
[//]: # (be overridden.)
[//]: # ( )
# postgresql-service
> A simple wrapper around `node-pg`

[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/nfroidure/postgresql-service/blob/main/LICENSE)


[//]: # (::contents:start)

This simple service covers my own usage of the `pg` module. I only use
transactions and queries and I use dependency injection with
[Knifecycle](https://github.com/nfroidure/knifecycle).

It also sets up a few tweaks I have to do for each projects like avoiding to
mess up with dates.

## Tagged templates

You may like to use [pgsqwell](https://github.com/nfroidure/pgsqwell) with this
module.

[//]: # (::contents:end)

# API
<a name="initPGService"></a>

## initPGService(services) ⇒ <code>Promise.&lt;Object&gt;</code>
Instantiate the pg service

**Kind**: global function  
**Returns**: <code>Promise.&lt;Object&gt;</code> - A promise of the pg service  

| Param | Type | Description |
| --- | --- | --- |
| services | <code>Object</code> | The services to inject |
| [services.log] | <code>function</code> | A logging function |
| [services.PG_URL_ENV_NAME] | <code>Object</code> | The environment variable name in which to pick-up the  PG url |
| [services.ENV] | <code>Object</code> | An environment object |
| services.PG | <code>Object</code> | A `pg` compatible configuration object |

**Example**  
```js
import initPGService from 'postgresql-service';

const { service: pg, dispose } = await initPGService({
  log: console.log.bind(console),
  ENV: process.env, // Proxy the PG_URL env var
});

const result = pg.query('SELECT 1');

await dispose();
```

* [initPGService(services)](#initPGService) ⇒ <code>Promise.&lt;Object&gt;</code>
    * [~query()](#initPGService..query) ⇒ <code>String</code> \| <code>Object</code>
    * [~queries()](#initPGService..queries) ⇒ <code>Array.&lt;String&gt;</code> \| <code>Object</code>
    * [~transaction()](#initPGService..transaction) ⇒ <code>Array.&lt;String&gt;</code> \| <code>Object</code>

<a name="initPGService..query"></a>

### initPGService~query() ⇒ <code>String</code> \| <code>Object</code>
Executes the given query

**Kind**: inner method of [<code>initPGService</code>](#initPGService)  
**Returns**: <code>String</code> - Query to execute<code>Object</code> - Arguments hash for the query  
**Example**  
```js
const { rows, fields } = await pg.query(
   'SELECT * FROM users WHERE user = $$userId',
   { userId: 1 }
);
```
<a name="initPGService..queries"></a>

### initPGService~queries() ⇒ <code>Array.&lt;String&gt;</code> \| <code>Object</code>
Executes the given queries in parallel (using the connections pool)

**Kind**: inner method of [<code>initPGService</code>](#initPGService)  
**Returns**: <code>Array.&lt;String&gt;</code> - Queries to execute<code>Object</code> - Arguments hashes for the queries  
**Example**  
```js
const [{ rows, fields }, { rows2, fields2 }] = await pg.queries([
   'SELECT * FROM users WHERE user = $$userId',
   'SELECT * FROM users WHERE user = $$userId',
], { userId: 1 });
```
<a name="initPGService..transaction"></a>

### initPGService~transaction() ⇒ <code>Array.&lt;String&gt;</code> \| <code>Object</code>
Executes the given queries in a single transaction

**Kind**: inner method of [<code>initPGService</code>](#initPGService)  
**Returns**: <code>Array.&lt;String&gt;</code> - Queries to execute<code>Object</code> - Arguments hashes for the queries  
**Example**  
```js
const [, { rows, fields }] = await pg.transaction([
   'UPDATE users SET isActive = true WHERE user = $$userId',
   'SELECT * FROM users WHERE user = $$userId',
], { userId: 1 });
```

# Authors
- [Nicolas Froidure](http://insertafter.com/en/index.html)

# License
[MIT](https://github.com/nfroidure/postgresql-service/blob/main/LICENSE)

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