pglite README

The project's README from github.com/electric-sql/pglite. As one markdown file: README.md.

PGlite logo

PGlite - the WASM build of Postgres from Electric.
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 and PostGIS.

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 an input/output pathway that facilitates interaction with PostgreSQL when it is compiled to WASM within a JavaScript environment.

Limitations

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.

Sponsors

Big shoutout to everybody supporting us!

Blacksmith

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.

pglite (electric-sql/pglite)

Searching Database…

16.2K ·GitHub
这是什么?PGlite 是被编译成 WebAssembly 的 Postgres,以 TypeScript 客户端库的形式打包,所以一个真正的 Postgres 数据库可以直接在浏览器、Node.js、Bun 或 Deno 里运行——不需要任何其他依赖。
它是需要单独安装的 Postgres 服务器,还是我导入代码里的库?它是一个客户端库,导入到你的 JavaScript 或 TypeScript 应用里,Postgres 以 WASM 的形式运行在你自己的进程内——不涉及单独的服务器,也不需要 Linux 虚拟机。
我怎么把它加到我的项目里?用你常用的包管理器安装它,或者直接从 JSDelivr 这类 CDN 导入,然后在代码里创建一个实例,就是一个内存数据库。
我需要先安装 Postgres 或其他依赖吗?什么都不用装——WASM 版的 Postgres 就内置在这个库里,gzip 压缩后只有 3mb。
可以有多个客户端同时连接它吗?不行——PGlite 是单用户、单连接的,同一时间只能有一个客户端访问它。
在商业项目里使用它免费吗?免费——它同时以 Apache 2.0 和 PostgreSQL License 两种条款授权,你可以任选其一。
没有虚拟机,Postgres 是怎么在浏览器里跑起来的?Postgres 用 Emscripten 编译成了 WebAssembly。由于 WASM 程序无法 fork 进程,PGlite 是基于 Postgres 的单用户模式构建的,并通过一条 I/O 通道让 JavaScript 与它通信。
它适合做 local-first 或实时应用吗?适合——这正是它的定位:Electric 主推用 PGlite 直接在 Postgres 上构建响应式、实时的 local-first 应用,浏览器里的数据会通过 indexedDB 持久化保存在本地。
除了 pgvector 和 PostGIS,它还自带哪些扩展?仓库的构建脚本还会把 pgmq(消息队列)、pgTAP(测试)和 pg_uuidv7 打包成 WASM 扩展。
What are similar projects?kysely, supabase, zvec, turso, electric, rxdb, lancedb, perspective
Size
31.2 MB
Stars
16.2K
Age
2y
Commits · 90 Days