Skip to main content
@bastani/atomic-storage-postgres is Atomic’s optional standalone PostgreSQL storage adapter. It stores sessions and Intercom multi-node routing in one shared PostgreSQL database so multiple machines or SDK hosts can share state. Unlike the six core libraries in Standalone libraries, this package is not bundled inside @bastani/atomic; install it only when you use PostgreSQL.
  • PostgreSQL version requirement: Atomic needs PostgreSQL 16 or newer.
  • Plain ESM with bundled types: Ships plain JavaScript ESM with TypeScript declarations and requires Node.js 24.20+ with no TypeScript loader.
For operator setup, role creation SQL, migrations, and troubleshooting, see PostgreSQL setup. For all four SDK storage backends and createAgentSessionRuntime(), see SDK storage backends.

Installation

Install according to how you use Atomic:
If you use cloud IAM authentication, also install the matching optional peer dependency for your cloud provider (@azure/identity, @aws-sdk/rds-signer, or google-auth-library).

SDK quick start

Create your database roles and run atomic storage migrate as the owner role first (see Set up the database), then connect from your Node.js application with openPostgresDatabase():
Pass the returned SqliteDatabase to createAgentSession({ sessionStore: { kind: "database", database, prefix? } }) or SqlSessionStore.open(database, { prefix? }). Atomic leaves caller-owned database handles open on session.dispose(), so always close database in your outer finally.

PostgresDatabaseOptions

Exported API

  • openPostgresDatabase(options: PostgresDatabaseOptions): Promise<SqliteDatabase> — opens a pooled PostgreSQL-backed SqliteDatabase.
  • Default constants: DEFAULT_MAX_CONNECTIONS (8), DEFAULT_READER_MAX_CONNECTIONS (2), DEFAULT_STATEMENT_TIMEOUT_MS (30000), DEFAULT_CONNECT_TIMEOUT_MS (10000), DEFAULT_IDLE_TIMEOUT_MS (300000), SURGE_IDLE_TIMEOUT_MS (10000), DEFAULT_KEEPALIVE_IDLE_MS (60000), DEFAULT_MAX_PREPARED_STATEMENTS (256), DEFAULT_SCHEMA ("atomic").
  • Types: PostgresDatabaseOptions, PostgresSocketFactory, ProxyTarget.
  • Default export: StorageAdapterModule ({ atomicStorageAdapter: 1, liveSecrets, openDatabase }) used by Atomic CLI settings and { kind: "adapter" } selections.
Low-level SQL translation helpers: The package also exports numberPlaceholders(sql) (rewrites positional ? parameters outside literals, quoted identifiers, and comments to $1, $2, …) and physicalIdentifiers(sql) (replaces unquoted identifiers longer than 63 bytes with deterministic PostgreSQL physical names). Ordinary applications do not need to call these directly—they are provided for custom adapters or tooling translating Atomic SQLite queries to PostgreSQL.

Cloud IAM authentication

On Azure, AWS, and Google Cloud, set auth and omit password / secrets.password. Only the selected provider’s SDK loads on first connection. Tokens stay in memory, never appear in settings or errors, time out after 8 seconds, and clear and retry once on rejection (28P01, or Azure 28000). Missing SDKs or token failures throw StorageConfigError (reason: "auth-unavailable"). Cloud tokens require TLS (sslmode=verify-full or require). For Azure Database for PostgreSQL Flexible Server (PostgreSQL 16+), Azure HorizonDB (PostgreSQL 17, read/write endpoint), Amazon RDS/Aurora, and Google Cloud SQL role-grant SQL, see Cloud identities.

TLS, HTTP proxies, and connection poolers

  • TLS defaults: When url omits sslmode, Atomic applies sslmode=verify-full for remote hosts (checking PGHOST if omitted from the URL). Add sslrootcert=/path/to/ca.pem for private CAs. Without sslrootcert, require verifies both certificate and hostname like verify-full (stricter than libpq). TLS parameter names are case-sensitive; unknown sslmode values are rejected.
  • HTTP CONNECT proxies: Without options.proxy, remote TCP hosts check HTTPS_PROXY (https_proxy), then ALL_PROXY (when HTTP/HTTPS), then HTTP_PROXY, respecting NO_PROXY. Loopback hosts and Unix socket directories connect directly. Keep sslmode=verify-full (or verify-ca with a private CA) so TLS runs end to end through the tunnel.
  • Connection poolers and wake-ups: Works with transaction-mode poolers (PgBouncer, Azure port 6432, Supabase Supavisor, Neon -pooler) with no session-level state. Atomic probes the first connection automatically: on PgBouncer 1.21+ with max_prepared_statements > 0, it keeps prepared statements enabled (up to 256); behind transaction poolers that do not support prepared statements (on any port), it disables them automatically. Set maxPreparedStatements only to override auto-detection or change the per-connection cap. A process that listens for wake-ups (such as an Intercom broker) opens one extra LISTEN connection outside its pools; when url uses a transaction pooler, set listenUrl to a direct endpoint or Atomic falls back to polling. See the PgBouncer guide.
  • Retries and errors: Connection drops, lock conflicts (40001, 40P01, 55P03), and canceled statements (57014) throw StorageTransientError (read-only and unwritten calls retry automatically). Invalid credentials (28P01), missing databases (3D000), and certificate errors fail immediately. Missing privileges (42501) fail immediately with StorageConfigError (reason: "missing-privilege") naming the exact GRANT or migration command.

CLI configuration

Join a machine with the CLI after installing the adapter in user scope and running atomic storage migrate:
1

Write the runtime password to a mode-0600 file

2

Join the shared database and remove the temporary file

3

Restart Intercom and verify connectivity