{"library":"pglite-server","title":"PGlite Wire Protocol Server","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install pglite-server"],"cli":null},"imports":["import { createServer } from 'pglite-server';","import { LogLevel } from 'pglite-server';","import { PGlite } from '@electric-sql/pglite';","import { Client } from 'pg';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { PGlite } from \"@electric-sql/pglite\";\nimport { Client } from \"pg\";\nimport { createServer } from \"pglite-server\";\n\n// Define the port for the PostgreSQL server\nconst PORT = 5432;\n\nasync function runExample() {\n  // Initialize PGlite database\n  const db = new PGlite();\n  // Wait for the PGlite instance to be ready\n  await db.waitReady;\n\n  // Execute initial schema and data creation\n  await db.exec(`\n    create table if not exists test (id serial primary key, name text);\n    insert into test (name) values ('foo'), ('bar'), ('baz');\n  `);\n\n  // Create the pglite-server instance, passing the PGlite database\n  const pgServer = createServer(db);\n\n  // Start listening on the defined port\n  pgServer.listen(PORT, async () => {\n    console.log(`pglite-server bound to port ${PORT}`);\n\n    // Connect using node-postgres client\n    const client = new Client({\n      host: \"localhost\",\n      port: PORT,\n      database: \"postgres\", // Default database name\n      user: \"postgres\"     // Default user\n    });\n\n    try {\n      await client.connect();\n      console.log(\"Connected to pglite-server via node-postgres client.\");\n\n      const res = await client.query(\"select * from test\");\n      console.log(\"Query result:\", res.rows);\n\n      // Clean up the client connection\n      await client.end();\n      console.log(\"node-postgres client disconnected.\");\n\n      // Stop the pglite-server\n      pgServer.close(() => {\n        console.log(\"pglite-server closed.\");\n      });\n    } catch (error) {\n      console.error(\"Error during client connection or query:\", error);\n      // Ensure server is closed even if client fails\n      pgServer.close(() => {\n        console.log(\"pglite-server closed due to error.\");\n      });\n    }\n  });\n}\n\nrunExample().catch(console.error);","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}