Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Embeddable Postgres with real-time, reactive bindings.

License

NotificationsYou must be signed in to change notification settings

electric-sql/pglite

Repository files navigation

ElectricSQL logo

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

License - Apache 2.0Status - AlphaChat - 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, includingpgvector.

import{PGlite}from"@electric-sql/pglite";constdb=newPGlite();awaitdb.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 seepglite.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:

constdb=newPGlite()awaitdb.query("select 'Hello world' as message;")// -> { rows: [ { message: "Hello world" } ] }

or to persist the database to indexedDB:

constdb=newPGlite("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";constdb=newPGlite();awaitdb.query("select 'Hello world' as message;");// -> { rows: [ { message: "Hello world" } ] }

or to persist to the filesystem:

constdb=newPGlite("./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) andpnpm 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/pglitecd pglitepnpm install

To build everything, we have the convenientpnpm 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.

Toonly 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 commentInterim build files:. Extract the files and place them underpackages/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 thebuild andtest scripts, as well as thestylecheck andtypecheck scripts to ensure style and type validity.

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

cd packages/pglitepnpm 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 ofStas Kelvich ofNeon in thisPostgres 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.


[8]ページ先頭

©2009-2025 Movatter.jp