@bastani/atomic-storage-postgres
v0.10.17
Published
PostgreSQL storage adapter for Atomic sessions and Intercom.
Maintainers
Readme
@bastani/atomic-storage-postgres
A PostgreSQL storage adapter for Atomic. With it, several machines keep their sessions and Intercom messages in one shared PostgreSQL database, so a team, a CI fleet, or one person on several computers sees the same sessions and can message agents on other machines. Install it only when you want PostgreSQL storage. The full guide is PostgreSQL setup.
Install
Choose the installation method that matches how you use the adapter:
- CLI,
settings.json,ATOMIC_STORAGE, and SDK{ kind: "adapter" }selections. Install the adapter in user scope (without-l). Atomic searches user-scope packages, host extension directories, and the agent directory for adapter modules, not an embedding application'snode_modules:
atomic install npm:@bastani/atomic-storage-postgres- Node.js and TypeScript SDK apps (
openPostgresDatabase()with{ kind: "database", database }). Install the package directly in your project alongside@bastani/atomic:
npm install @bastani/atomic-storage-postgresConfigure
Atomic needs PostgreSQL 16 or newer.
Join with the CLI
After creating your database and roles (see Set up the database), run atomic storage migrate as the owner role to create or upgrade the schema, then join each machine as the runtime role:
(umask 077; printf 'Runtime password: '; read -rs password; printf '\n'; printf '%s' "$password" > ~/atomic-db-password)
atomic storage join 'postgresql://[email protected]:5432/atomic' --password-file ~/atomic-db-password
rm ~/atomic-db-password # atomic storage join keeps its own copy
atomic intercom restart
atomic storage statusatomic storage join verifies connectivity with SELECT 1 before writing anything, then updates the storage block in ~/.atomic/agent/settings.json. Pass one credential flag:
--password-file <path>: Trims surrounding whitespace and copies the password into a private mode-600file under~/.atomic/agent/.--token-file <path>: References an external rotated password file in place and rereads it on every new connection, stripping only one trailing\nor\r\n(avoid extra trailing spaces or blank lines).--auth azure|aws|gcp: Authenticates using a cloud identity instead of a password.
Configure settings manually
You can also edit ~/.atomic/agent/settings.json directly:
{
"storage": {
"adapter": "@bastani/atomic-storage-postgres",
"options": { "url": "postgresql://[email protected]:5432/atomic" },
"secrets": { "password": { "file": "postgres-password" } }
}
}(umask 077; printf 'Password: '; read -rs password; printf '\n'; printf '%s' "$password" > ~/.atomic/postgres-password)| Option | Required | Description |
| --- | --- | --- |
| url | Yes | A postgres:// or postgresql:// URL with a username and no embedded password. Any password in the URL or a password= query parameter is refused (credential-in-url). |
| password | When using password auth | Pass through secrets.password ({ "file": ... }, { "env": ... }, or inMemorySecret() in SDK { kind: "adapter" } selections). Because the adapter declares liveSecrets: ["password"], Atomic rereads secrets.password for every new connection and ignores options.password, throwing StorageConfigError (reason: "secret-missing") when secrets.password is omitted. |
| auth | No | "password" (default), or "azure-entra", "aws-iam", or "gcp-iam" for cloud IAM authentication. Omit secrets.password when using cloud auth; direct SDK calls to openPostgresDatabase() reject combining cloud auth with password (conflicting-token). |
| maxConnections | No | Most connections one Atomic process opens in its main pool. Default 8. A positive integer. Every store a process opens on the same URL, role, password, schema and pool options shares one main pool and one reader pool. See Tuning for many agents for how to size it. |
| readerMaxConnections | No | Most connections in the process's separate pool for reads, capped at maxConnections. Default 2. 0 turns the separate pool off, so reads share the main pool. |
| statementTimeoutMs | No | How long one statement may run, in milliseconds, before the server cancels it. Default 30000. |
| connectTimeoutMs | No | How long opening a connection, or waiting for a free one, may take, in milliseconds. Default 10000. |
| idleTimeoutMs | No | How long, in milliseconds, a pool's last connection stays open after the pool's last call. Default 300000. Connections opened for a burst close after 10 seconds idle. |
| keepAliveIdleMs | No | How long a connection sits unused, in milliseconds, before TCP keepalive probes start. Default 60000. |
| maxPreparedStatements | No | How many statements each pooled connection keeps prepared, so the server parses and plans each one once per connection instead of on every call. Default 256, or 0 (off) when listenUrl is set or the URL's port is 6432 or 6543, where a transaction pooler usually sits, unless the first connection finds PgBouncer 1.21 or later there with max_prepared_statements above 0. Set 0 to turn them off, for example behind a pooler without prepared-statement support. Each prepared statement uses some server memory on its connection. |
| listenUrl | No | Direct session-mode endpoint for wake-up notifications when url goes through a transaction pooler. Defaults to url. Enforces the same URL, TLS, proxy, and no-embedded-password rules as url (credential-in-url). |
| schema | No | Lowercase schema name for Atomic's tables. Default atomic. Databases with earlier tables in public and no atomic schema keep using public until atomic storage migrate moves them. Every transaction sets SET LOCAL search_path. |
| proxy | No | An http:// or https:// proxy URL for CONNECT tunneling, or "none" to connect directly. Defaults to environment proxy settings. |
Add a prefix to the storage block to isolate projects or teams inside one database. PostgreSQL prefixes must match [a-z_][a-z0-9_]* (lowercase ASCII only), cannot start with sqlite_, and must be at most 24 characters so generated identifiers stay within PostgreSQL's 63-byte limit; invalid prefixes throw StoragePrefixError. Atomic redacts passwords and tokens from error messages and diagnostics. See Storage backends and @bastani/atomic-storage-postgres docs for selection precedence and additional storage options.
TLS and SSL modes
When url omits sslmode, Atomic applies sslmode=verify-full for remote hosts (checking PGHOST if the URL omits the host), encrypting the connection and verifying both the server certificate and hostname.
- Add
sslrootcert=/path/to/ca.pemwhen the server certificate is signed by a private or custom CA. verify-cachecks the certificate's CA (sslrootcert, or Node's trusted CAs) without verifying the hostname.requirewithsslrootcertbehaves likeverify-ca. Withoutsslrootcert,requireverifies both certificate and hostname likeverify-full(stricter than libpq).preferandallowencrypt without verifying the certificate.disableturns TLS off.- TLS parameter names are case-sensitive; uppercase TLS parameters and unknown
sslmodevalues are rejected.
HTTP proxies
In environments that require an outbound HTTP proxy, every connection (pooled connections, wake-up listeners, cancel requests, and reconnects) tunnels through the proxy using CONNECT.
- Environment precedence: Atomic checks
HTTPS_PROXY(orhttps_proxy), thenALL_PROXYwhen it names an HTTP or HTTPS proxy, thenHTTP_PROXY, unlessNO_PROXYexempts the database host. Atomic's other HTTPS clients readHTTPS_PROXY,HTTP_PROXY, andNO_PROXYbut notALL_PROXY. - Direct overrides: Loopback hosts and Unix socket directories bypass environment proxies. Setting
options.proxyoverrides environment variables for TCP hosts, while socket directories always connect directly. - Security and errors: Proxy credentials are sent in
Proxy-Authorizationand redacted from logs and errors. TLS runs end to end through the tunnel, so keepsslmode=verify-full(orverify-cawith a privatesslrootcertCA) to prevent proxy interception. A refused tunnel fails withStorageUnavailableErrornaming both the proxy and targethost:port.
Connections and readers
Calls from one process that cannot conflict run at the same time, each on its own connection. Writes to different sessions are one example, and read-only transactions are another. Calls that conflict, such as two writes to one session, run in the order they were made. Saving new entries to a session takes 1 round trip to the server once atomic storage migrate has run, and 2 before.
openReader() returns a handle for reads. Every reader of the process shares one pool of up to readerMaxConnections connections, so reads do not wait for connections that writes are using. Closing the database also closes its readers.
Connections opened for a burst close after 10 seconds without use. A pool's last connection stays open for idleTimeoutMs after the pool's last call, so the next call does not wait for a new connection. Open connections never keep the process from exiting.
Tuning for many agents
Every Atomic process on every machine shares the server's max_connections. Give each process as many connections as the server can spare. When the server cannot give each process at least 2, connect through PgBouncer in transaction mode, as the PgBouncer guide describes. Tuning for many concurrent agents shows how to count connections, with worked examples, and the queries and errors that tell you what to change.
Connection poolers and wake-ups
The adapter works through poolers in transaction mode, such as PgBouncer and Supabase's Supavisor. It keeps no state on a connection between transactions.
Behind a pooler, prepared statements are off by default: when listenUrl is set, or the URL's port is 6432 (PgBouncer) or 6543 (Supavisor). The adapter turns them on by itself when its first connection finds PgBouncer 1.21 or later with max_prepared_statements above 0, which is the default from PgBouncer 1.24. Anything it cannot confirm leaves them off. If your pooler listens on another port, set listenUrl or maxPreparedStatements: 0. If a pooler loses prepared statements, the server reports prepared statement "atomic_…" does not exist or already exists. The adapter then runs the call again and stops preparing statements, and you can set maxPreparedStatements to 0 to avoid that first failure.
Each Intercom broker also holds one connection outside the pools to LISTEN for wake-up notifications, so it learns of new messages at once. Poolers in transaction mode do not pass LISTEN and NOTIFY, so behind one the adapter checks for new messages every half second instead, unless listenUrl points at the server directly. No message is lost either way. Run atomic storage status to see which mode is active.
Errors and retries
- Transient failures: Connection drops, lock conflicts (SQLSTATE
40001,40P01,55P03), and canceled statements (57014) throwStorageTransientError. Read-only or unwritten calls retry automatically. - Immediate failures: Invalid credentials (
28P01), missing databases (3D000), and certificate errors fail immediately without retrying. - Missing privileges: Permission errors (
42501) fail immediately withStorageConfigError(reason: "missing-privilege"), naming the missing privilege and the exactGRANToratomic storage migratecommand to run.
Azure
Azure Database for PostgreSQL (flexible server), PostgreSQL 16 or newer:
- Connect to
<server>.postgres.database.azure.com. Port5432reaches PostgreSQL directly; port6432reaches the built-in PgBouncer whenpgbouncer.enabledis turned on (General Purpose and Memory Optimized tiers; Burstable tiers do not include PgBouncer). When using port6432, setlistenUrlto port5432so wake-ups stay enabled. - Azure certificates chain to CAs trusted by Node.js, so no extra TLS parameter is required.
- The administrator belongs to
azure_pg_adminrather than superuser; the owner and runtime role setup in Set up the database works without changes. - Add
PG_TRGMto theazure.extensionsserver parameter before runningatomic storage migrate. Withoutpg_trgm, session history search scans sessions page by page. - Watch the server's connection limit. A Burstable B1ms server allows 50 connections (15 reserved for administrators), and each Atomic process opens up to
maxConnections(8), 2 reader connections, and one wake-up connection, however many stores it opens. LowermaxConnections, use PgBouncer, or choose a larger tier when running many processes.
Azure HorizonDB (preview), PostgreSQL 17:
- Use the read/write endpoint
<cluster>.rw.<id>.<region>.horizondb.azure.comon port5432. Atomic writes on every connection and cannot use the read-only endpoint. Wake-ups arrive on the read/write endpoint. - TLS is required and chains to CAs trusted by Node.js, so no extra TLS parameter is needed.
- The administrator belongs to
azure_pg_admin, using the same owner and runtime role setup as flexible server. - Add
pg_trgmtoazure.extensionsin the compute pool's parameter group. - Allow client addresses in the pool firewall rules, or connect over a private endpoint.
Both Azure services support Microsoft Entra authentication with auth: "azure-entra".
Cloud IAM authentication
On Azure, AWS, and Google Cloud, the adapter can sign in with the machine's cloud identity instead of a static password. Set auth and omit secrets.password:
{
"storage": {
"adapter": "@bastani/atomic-storage-postgres",
"options": {
"url": "postgresql://[email protected]/atomic?sslmode=verify-full",
"auth": "azure-entra"
}
}
}Each cloud provider uses its own SDK, which the adapter does not bundle. Install the matching package in user scope (listed below at the versions tested with the adapter). The adapter searches beside itself and in ~/.atomic/agent/npm, where atomic install npm:<package> installs user packages:
| auth | SDK | Install | Credential source |
| --- | --- | --- | --- |
| azure-entra | @azure/identity | atomic install npm:@azure/[email protected] | DefaultAzureCredential with scope https://ossrdbms-aad.database.windows.net/.default (AZURE_* variables, AKS workload identity, managed identity with optional AZURE_CLIENT_ID, or az login). |
| aws-iam | @aws-sdk/rds-signer | atomic install npm:@aws-sdk/[email protected] | RDS Signer for the URL's host, port, and user using the AWS default credential chain (AWS_*, profiles, IRSA, instance profile). Region is inferred from the RDS hostname or AWS_REGION/profile. |
| gcp-iam | google-auth-library | atomic install npm:[email protected] | GoogleAuth with scope https://www.googleapis.com/auth/sqlservice.login (GOOGLE_APPLICATION_CREDENTIALS, Compute Engine/GKE/Cloud Run metadata server, or gcloud auth application-default login). |
In an SDK project, install the same optional peer dependency alongside @bastani/atomic-storage-postgres. Only the configured cloud's SDK is loaded when opening the first connection.
For each new connection, the adapter requests a short-lived token and sends it as the password. Tokens stay in memory and never appear in settings, logs, or error messages. Azure tokens are reused until 5 minutes before expiration, while AWS and Google SDKs manage their own caching. If the server rejects a token (28P01, or Azure 28000), the adapter clears the cached token and retries once with a fresh token. Each token request times out after 8 seconds. A missing SDK or token failure throws StorageConfigError (reason: "auth-unavailable").
Cloud tokens require TLS; keep sslmode=verify-full (the remote default) or at least require.
Least-privilege roles
Atomic uses an owner role to own the database and atomic schema and run atomic storage migrate, plus a runtime role for sessions and Intercom brokers that runs no DDL. Grant the cloud identity runtime privileges after running atomic storage migrate as the owner role (see Set up the database):
GRANT CONNECT ON DATABASE atomic TO "<identity>";
GRANT USAGE ON SCHEMA atomic TO "<identity>";
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA atomic TO "<identity>";
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA atomic TO "<identity>";
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA atomic TO "<identity>";
ALTER DEFAULT PRIVILEGES FOR ROLE atomic_owner IN SCHEMA atomic GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO "<identity>";
ALTER DEFAULT PRIVILEGES FOR ROLE atomic_owner IN SCHEMA atomic GRANT USAGE, SELECT ON SEQUENCES TO "<identity>";
ALTER DEFAULT PRIVILEGES FOR ROLE atomic_owner IN SCHEMA atomic GRANT EXECUTE ON FUNCTIONS TO "<identity>";Cloud-specific role setup:
Azure Database for PostgreSQL (flexible server): Enable Microsoft Entra authentication. Connected to the postgres database as the Entra administrator, create a role matching the managed identity name and grant database access:
SELECT * FROM pgaadauth_create_principal('atomic-hosts', false, false);
GRANT CONNECT ON DATABASE atomic TO "atomic-hosts";No Azure RBAC role is needed. Use the role name as the username in url.
Azure HorizonDB: Enable Microsoft Entra authentication on the cluster, add an Entra administrator, and run the same pgaadauth_create_principal and GRANT CONNECT statements as flexible server.
Amazon RDS and Aurora PostgreSQL: Enable IAM database authentication on the instance or cluster, create the user, and allow rds-db:connect for that user:
CREATE USER atomic;
GRANT rds_iam TO atomic;
GRANT CONNECT ON DATABASE atomic TO atomic;{
"Effect": "Allow",
"Action": "rds-db:connect",
"Resource": "arn:aws:rds-db:<region>:<account-id>:dbuser:<DbiResourceId or cluster resource id>/atomic"
}Google Cloud SQL for PostgreSQL: Enable cloudsql.iam_authentication=on, add the service account as an IAM database user, and grant roles/cloudsql.instanceUser (add roles/cloudsql.client only when connecting through the Cloud SQL Auth Proxy or a connector). Use the service account email without .gserviceaccount.com as the database username:
gcloud sql users create [email protected] --instance=atomic --type=cloud_iam_service_account
gcloud projects add-iam-policy-binding my-project \
--member=serviceAccount:[email protected] --role=roles/cloudsql.instanceUserGRANT CONNECT ON DATABASE atomic TO "[email protected]";Compute Engine VMs require the cloud-platform access scope for metadata tokens. AlloyDB uses the same pattern with roles/alloydb.databaseUser and roles/serviceusage.serviceUsageConsumer.
Use from the SDK
Install @bastani/atomic and @bastani/atomic-storage-postgres with npm install, open a SqliteDatabase with openPostgresDatabase(), and pass it to createAgentSession() via sessionStore: { kind: "database", database, prefix? }:
import { readFile } from "node:fs/promises";
import { createAgentSession } from "@bastani/atomic";
import { openPostgresDatabase } from "@bastani/atomic-storage-postgres";
const database = await openPostgresDatabase({
url: "postgresql://[email protected]:5432/atomic",
password: async () => (await readFile("/run/secrets/postgres-password", "utf8")).trim(),
});
try {
const { session } = await createAgentSession({
sessionStore: { kind: "database", database, prefix: "app_" },
});
try {
await session.prompt("Summarize the failing tests");
} finally {
await session.dispose();
}
} finally {
await database.close();
}password accepts a string or an async callback invoked for each new connection. When connecting as a low-privilege runtime role, run atomic storage migrate --db 'postgresql://atomic_owner@...' once as the owner role (and after Atomic upgrades) before opening sessions from the SDK.
session.dispose() closes the SqlSessionStore wrapper opened for { kind: "database", database } and leaves your caller-owned database open so multiple sessions can share one pool. Close database in a finally block after all sessions are disposed. If you open SqlSessionStore.open(database, { prefix }) directly, close the store once before closing database.
Additional SDK options and exports:
sdkSearchPaths: Extra npm prefix directories to search when loading cloud IAM SDKs.socket: Custom transport callback receiving{ host, port }(fromurl, orPGHOST/PGPORT) and returning a stream carrying the PostgreSQL protocol. Atomic negotiates TLS over it as over a normal socket. Cannot be combined withproxy(conflicting-transport), and socket directories still connect directly.numberPlaceholders(sql)andphysicalIdentifiers(sql): Exported SQL rewrite helpers for converting positional?parameters to$1, $2, ...and shortening unquoted identifiers longer than 63 bytes.
The package ships plain ESM JavaScript with TypeScript declarations and works directly in supported Node.js versions without a TypeScript loader. See SDK session storage and @bastani/atomic-storage-postgres for full SDK recipes, store ownership rules, and error handling.
License
MIT. See LICENSE.
