JSPM

  • Created
  • Published
  • Downloads 5212631
  • Score
    100M100P100Q240172F
  • License Apache-2.0

PGlite is a WASM Postgres build packaged into a TypeScript client library that enables you to run Postgres in the browser, Node.js and Bun, with no need to install any other dependencies. It is only 3.7mb gzipped.

Package Exports

  • @electric-sql/pglite
  • @electric-sql/pglite/contrib/adminpack
  • @electric-sql/pglite/contrib/amcheck
  • @electric-sql/pglite/contrib/auto_explain
  • @electric-sql/pglite/contrib/bloom
  • @electric-sql/pglite/contrib/btree_gin
  • @electric-sql/pglite/contrib/btree_gist
  • @electric-sql/pglite/contrib/citext
  • @electric-sql/pglite/contrib/cube
  • @electric-sql/pglite/contrib/earthdistance
  • @electric-sql/pglite/contrib/fuzzystrmatch
  • @electric-sql/pglite/contrib/hstore
  • @electric-sql/pglite/contrib/isn
  • @electric-sql/pglite/contrib/lo
  • @electric-sql/pglite/contrib/ltree
  • @electric-sql/pglite/contrib/pg_trgm
  • @electric-sql/pglite/contrib/seg
  • @electric-sql/pglite/contrib/tablefunc
  • @electric-sql/pglite/contrib/tcn
  • @electric-sql/pglite/contrib/tsm_system_rows
  • @electric-sql/pglite/contrib/tsm_system_time
  • @electric-sql/pglite/contrib/uuid_ossp
  • @electric-sql/pglite/live
  • @electric-sql/pglite/nodefs
  • @electric-sql/pglite/opfs-ahp
  • @electric-sql/pglite/vector
  • @electric-sql/pglite/worker

Readme

ElectricSQL logo

PGlite - the WASM build of Postgres from ElectricSQL.
Build reactive, realtime, local-first apps directly on Postgres.

License - Apache 2.0 Status - Alpha Chat - Discord

PGlite - Postgres in WASM

PGlite

PGlite is a WASM Postgres build packaged into a TypeScript client library that enables you to run Postgres in the browser, Node.js and Bun, with no need to install any other dependencies. It is only 3mb gzipped and has support for many Postgres extensions, including pgvector.

import { PGlite } from "@electric-sql/pglite";

const db = new PGlite();
await db.query("select 'Hello world' as message;");
// -> { rows: [ { message: "Hello world" } ] }

It can be used as an ephemeral in-memory database, or with persistence either to the file system (Node/Bun) or indexedDB (Browser).

Unlike previous "Postgres in the browser" projects, PGlite does not use a Linux virtual machine - it is simply Postgres in WASM.

For full documentation and user guides see pglite.dev.

Browser

It can be installed and imported using your usual package manager:

import { PGlite } from "@electric-sql/pglite";

or using a CDN such as JSDeliver:

import { PGlite } from "https://cdn.jsdelivr.net/npm/@electric-sql/pglite/dist/index.js";

Then for an in-memory Postgres:

const db = new PGlite()
await db.query("select 'Hello world' as message;")
// -> { rows: [ { message: "Hello world" } ] }

or to persist the database to indexedDB:

const db = new PGlite("idb://my-pgdata");

Node/Bun

Install into your project:

npm install @electric-sql/pglite

To use the in-memory Postgres:

import { PGlite } from "@electric-sql/pglite";

const db = new PGlite();
await db.query("select 'Hello world' as message;");
// -> { rows: [ { message: "Hello world" } ] }

or to persist to the filesystem:

const db = new PGlite("./path/to/pgdata");

How it works

PostgreSQL typically operates using a process forking model; whenever a client initiates a connection, a new process is forked to manage that connection. However, programs compiled with Emscripten - a C to WebAssembly (WASM) compiler - cannot fork new processes, and operates strictly in a single-process mode. As a result, PostgreSQL cannot be directly compiled to WASM for conventional operation.

Fortunately, PostgreSQL includes a "single user mode" primarily intended for command-line usage during bootstrapping and recovery procedures. Building upon this capability, PGlite introduces a input/output pathway that facilitates interaction with PostgreSQL when it is compiled to WASM within a JavaScript environment.

Limitations

  • PGlite is single user/connection.

Acknowledgments

PGlite builds on the work of Stas Kelvich of Neon in this Postgres fork.

License

PGlite is dual-licensed under the terms of the Apache License 2.0 and the PostgreSQL License, you can choose which you prefer.

Changes to the Postgres source are licensed under the PostgreSQL License.