For years, the standard developer playbook for offline-first web apps was predictable: run SQLite in the browser via WebAssembly, pray your SQL dialect differences don't bite you in production, and run actual PostgreSQL in a hefty Docker container on your server. If you wanted vector search on the client, you usually wound up bolting on bespoke JavaScript cosine-distance loops that brought your user's laptop fans to a screaming halt.
The team at ElectricSQL decided that compromise was thoroughly unnecessary. Enter PGlite (GitHub: electric-sql/pglite), an open-source project that compiles official PostgreSQL source code into a WebAssembly build weighing in at under 3MB compressed.
It is not an API shim, an emulation layer, or an expressive wrapper around SQLite. It is genuine Postgres running directly inside your browser tab, Web Worker, Node.js process, Bun, or Deno runtimeβcomplete with extensions like pgvector.
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Browser / Edge Runtime β
β β
β WebGPU / Transformers.js ββ> [ 384-dim Embedding ] β
β β β
β βΌ β
β ββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β PGlite (Postgres 16 in WebAssembly, ~3MB) β β
β β βββ Single-Process Architecture β β
β β βββ pgvector Extension (HNSW / IVFFlat) β β
β β βββ Storage Drivers: β β
β β βββ In-Memory (Ephemeral) β β
β β βββ IndexedDB (Persistent Browser) β β
β β βββ Node/Bun File System (fs) β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββ β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
What Makes PGlite Architecturally Unique?
PostgreSQL's traditional architecture relies heavily on a multi-process model coordinated by the postmaster daemon, passing messages through shared memory and Unix sockets. Browsers, naturally, do not tolerate arbitrary multi-process IPC or raw POSIX socket setups.
PGlite solves this by stripping Postgres down to its single-user mode (postgres --single), wrapping it in an Emscripten-compiled C filesystem layer, and replacing the networking stack with direct memory exchange. When you send a query string to PGlite, it passes directly through the PostgreSQL parser, planner, and executor entirely inside the WebAssembly linear memory space.
| Feature | Browser SQLite (sql.js / OPFS) | Traditional Postgres | PGlite (ElectricSQL) |
|---|---|---|---|
| Footprint (Gzipped) | ~1MB | Hundreds of MBs | ~3MB |
| Dialect | SQLite dialect | Pure PostgreSQL | Pure PostgreSQL |
| Extension Support | Custom C extensions | Rich extension ecosystem | Select Wasm extensions (pgvector, fuzzystrmatch, uuid-ossp) |
| Startup Overhead | Milliseconds | Seconds (or Docker spin-up) | < 20ms |
| Browser Persistence | IndexedDB or OPFS | None (remote only) | IndexedDB, Memory, or OPFS |
Local Vector Search with pgvector
With the explosion of local, private AI workflows powered by WebGPU (such as running small open-weight embedding models inside the browser via Transformers.js), developers quickly hit a bottleneck: where do you store and index thousands of vectors on the user's machine without building an ad-hoc index from scratch?
PGlite ships with modular extension support, notably bundling pgvector. You can build fully offline Retrieval-Augmented Generation (RAG) pipelines where document chunking, embedding generation, vector indexing, and relational querying happen entirely within the user's browser session.
Installation
Install PGlite alongside the vector extension via your favourite package manager:
npm install @electric-sql/pglite
Running In-Browser Vector Queries
Here is how straightforward it is to initialise an IndexedDB-persisted Postgres database, activate pgvector, and run an approximate nearest-neighbour query:
import { PGlite } from '@electric-sql/pglite';
import { vector } from '@electric-sql/pglite/vector';
// Initialise Postgres persisted to the browser's IndexedDB
const db = new PGlite('idb://rag-demo-database', {
extensions: { vector },
});
// Create tables using native Postgres syntax
await db.exec(`
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE IF NOT EXISTS local_documents (
id SERIAL PRIMARY KEY,
content TEXT,
embedding vector(3)
);
`);
// Insert chunks with vector embeddings
await db.query(
`INSERT INTO local_documents (content, embedding) VALUES ($1, $2);`,
['PGlite brings native SQL to local runtimes.', '[0.12, 0.84, -0.22]']
);
await db.query(
`INSERT INTO local_documents (content, embedding) VALUES ($1, $2);`,
['SQLite remains widely used in mobile development.', '[0.05, 0.11, 0.78]']
);
// Execute a cosine distance similarity search (<=> operator)
const queryEmbedding = '[0.10, 0.80, -0.20]';
const results = await db.query(`
SELECT content, embedding <=> $1 AS distance
FROM local_documents
ORDER BY distance ASC
LIMIT 1;
`, [queryEmbedding]);
console.log('Top match:', results.rows[0]);
Key Developer Use Cases
Community consensus across recent YouTube deep-dives and dev forums points to three primary scenarios where PGlite immediately pays for itself:
- Deterministic Testing in CI/CD: Running automated test suites with real PostgreSQL often meant waiting for Docker containers to initialise or managing shared test databases. With PGlite, tests written for Vitest or Jest can spin up an ephemeral, fresh PostgreSQL instance in less than 20 milliseconds directly inside the test runner process.
- Local-First Web Applications: Building offline-capable applications normally requires maintaining two separate schemas: one for browser SQLite and another for backend Postgres. PGlite eliminates this cognitive overhead. Your frontend schema, migrations, and ORM queries (such as Drizzle or Kysely) share exact syntax parity with your backend.
- Zero-Egress Client-Side AI: For security-conscious use casesβsuch as legal document analysis or healthcare record reviewβuploading sensitive document chunks to third-party cloud vector stores is a non-starter. PGlite allows end-to-end local document search without a single byte leaving the user's local hardware.
Key Takeaways for Builders
- Zero Network Latency: Queries execute synchronously inside WebAssembly memory without HTTP or WebSocket overhead.
- Micro Footprint: A fully functional, standards-compliant SQL engine bundled inside a footprint small enough for typical web bundle budgets.
- Universal Runtime: Code written against PGlite functions identically across Chrome, Firefox, Safari, Edge, Node.js, Bun, Deno, and Electron without environment-specific branches.
PGlite fundamentally alters the division of labour between the browser and the server. By shrinking one of open source's most battle-tested databases down to a disposable 3MB WebAssembly module, ElectricSQL has handed developers an unprecedented tool for reactive, offline-first, and vector-enabled applications.