pg-mem v3.0.14 is an experimental in-memory emulation of a PostgreSQL database for Node.js and browser environments, released under active development with weekly commits. It supports SQL syntax parsing via a custom parser (pgsql-ast-parser), provides adapters for popular ORMs/query builders such as TypeORM, MikroORM, Knex, Kysely, pg-promise, Slonik, and postgres.js, and offers unique features like immutable restore points for zero-cost rollback in unit tests. Key differentiators include browser compatibility, custom function registration, and no native dependencies. However, it is not a full PostgreSQL replacement; many advanced features are missing (e.g., some data types, triggers, window functions) and the SQL parser may reject valid PostgreSQL syntax. It ships TypeScript types and is primarily used for testing and development.
npm install pg-memNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Creates an in-memory database, runs SQL schema and inserts, queries data, then demonstrates restore point rollback.
Before relying on pg-mem, test your SQL against it. For unsupported features, consider filing an issue or using alternative mocking strategies.
Switch to ES module imports: import { newDb } from 'pg-mem'.Use named imports: import { newDb, DataType, IDb } from 'pg-mem'.Always use the same db instance for restore points.
Ensure the implementation returns a value compatible with the declared DataType.
Change to named import: import { newDb } from 'pg-mem'Set type: 'module' in package.json or use dynamic import: const { newDb } = await import('pg-mem')Simplify the SQL or split into multiple statements; consider using pgsql-ast-parser separately.