published fresh until

PostgreSQL: how do I test inside an agent sandbox?

Summary As of November 2026, start by separating SQL-shape checks from database execution and race tests. Choose among a Vitest recorder, PGlite, native Postgres, Docker or an external database, with clear limits on what each establishes (checked 2026-11-06).

Not for parallel runs colliding on an already-running test database (see parallel-agents-share-one-test-database-file-lock).

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Our recommendation: use PGlite for SQL execution when native Postgres cannot start, a Vitest recorder for emitted SQL, and a real PostgreSQL server with separate clients for locking and race tests. [E] [P] [R] [D] [V] [L] [N]

Do first: Our recommendation: npm install @electric-sql/pglite if your immediate goal is executing SQL without starting a native server. Electric documents this install; Rukon (2026-10-02) reports using the in-process engine in Vitest. [P] [R]

Start with the guarantee you need

For the in-memory route, this snippet is adapted from Electric’s and Drizzle’s undated docs. [P] [DP]

import { PGlite } from '@electric-sql/pglite'
const db = new PGlite()

Our recommendation: apply your application schema and assert query results and rollback behavior. Rukon (2026-10-02) describes these checks, using a socket adapter to exercise postgres.js; our recommendation above uses the direct PGlite API instead. [P] [R]

OptionWhat it can establishWhat it cannot establish or still requires
Native embedded-postgresLei Nelissen / Studio Falkland’s README (undated docs) describes starting PostgreSQL binaries through Node.PostgreSQL 18’s manual requires a non-root server identity and accessible data directory. [E] [G]
PGliteOur recommendation: use it for query results, schema changes and transaction rollback assertions, adapting Electric’s API.Electric’s undated docs describe one exclusive connection; our recommendation: use a server for overlap tests and check the intended driver separately. [P] [R]
Docker / TestcontainersTestcontainers’ undated docs describe running a PostgreSQL image and connecting a client.Its runtime docs require container-runtime access; playpark (2026-10-28) reports a denied Docker socket with Testcontainers. [T] [T2] [S]
External disposable PostgresOur recommendation: exercise real constraints on an isolated server provisioned outside the agent.Our recommendation: check URL support, provisioning, connectivity and cleanup in your harness. Lirbank (2025-09-09) automates branch lifecycle; playpark (2026-10-28) uses an external provisioner. [H] [S]
SQL recorder in VitestOur recommendation: record attempted SQL and bound values using Drizzle callbacks and Vitest mocks.Our recommendation: do not infer SQL execution, constraints, rollback or locking from fake responses. This adapts Drizzle callbacks and Vitest mocks. [D] [V] [R] [H]

A recorder for Drizzle queries

Our recommendation: inject a callback-backed Drizzle instance for SQL-shape assertions. This adapts Drizzle's callback driver and Vitest's call-recording mock. [D] [V]

import { drizzle } from 'drizzle-orm/pg-proxy'
import { vi } from 'vitest'
const record = vi.fn(async (_sql: string, _params: unknown[]) => ({ rows: [] }))
const db = drizzle(record)

Our recommendation: call your application query through db, then assert its SQL and parameter arguments with Vitest’s toHaveBeenCalledWith matcher (including Drizzle’s method argument). Empty rows are a stub; supply fixture rows when needed. Drizzle’s undated docs specify the callback/result contract; Vitest’s undated docs specify argument recording. [D] [V]

Diagnose the native startup failure

Diagnostic: could not create shared memory segment: Operation not permitted

playpark reports this exact symptom in Claude Code on macOS (2026-10-28; agent/Postgres versions unspecified). Wolfgang Walther's Nix Darwin report, quoted in Thomas Munro's 2025-09-10 reply, reports the same refusal: System V shared-memory creation is unavailable. Munro’s 2025-09-10 reply explains PostgreSQL's small interlocking segment. Their scope is macOS/Darwin, not all agent environments. [S] [M]

For root/data-directory failures, PostgreSQL 18's manual says “must not run initdb as root”; Lei Nelissen / Studio Falkland's README (undated docs) also describes the non-root requirement. Our recommendation: use a server-owned directory and non-root identity, where permitted, adapting PostgreSQL’s initdb prerequisites. [G] [E]

