Guide

Server instance

usePGlite() in your Nitro routes, configured in server/pglite.config.ts.

The server side is enabled by default. usePGlite() is auto-imported in your server code and resolves to a single PGlite instance, created on first use and closed when the server shuts down.

server/api/hello.get.ts
export default defineEventHandler(async () => {
  const pg = await usePGlite()
  return pg.query('SELECT now()')
})

Configuring it

Extensions, init and anything else that is code go in server/pglite.config.ts; build-time options that fit in JSON, such as the data directory, in nuxt.config.ts.

Data directory

The data lives in .data/pglite under your project unless you say otherwise. There are three places to set it, from the most static to the most dynamic:

WhereResolved fromUse it for
pglite.server.options.dataDir in nuxt.config.tsthe project root, at build timea path inside the project
dataDir in server/pglite.config.tsthe working directory, at runtimea path decided by code
NUXT_PGLITE_DATA_DIRthe working directory, at runtimea path chosen at deploy time; wins over both

PGlite's own schemes (memory://, …) are passed through untouched.

Disabling the server side

nuxt.config.ts
export default defineNuxtConfig({
  pglite: {
    server: { enabled: false },
  },
})

Nothing of PGlite is then in the server bundle. #pglite/server still resolves, to a stub whose usePGlite() rejects with a pointer, so a leftover import fails clearly at runtime rather than at bundling. The development socket keeps working: that is the PGlite in dev, Postgres in prod recipe.