Server instance
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.
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:
| Where | Resolved from | Use it for |
|---|---|---|
pglite.server.options.dataDir in nuxt.config.ts | the project root, at build time | a path inside the project |
dataDir in server/pglite.config.ts | the working directory, at runtime | a path decided by code |
NUXT_PGLITE_DATA_DIR | the working directory, at runtime | a path chosen at deploy time; wins over both |
PGlite's own schemes (memory://, …) are passed through untouched.
Disabling the server side
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.