BarakChamo

payload pglite

by BarakChamo
Testing & developer experience
v3
v4
^3.94.0 || ^4.0.0-beta.12
MIT

Overview

Summary generated from the README

Wraps Payload's Postgres adapters so they run against an embedded PGlite database behind a loopback socket during development, tests and CI. A CLI starts PGlite and passes its URL to your dev server, while the wrapper returns your adapter unchanged when no PGlite URL is set. Works with the Postgres and Vercel Postgres adapters on Payload 3 and 4, and is not meant for production.

Features

  • withPGlite wrapper and pglitePlugin that fit the Postgres or Vercel Postgres adapter to PGlite
  • payload-pglite command that starts PGlite and runs next dev, payload migrate or any other command
  • Per-checkout data stored in .cache/pglite on an OS-picked port, with a lock to avoid collisions
  • startPGlite helper gives each test file an in-memory database
  • Pool capped to one connection, with a clear error when a nested call forgets req and deadlocks
  • Leaves the production adapter and bundle untouched when no PGlite URL is set

Use cases

  • Develop a Payload app on Postgres without Docker or a local database server
  • Run Vitest suites on real Postgres with one in-memory database per test file
  • Generate and run migrations against the same SQL engine used in production

From the author

Real PostgreSQL for Payload CMS development, tests and CI with nothing to install: an embedded PGlite behind a loopback socket, and a wrapper that fits Payload's own Postgres adapters to it (v3 and v4)

Installation

npm i payload-pglite

0

Stars

0

Forks

0

Open Issues

Today

Last Updated

0

Weekly Downloads

v

npm Version

Dependencies

GitHub stars

Trends appear after 7 days of data. Collecting since Dec 6 — 1 daily snapshot so far.

npm weekly downloads

Trends appear after 7 days of data. Daily snapshots have not started yet.

Each point is npm's rolling 7-day download count.

Health Score