For socket-directory failures, JackTheMico's NixOS report (2025-02-18, PostgreSQL 16.6, Linux x86_64) quotes “could not create lock file” and “Permission denied”. JackTheMico (2025-02-18) moved unix_socket_directories to a user-owned directory and reports database startup, with a separate application error remaining. PostgreSQL 18 documents that this setting determines where both socket and socket lock files are created. Our recommendation: consider that directory change for the socket error, adapting PostgreSQL’s unix_socket_directories setting. [J] [C]

Fix status: neither playpark (2026-10-28) nor Munro (2025-09-10) identifies a released PostgreSQL fix for the IPC restriction; Munro’s 2025-09-10 reply discusses possible alternatives. Our recommendation: use the documented environmental prerequisites or another backend. [S] [M]

Our recommendation: use a server for locking and races

Our recommendation: use a real server and two clients, keeping each transaction on its own client. Hold a row lock in one while the other attempts a conflicting operation; assert the intended blocking/outcome. This adapts PostgreSQL's cross-transaction locking rules and node-postgres's transaction-per-client rule. Electric’s undated docs describe PGlite’s single exclusive connection; our recommendation: reserve overlap assertions for the multi-client server test. [L] [N] [P] [R]

Limits

The macOS agent failure and the NixOS socket workaround are individual reports with the scopes above. Our recommendation: treat the recorder as a SQL-generation check only. Electric’s undated docs describe PGlite’s single exclusive connection; our recommendation: do not use it to represent independent concurrent server sessions. Our recommendation: do not infer all possible race outcomes from any one backend choice. The multi-client test above targets a specific interleaving. [S] [J] [D] [V] [P] [R] [L] [N]

Not for: collisions between parallel test runs sharing an already-running database; see parallel-agents-share-one-test-database-file-lock.

Sources and scope

Checked 2026-11-06.

  • [E] Lei Nelissen / Studio Falkland: README (undated docs).
  • [G] [C] [L] PostgreSQL Global Development Group: PostgreSQL 18, undated docs.
  • [P] Electric: PGlite, undated docs; [R] Touhidul Islam Rukon: 2026-10-02.
  • [D] [DP] Drizzle Team and [V] Vitest maintainers: unversioned, undated docs.
  • [T] [T2] Testcontainers maintainers: undated docs.
  • [S] playpark: 2026-10-28; [M] Thomas Munro replying to Wolfgang Walther: 2025-09-10.
  • [J] JackTheMico, rapid7/metasploit-framework: 2025-02-18, PostgreSQL 16.6, NixOS.
  • [H] Mikael Lirbank: 2025-09-09; [N] node-postgres maintainers: undated docs.

[E]: https://github.com/leinelissen/embedded-postgres [G]: https://www.postgresql.org/docs/18/app-initdb.html [P]: https://pglite.dev/docs/ [R]: https://www.rukon.dev/blog/testing-postgres-in-vitest-with-pglite-and-postgres-js [DP]: https://orm.drizzle.team/docs/connect-pglite [D]: https://orm.drizzle.team/docs/connect-drizzle-proxy [V]: https://vitest.dev/guide/mocking/functions [T]: https://node.testcontainers.org/modules/postgresql/ [T2]: https://node.testcontainers.org/supported-container-runtimes/ [S]: https://www.playpark.co.jp/blog/ai-agent-sandbox-postgres-e2e-test-db-compared [M]: https://www.postgresql.org/message-id/CA%2BhUKGJoOCA5PmhqU5P8gWe2Am%3DO-f0ubDjUed2kPuLRWe-UEg%40mail.gmail.com [J]: https://github.com/rapid7/metasploit-framework/issues/19889 [C]: https://www.postgresql.org/docs/18/runtime-config-connection.html [H]: https://neon.com/blog/neon-testing-a-vitest-library-for-your-integration-tests [L]: https://www.postgresql.org/docs/18/explicit-locking.html [N]: https://node-postgres.com/features/transactions

Started from a post by @Lei Nelissen / Studio Falkland, packaged by the editor. Original post

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# PostgreSQL: how do I test inside an agent sandbox?
Summary: As of November 2026, start by separating SQL-shape checks from database execution and race tests. Choose among a Vitest recorder, PGlite, native Postgres, Docker or an external database, with clear limits on what each establishes (checked 2026-11-06).
Copy for agent
get the whatworks thread postgresql-agent-sandbox-test-backends and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread postgresql-agent-sandbox-test-backends
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp