-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
322 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,37 @@ | ||
# What is PGlite | ||
# What is PGlite | ||
|
||
PGlite is a WASM Postgres build packaged into a TypeScript/JavaScript client library that enables you to run Postgres in the browser, Node.js and Bun, with no need to install any other dependencies. It's under 3mb gzipped, and has support for many [Postgres extensions](../extensions/), including [pgvector](../extensions/#pgvector). | ||
|
||
Unlike previous "Postgres in the browser" projects, PGlite does not use a Linux virtual machine - it is simply Postgres in WASM. | ||
|
||
It's being developed by [ElectricSQL](https://electric-sql.com/) for our use case of embedding into applications, either locally or at the edge, allowing users to sync a subset of their Postgres database. | ||
|
||
However, there are many more use cases for PGlite beyond it's use as an embedded application databases: | ||
|
||
- Unit and CI testing<br> | ||
PGlite is very fast to start and tare down, perfect for unit tests, you can a unique fresh Postgres for each test. | ||
|
||
- Local development<br> | ||
You can use PGlite as an alternative to a full local Postgres for local development, masivly simplifyinf your development environmant. | ||
|
||
- Remote development, or local web containers<br> | ||
As PGlite is so light weight it can be easily embedded into remote containerised development environments, or in-browser [web containers](https://webcontainers.io). | ||
|
||
- On-device or edge AI and RAG<br> | ||
PGlite has full support for [pgvector](../extensions/#pgvector), enabling a local or edge retrieval augmented generation (RAG) workflow. | ||
|
||
We are very keen to establish PGlite as an open source, and open contribution, project, working to build a community around it to develop its capabilities for all use cases. | ||
|
||
Getting started with PGlite is super easy, just install and import the NPM package, then create a your embded database: | ||
|
||
```js | ||
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). | ||
|
||
Read more in our [getting started guide](./index.md). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>PGlite Dump Datadir Example</title> | ||
<link rel="stylesheet" href="./styles.css" /> | ||
<script src="./utils.js"></script> | ||
<script type="importmap"> | ||
{ | ||
"imports": { | ||
"@electric-sql/pglite": "../dist/index.js" | ||
} | ||
} | ||
</script> | ||
</head> | ||
<body> | ||
<h1>PGlite Dump Datadir Example</h1> | ||
<div class="script-plus-log"> | ||
<script type="module" src="./dumpDataDir.js"></script> | ||
<div id="log"></div> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { PGlite } from "../dist/index.js"; | ||
|
||
const pg = new PGlite(); | ||
await pg.exec(` | ||
CREATE TABLE IF NOT EXISTS test ( | ||
id SERIAL PRIMARY KEY, | ||
name TEXT | ||
); | ||
`); | ||
await pg.exec("INSERT INTO test (name) VALUES ('test');"); | ||
|
||
const file = await pg.dumpDataDir(); | ||
|
||
if (typeof window !== "undefined") { | ||
// Download the dump | ||
const url = URL.createObjectURL(file); | ||
const a = document.createElement("a"); | ||
a.href = url; | ||
a.download = file.name; | ||
a.click(); | ||
} else { | ||
// Save the dump to a file using node fs | ||
const fs = await import("fs"); | ||
fs.writeFileSync(file.name, await file.arrayBuffer()); | ||
} | ||
|
||
const pg2 = new PGlite({ | ||
loadDataDir: file, | ||
}); | ||
|
||
const rows = await pg2.query("SELECT * FROM test;"); | ||
console.log(rows); |
Oops, something went wrong.