← Back to all spotlights

PGlite: Running Full Postgres and pgvector in WebAssembly

ElectricSQL's PGlite packages complete Postgres and pgvector into a 3MB WebAssembly bundle for in-browser RAG and reactive local-first apps.

P24
By Pickwise24 Editorial Team
Verified Open-Source Review

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.

FeatureBrowser SQLite (sql.js / OPFS)Traditional PostgresPGlite (ElectricSQL)
Footprint (Gzipped)~1MBHundreds of MBs~3MB
DialectSQLite dialectPure PostgreSQLPure PostgreSQL
Extension SupportCustom C extensionsRich extension ecosystemSelect Wasm extensions (pgvector, fuzzystrmatch, uuid-ossp)
Startup OverheadMillisecondsSeconds (or Docker spin-up)< 20ms
Browser PersistenceIndexedDB or OPFSNone (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:

  1. 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.
  2. 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.
  3. 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.

πŸ›‘οΈ Editorial Standards & Methodology

Every repository featured on Pickwise24 undergoes testing on local workstation hardware before publication. We verify CLI installation steps, review open-source repository licensing, benchmark computational footprint, and evaluate architectural trade-offs to provide genuine, high-utility developer intelligence.