npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

pg2s-baileys

v1.0.1

Published

Lightweight Baileys auth state adapter

Readme

Lightweight Baileys auth state adapter using PostgreSQL and NodeCache
Built for efficient, persistent, and cache-optimized WhatsApp authentication sessions with multi-instance support.

Table of Contents

Installation

Install the package using your preferred JavaScript package manager:

npm

npm install pg2s-baileys

pnpm

pnpm add pg2s-baileys

yarn

yarn add pg2s-baileys

bun

bun add pg2s-baileys

Dependencies

  • Requires pg (PostgreSQL client) and node-cache (installed automatically).
  • Ensure you have a running PostgreSQL server (version 12+ recommended).

Usage

Single Instance

import { makeWASocket, DisconnectReason, proto, initAuthCreds, BufferJSON } from '@whiskeysockets/baileys';
// or
// import { makeWASocket, DisconnectReason, proto, initAuthCreds, BufferJSON } from 'baileys';
import usePostgresAuthState from 'pg2s-baileys';

const { state, saveCreds, resetSession } = await usePostgresAuthState(
  { connectionString: 'postgres://user:pass@localhost:5432/mydb' },  // PostgreSQL connection options (object)
  '6281234567890',  // phoneNumber (string) for multi-instance support
  { proto, initAuthCreds, BufferJSON }  // Baileys utils
);

// Initialize WhatsApp socket
const sock = makeWASocket({
  auth: state,
});

// Save credentials on update
sock.ev.on('creds.update', saveCreds);

// Example: Reset session
sock.ev.on('connection.update', async (update) => {
  const { lastDisconnect, connection } = update;
  if (connection === "close") {
    const statusCode = lastDisconnect?.error?.output?.statusCode;
    if (statusCode === DisconnectReason.loggedOut) {
      await resetSession();
    }
  }
});

Multi-Instance (Multiple Sessions)

For handling multiple WhatsApp accounts in the same app (e.g., different phone numbers), call usePostgresAuthState multiple times with different phoneNumber values. All data is stored in a shared PostgreSQL table (auth_state), scoped by phone_number.

import { makeWASocket, DisconnectReason, proto, initAuthCreds, BufferJSON } from '@whiskeysockets/baileys';
import usePostgresAuthState from 'pg2s-baileys';

const connOptions = { connectionString: 'postgres://user:pass@localhost:5432/mydb' };
const baileysUtils = { proto, initAuthCreds, BufferJSON };

// Instance 1: Phone A
const { state: state1, saveCreds: saveCreds1, resetSession: resetSession1 } = await usePostgresAuthState(
  { connectionString: 'postgres://user:pass@localhost:5432/mydb' },
  '6281234567890',
  { proto, initAuthCreds, BufferJSON }
);
const sock1 = makeWASocket({ auth: state1 });
sock1.ev.on('creds.update', saveCreds1);
sock1.ev.on('connection.update', async (update) => {
  if (update.connection === 'close' && update.lastDisconnect?.error?.output?.statusCode === DisconnectReason.loggedOut) {
    await resetSession1();
  }
});

// Instance 2: Phone B
const { state: state2, saveCreds: saveCreds2, resetSession: resetSession2 } = await usePostgresAuthState(
  { connectionString: 'postgres://user:pass@localhost:5432/mydb' },
  '6289876543210',
  { proto, initAuthCreds, BufferJSON }
);
const sock2 = makeWASocket({ auth: state2 });
sock2.ev.on('creds.update', saveCreds2);
sock2.ev.on('connection.update', async (update) => {
  if (update.connection === 'close' && update.lastDisconnect?.error?.output?.statusCode === DisconnectReason.loggedOut) {
    await resetSession2();
  }
});

// Now manage sock1 and sock2 independently (e.g., in an array or map)

Description

pg2s-baileys is a drop-in replacement for Baileys' default auth state management. It replaces file-based JSON storage with PostgreSQL for scalable persistence (using connection pooling via pg.Pool) and NodeCache for fast in-memory access (no auto-TTL for auth data).

Key Benefits:

  • Efficiency: Reduces I/O with caching and async queries.
  • Scalability: Supports multi-instance via phoneNumber parameter; ideal for high-traffic bots handling multiple WhatsApp accounts.
  • Persistence: All auth data (creds, keys, signals) in a single PostgreSQL table with BIGINT phone_number for scoping.
  • Lightweight: Minimal overhead, with manual UPSERT for writes and error handling.

Ideal for production WhatsApp bots with multiple sessions. Use a single database for all instances.

Arguments:

  • connOptions (object): PostgreSQL config, e.g., { connectionString: 'postgres://...' } or full { host, port, user, password, database }.
  • phoneNumber (string): Numeric phone number (e.g., "6281234567890") for scoping auth data; required for multi-instance.
  • baileysUtils (object): { proto, initAuthCreds, BufferJSON } from Baileys. Validates inputs for errors.

Error Handling: Throws descriptive errors for invalid inputs, DB failures, or missing dependencies. Logs errors to console for reads/writes. Ensures numeric phoneNumber conversion to BIGINT.

Database Schema (Auto-Created):

CREATE TABLE IF NOT EXISTS auth_state (
  id SERIAL PRIMARY KEY,
  phone_number BIGINT NOT NULL,
  key TEXT NOT NULL,
  value TEXT
);

License

This project is licensed under the MIT License - see the LICENSE file for details.

Developed with ❤️ by Brother of Ijul.