Registry / database / pglite-server

pglite-server

JSON →
library0.1.5jsnpmunverified

pglite-server provides a PostgreSQL wire protocol server, enabling standard PostgreSQL clients and tools (like `psql` or the `node-postgres` client) to connect to an in-memory or file-backed PGlite instance. It acts as a proxy, intercepting `SSLRequest` and `StartupMessage` for initial handshake and then forwarding all subsequent traffic to the underlying PGlite database. As of version 0.1.5, it is in early development, with an irregular, feature-driven release cadence. Its key differentiator is bridging existing PostgreSQL tooling with PGlite's WASM-based database, offering a quick way to establish a temporary, compliant PostgreSQL endpoint for testing or development. For more versatile and feature-rich requirements, the README suggests considering `pg-gateway` from Supabase.

npm install pglite-server
INSTALL
IMPORT
SIG · PGLITE-SERVER
P
pglite-server
databasejavascriptv0.1.5
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createServer
import { createServer } from 'pglite-server';
const { createServer } = require('pglite-server');
This package is ESM-first, CommonJS `require` syntax is not the recommended way.
LogLevel
import { LogLevel } from 'pglite-server';
const { LogLevel } = require('pglite-server');
Used for configuring server logging verbosity.
PGlite
import { PGlite } from '@electric-sql/pglite';
This is the peer dependency that `pglite-server` connects to. It must be installed separately.
Client
import { Client } from 'pg';
Used for connecting to the `pglite-server` instance from a standard Node.js PostgreSQL client like `node-postgres`.

Demonstrates how to set up a `pglite-server` instance, initialize a `PGlite` database, and connect to it using the `node-postgres` client, performing a simple query.

import { PGlite } from "@electric-sql/pglite"; import { Client } from "pg"; import { createServer } from "pglite-server"; // Define the port for the PostgreSQL server const PORT = 5432; async function runExample() { // Initialize PGlite database const db = new PGlite(); // Wait for the PGlite instance to be ready await db.waitReady; // Execute initial schema and data creation await db.exec(` create table if not exists test (id serial primary key, name text); insert into test (name) values ('foo'), ('bar'), ('baz'); `); // Create the pglite-server instance, passing the PGlite database const pgServer = createServer(db); // Start listening on the defined port pgServer.listen(PORT, async () => { console.log(`pglite-server bound to port ${PORT}`); // Connect using node-postgres client const client = new Client({ host: "localhost", port: PORT, database: "postgres", // Default database name user: "postgres" // Default user }); try { await client.connect(); console.log("Connected to pglite-server via node-postgres client."); const res = await client.query("select * from test"); console.log("Query result:", res.rows); // Clean up the client connection await client.end(); console.log("node-postgres client disconnected."); // Stop the pglite-server pgServer.close(() => { console.log("pglite-server closed."); }); } catch (error) { console.error("Error during client connection or query:", error); // Ensure server is closed even if client fails pgServer.close(() => { console.log("pglite-server closed due to error."); }); } }); } runExample().catch(console.error);
Debug
Known issues
gotchaThis package is an early-stage project (v0.1.5), primarily a spare-time attempt to understand the Postgres Wire Protocol. While functional, it might not be suitable for production environments requiring robust error handling or advanced features. For more feature-rich solutions, consider the recommended `pg-gateway` from Supabase.
fix
Evaluate alternatives like `pg-gateway` for production use cases, or contribute to `pglite-server` to enhance its capabilities.
affects: >=0.1.0
breaking`pglite-server` requires Node.js version 20 or higher due to its reliance on modern Node.js APIs and language features.
fix
Upgrade your Node.js environment to version 20 or later.
affects: >=0.1.0
gotchaThe `@electric-sql/pglite` package is a peer dependency and must be explicitly installed alongside `pglite-server` for the server to function correctly.
fix
Install `@electric-sql/pglite` using `npm install @electric-sql/pglite` or `yarn add @electric-sql/pglite`.
affects: >=0.1.0
Errors
Common errors & fixes
Error: connect ECONNREFUSED 127.0.0.1:5432
The `pglite-server` instance is either not running, not listening on the specified port, or another process is already using the port.
fix
Ensure `pgServer.listen(PORT)` has been successfully called and the server is actively listening before attempting to connect. Check for port conflicts.
TypeError: Cannot read properties of undefined (reading 'exec')
The `PGlite` database instance was not fully initialized before attempting to perform operations.
fix
Always include `await db.waitReady;` immediately after `new PGlite()` to ensure the database is ready for queries.
Error: Cannot find module '@electric-sql/pglite' or its corresponding type declarations.
The required peer dependency `@electric-sql/pglite` is not installed in your project.
fix
Install the peer dependency: `npm install @electric-sql/pglite`.
Upgrade
Version history
0.1.5latest on npm
Audit
Dependencies
@electric-sql/pgliterequiredRequired runtime database engine for the server to proxy requests to.
Agent activity
2 hits · last 30 days
node
2
Resources
pglite-server — npm install pglite-server · libregistry