35/100Fair

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/1484879902-root.svg)](https://payloaddirectory.dev/plugins/1484879902-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1484879902-root"><img src="https://payloaddirectory.dev/badge/1484879902-root.svg" alt="Payload Directory"></a>
Updated Dec 5, 2026Created Dec 5, 2026

README

payload-pglite

Real PostgreSQL for Payload CMS development, tests and CI, with nothing to install.

Run your app on an embedded Postgres (PGlite) through the same Postgres adapter you deploy with,
and leave production untouched.

CI npm Payload 3 and 4 MIT license

Quick start • Docs • Examples • Testing • Changelog • npm

npx payload-pglite next dev
# PGlite starts in .cache/pglite, Next.js gets PAYLOAD_PGLITE_URL, and Payload runs on Postgres
// payload.config.ts: your deployed config, plus one wrapper
db: withPGlite(postgresAdapter({ pool: { connectionString: process.env.DATABASE_URL } }), {
	url: process.env.PAYLOAD_PGLITE_URL, // set in development only
}),

Why payload-pglite

Payload on Postgres usually means running a database server for development: Docker, a local install, or a shared cloud branch. SQLite avoids that, but it is a different database, with a different adapter, different SQL and different behavior around transactions and locks. payload-pglite gives every checkout its own real Postgres, with no server:

  • Your production adapter, unchanged. @payloadcms/db-postgres or @payloadcms/db-vercel-postgres runs the same SQL, migrations and Drizzle schema it runs in production. You are not swapping in a different adapter for development.
  • Nothing reaches production. Without a PGlite URL the wrapper returns your adapter untouched. The entry your config imports has no PGlite code in it, so your deployment's bundle and runtime are exactly what they were.
  • Nothing to install or run. PGlite is Postgres compiled to WebAssembly, in-process. The payload-pglite command starts it, hands your dev server its URL, and shuts it down cleanly.
  • One database per checkout. Data lives in .cache/pglite, on a port the OS picks, behind a lock: git worktrees and parallel projects never collide.
  • Tests on real Postgres. startPGlite() gives each test file an in-memory database in under a second.
  • Mistakes surface instead of hiding. A nested Local API call that forgets req deadlocks against its own transaction. A production pool hides this until load; PGlite has one connection, so it fails at once, with an error that says how to fix it.

Quick start

1. Install

npm install payload-pglite
npm install --save-dev @electric-sql/pglite @electric-sql/pglite-socket

The two PGlite packages are only needed by the payload-pglite command and by tests, so they stay in devDependencies.

2. Wrap your Postgres adapter

import { postgresAdapter } from '@payloadcms/db-postgres'
import { buildConfig } from 'payload'
import { withPGlite } from 'payload-pglite'

export default buildConfig({
	db: withPGlite(postgresAdapter({ pool: { connectionString: process.env.DATABASE_URL } }), {
		url: process.env.PAYLOAD_PGLITE_URL,
	}),
	// ...
})

Prefer plugins? plugins: [pglitePlugin({ url: process.env.PAYLOAD_PGLITE_URL })] does the same. It works with vercelPostgresAdapter() too.

3. Run your dev server through payload-pglite

{
	"scripts": {
		"dev": "payload-pglite next dev",
		"payload": "payload-pglite payload"
	}
}

npm run dev now runs Payload on Postgres. npm run payload migrate:create and every other Payload CLI command use the same database. Deployments don't use the command, never get PAYLOAD_PGLITE_URL, and run your adapter as shipped.

How it works

The payload-pglite command starts PGlite behind a loopback Postgres socket (127.0.0.1, a port the OS picks), then runs your command with PAYLOAD_PGLITE_URL and DATABASE_URL set to it. When it sees that URL, withPGlite points your adapter at it and fits it to PGlite:

  • One connection. The socket serves one connection at a time, so the pool is capped at one, and read replicas are switched off.
  • Clean reconnects. On hot reload and on shutdown the old pool is drained, so the next one can connect straight away.
  • Explained waits. A call that waits longer than 30 s for the connection fails with PGliteConnectionWaitError, which names the usual cause.

→ How it works

The payload-pglite command

payload-pglite [--cwd <directory>] <command> [arguments...]

It runs any command: next dev, payload migrate, a seed script, turbo dev. It keeps the data in PAYLOAD_PGLITE_DATA_DIR (default .cache/pglite), forwards signals, exits with your command's exit code, and refuses NODE_ENV=production.

→ Development

Tests

import { startPGlite } from 'payload-pglite/server'

const database = await startPGlite() // in memory; pass { dataDir } to keep it
const payload = await getPayload({
	config: await buildConfig({
		...config,
		db: withPGlite(postgresAdapter({ pool: {} }), { url: database.url }),
	}),
})
// ...
await payload.destroy()
await database.close()

→ Testing

Migrations

push: true (the default in development) keeps the schema in step as you edit. When you write migrations, generate and run them against PGlite, the same engine and SQL as production:

npx payload-pglite payload migrate:create add-summary
npx payload-pglite payload migrate

→ Migrations

Examples

  • Shop: an existing app with withPGlite. Orders take stock in the same transaction, a refused order changes nothing, and concurrent orders never oversell.
  • Vercel Postgres with the plugin: a docs site on vercelPostgresAdapter with pglitePlugin. Editors sign in, drafts stay private, and published pages are public.
  • Testing: one in-memory database per Vitest file, real rollbacks, and how a forgotten req fails fast.
  • Migrations: generate, apply and evolve migrations through the Payload CLI under payload-pglite, with push: false.

Each example has its own README and tests that run in CI.

Compatibility

Payload3 (≥ 3.94) and 4 (beta)
Adapters@payloadcms/db-postgres, @payloadcms/db-vercel-postgres (without forceUseVercelPostgres)
PGlite@electric-sql/pglite 0.5, @electric-sql/pglite-socket 0.2
Node22, 24, 26+ (as Payload requires)
UseDevelopment, tests and CI. Never production.

CI runs every suite on Payload 3 and 4. payload-pglite is pre-1.0, so minor versions may still change the API; the changelog lists every change.

Contributing

Issues and pull requests are welcome. See CONTRIBUTING.md for setup, and TESTING.md for the suites.

License

MIT

Similar plugins