@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.
createAgentSessionRuntime(), see SDK storage backends.
Installation
Install according to how you use Atomic:@azure/identity, @aws-sdk/rds-signer, or google-auth-library).
SDK quick start
Create your database roles and runatomic storage migrate as the owner role first (see Set up the database), then connect from your Node.js application with openPostgresDatabase():
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-backedSqliteDatabase.- 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, setauth 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
urlomitssslmode, Atomic appliessslmode=verify-fullfor remote hosts (checkingPGHOSTif omitted from the URL). Addsslrootcert=/path/to/ca.pemfor private CAs. Withoutsslrootcert,requireverifies both certificate and hostname likeverify-full(stricter than libpq). TLS parameter names are case-sensitive; unknownsslmodevalues are rejected. - HTTP
CONNECTproxies: Withoutoptions.proxy, remote TCP hosts checkHTTPS_PROXY(https_proxy), thenALL_PROXY(when HTTP/HTTPS), thenHTTP_PROXY, respectingNO_PROXY. Loopback hosts and Unix socket directories connect directly. Keepsslmode=verify-full(orverify-cawith 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+ withmax_prepared_statements > 0, it keeps prepared statements enabled (up to256); behind transaction poolers that do not support prepared statements (on any port), it disables them automatically. SetmaxPreparedStatementsonly to override auto-detection or change the per-connection cap. A process that listens for wake-ups (such as an Intercom broker) opens one extraLISTENconnection outside its pools; whenurluses a transaction pooler, setlistenUrlto 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) throwStorageTransientError(read-only and unwritten calls retry automatically). Invalid credentials (28P01), missing databases (3D000), and certificate errors fail immediately. Missing privileges (42501) fail immediately withStorageConfigError(reason: "missing-privilege") naming the exactGRANTor migration command.
CLI configuration
Join a machine with the CLI after installing the adapter in user scope and runningatomic 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
Related docs
- PostgreSQL operator guide: role creation SQL, migrations, cloud provider setup, and troubleshooting.
- SDK storage backends: complete
createAgentSession(),createAgentSessionRuntime(), and error-handling recipes. - Standalone libraries: all published
@bastani/atomic-*packages.