MCPcopy
hub / github.com/electric-sql/pglite

github.com/electric-sql/pglite @pglite-unixsocket-example@1.0.0 sqlite

repository ↗ · DeepWiki ↗ · release pglite-unixsocket-example@1.0.0 ↗ · compare 2 versions
736 symbols 2,025 edges 217 files 83 documented · 11%
README

  <img alt="ElectricSQL logo"
      src="https://raw.githubusercontent.com/electric-sql/pglite/main/docs/public/img/brand/logo-light.svg"
  />

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, Bun and Deno, 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/Deno) 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/Deno

Install into your project:

NodeJS

npm install @electric-sql/pglite

Bun

bun install @electric-sql/pglite

Deno

deno add npm:@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.

How to build PGlite and contribute

The build process of PGlite is split into two parts:

  1. Building the Postgres WASM module.
  2. Building the PGlite client library and other TypeScript packages.

Docker is required to build the WASM module, along with Node (v20 or above) and pnpm for package management and building the TypeScript packages.

To start checkout the repository and install dependencies:

git clone --recurse-submodules https://github.com/electric-sql/pglite
cd pglite
pnpm install

To build everything, we have the convenient pnpm build:all command in the root of the repository. This command will:

  1. Use Docker to build the Postgres WASM module. The artifacts produced by this step are then copied to /packages/pglite/release.
  2. Build the PGlite client library and other TypeScript packages.

To only build the Postgres WASM module (i.e. point 1 above), run

pnpm wasm:build

If you don't want to build the WASM module and assorted WASM binaries from scratch, they are generated automatically on Github after each successful PR merge. You can download the latest binaries by going to the last successfully merged PR and clicking the link after the comment Interim build files:. Extract the files and place them under packages/pglite/release in your local repo copy.

To build all TypeScript packages (i.e. point 2 of the above), run:

pnpm ts:build

This will build all packages in the correct order based on their dependency relationships. You can now develop any individual package using the build and test scripts, as well as the stylecheck and typecheck scripts to ensure style and type validity.

Or alternatively to build a single package, move into the package directory and run:

cd packages/pglite
pnpm build

When ready to open a PR, run the following command at the root of the repository:

pnpm changeset

And follow the instructions to create an appropriate changeset. Please ensure any contributions that touch code are accompanied by a changeset.

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.

Extension points exported contracts — how you extend this code

Filesystem (Interface)
(no doc) [2 implementers]
packages/pglite/src/fs/base.ts
NoticeOrError (Interface)
(no doc) [2 implementers]
packages/pg-protocol/src/messages.ts
QueuedConnection (Interface)
* Represents a queued connection with timeout
packages/pglite-socket/src/index.ts
SubscriptionState (Interface)
(no doc)
packages/pglite-sync/src/subscriptionState.ts
PGliteDependencyInjection (Interface)
(no doc)
packages/pglite-vue/src/dependency-injection.ts
PgDumpOptions (Interface)
(no doc)
packages/pglite-tools/src/pg_dump.ts
Result (Interface)
(no doc)
packages/benchmark/baseline.ts
Props (Interface)
(no doc)
packages/pglite-react/src/provider.tsx

Core symbols most depended-on inside this repo

query
called by 375
packages/pglite/src/interface.ts
exec
called by 364
packages/pglite/src/interface.ts
log
called by 142
packages/pglite-socket/src/index.ts
join
called by 106
packages/pg-protocol/test/testing/buffer-list.ts
create
called by 75
packages/pglite/src/pglite.ts
sql
called by 68
packages/pglite/src/interface.ts
addInt16
called by 43
packages/pg-protocol/test/testing/buffer-list.ts
close
called by 40
packages/pglite/src/fs/opfs-ahp.ts

Shape

Method 310
Function 285
Class 80
Interface 58
Enum 3

Languages

TypeScript100%

Modules by API surface

packages/pglite-tools/src/wasi/easywasi.js73 symbols
packages/pglite/src/fs/opfs-ahp.ts60 symbols
packages/pg-protocol/src/messages.ts58 symbols
packages/pglite/src/worker/index.ts57 symbols
packages/pglite/src/fs/base.ts44 symbols
packages/pglite/src/pglite.ts33 symbols
packages/pglite-socket/src/index.ts27 symbols
packages/pg-protocol/src/parser.ts21 symbols
packages/pg-protocol/src/serializer.ts19 symbols
packages/pglite/src/interface.ts15 symbols
packages/pglite/src/base.ts15 symbols
packages/pg-protocol/test/testing/buffer-list.ts13 symbols

Dependencies from manifests, versioned

@changesets/cli2.27.9 · 1×
@codemirror/autocomplete6.18.1 · 1×
@codemirror/commands6.7.0 · 1×
@codemirror/lang-sql6.8.0 · 1×
@codemirror/view6.34.1 · 1×
@electric-sql/client1.0.0 · 1×
@electric-sql/experimental1.0.0 · 1×
@electric-sql/pg-protocolworkspace:* · 1×
@electric-sql/pgliteworkspace:* · 1×
@electric-sql/pglite-reactworkspace:* · 1×
@electric-sql/pglite-replworkspace:* · 1×

Datastores touched

electricDatabase · 1 repos
dbDatabase · 1 repos
postgresDatabase · 1 repos

For agents

$ claude mcp add pglite \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact