0.2.12 • Published 1 month ago

@electric-sql/pglite v0.2.12

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
1 month ago

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.

How to contribute

You will need pnpm installed, and a recent version of Node.js (v20 and above).

You will also need the Postgres WASM build files, which you download from a comment under the most recently merged PR, labeled as interim build files, and place them under packages/pglite/release. These are necessary to build PGlite and the dependent workspace projects. We plan to enable a local build in the future to streamline this step.

Once the requirements are met, you can install dependencies and build the workspace projects:

pnpm install
pnpm 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.

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.

0.2.0-alpha.8

4 months ago

0.2.0-alpha.7

4 months ago

0.2.0-alpha.9

4 months ago

0.2.0-alpha.0

4 months ago

0.2.0-alpha.2

4 months ago

0.2.0-alpha.1

4 months ago

0.2.0-alpha.4

4 months ago

0.2.0-alpha.3

4 months ago

0.2.0-alpha.6

4 months ago

0.2.0-alpha.5

4 months ago

0.2.12

1 month ago

0.2.11

2 months ago

0.2.10

2 months ago

0.2.1

3 months ago

0.2.0

3 months ago

0.2.7

2 months ago

0.2.6

2 months ago

0.2.9

2 months ago

0.2.8

2 months ago

0.2.3

3 months ago

0.2.2

3 months ago

0.2.5

3 months ago

0.2.4

3 months ago

0.1.5

7 months ago

0.1.4

7 months ago

0.1.3

7 months ago

0.1.2

8 months ago

0.1.1

8 months ago

0.1.0

8 months ago

0.0.2

9 months ago

0.0.1

9 months ago

0.0.1-debug.2

9 months ago

0.0.1-alpha.2

9 months ago

0.0.1-alpha.1

9 months ago

0.0.1-alpha.0

9 months ago

0.0.0-alpha.2

9 months ago

0.0.0-alpha.1

9 months ago

0.0.0-alpha.0

9 months ago