Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

x/postgres/mod.ts>Pool

PostgreSQL driver for Deno
Extremely Popular
Latest
class Pool
import { Pool } from "https://deno.land/x/postgres@v0.19.3/mod.ts";

Connection pools are a powerful resource to execute parallel queries and save up time in connection initialization. It is highly recommended that all applications that require concurrent access use a pool to communicate with their PostgreSQL database

import { Pool } from "https://deno.land/x/postgres/mod.ts";
const pool = new Pool({
  database: "database",
  hostname: "hostname",
  password: "password",
  port: 5432,
  user: "user",
}, 10); // Creates a pool with 10 available connections

const client = await pool.connect();
await client.queryArray`SELECT 1`;
client.release();

You can also opt to not initialize all your connections at once by passing the lazy option when instantiating your pool, this is useful to reduce startup time. In addition to this, the pool won't start the connection unless there isn't any already available connections in the pool

import { Pool } from "https://deno.land/x/postgres/mod.ts";
// Creates a pool with 10 max available connections
// Connection with the database won't be established until the user requires it
const pool = new Pool({}, 10, true);

// Connection is created here, will be available from now on
const client_1 = await pool.connect();
await client_1.queryArray`SELECT 1`;
client_1.release();

// Same connection as before, will be reused instead of starting a new one
const client_2 = await pool.connect();
await client_2.queryArray`SELECT 1`;

// New connection, since previous one is still in use
// There will be two open connections available from now on
const client_3 = await pool.connect();
client_2.release();
client_3.release();

Constructors

new
Pool(
connection_params: ClientOptions | ConnectionString | undefined,
size: number,
lazy?: boolean,
)

A class that manages connection pooling for PostgreSQL clients

Properties

readonly
available: number

The number of open connections available for use

Lazily initialized pools won't have any open connections by default

readonly
size: number

The number of total connections open in the pool

Both available and in use connections will be counted

Methods

connect(): Promise<PoolClient>

This will return a new client from the available connections in the pool

In the case of lazy initialized pools, a new connection will be established with the database if no other connections are available

import { Pool } from "https://deno.land/x/postgres/mod.ts";
const pool = new Pool({}, 10);
const client = await pool.connect();
await client.queryArray`UPDATE MY_TABLE SET X = 1`;
client.release();
end(): Promise<void>

This will close all open connections and set a terminated status in the pool

import { Pool } from "https://deno.land/x/postgres/mod.ts";
const pool = new Pool({}, 10);

await pool.end();
console.assert(pool.available === 0, "There are connections available after ending the pool");
await pool.end(); // An exception will be thrown, pool doesn't have any connections to close

However, a terminated pool can be reused by using the "connect" method, which will reinitialize the connections according to the original configuration of the pool

import { Pool } from "https://deno.land/x/postgres/mod.ts";
const pool = new Pool({}, 10);
await pool.end();
const client = await pool.connect();
await client.queryArray`SELECT 1`; // Works!
client.release();
initialized(): Promise<number>

This will return the number of initialized clients in the pool