Skip to content

[BUG]: PGlite failed to initialize properly on v0.5.1 #1019

@seepine

Description

@seepine

Describe the bug

The same code runs normally in PGlite version 0.4.6, but throws an error after upgrading to version 0.5.1.
The error persists even when running in a brand new Docker environment with no residual old data, ruling out issues caused by legacy data.

To Reproduce - include code sample(s)

  1. add pglite.ts
import { PGlite, type PGliteOptions } from '@electric-sql/pglite'

// @ts-ignore
import wasmPath from 'node_modules/@electric-sql/pglite/dist/pglite.wasm' with {
  type: 'file',
  embed: 'true',
}

// @ts-ignore
import initdbWasmPath from 'node_modules/@electric-sql/pglite/dist/initdb.wasm' with {
  type: 'file',
  embed: 'true',
}

// @ts-ignore
import dataPath from 'node_modules/@electric-sql/pglite/dist/pglite.data' with {
  type: 'file',
  embed: 'true',
}

export async function createPGlite(dataDir: string, options?: PGliteOptions): Promise<PGlite> {
  // Read the embedded files
  const [wasmBuffer, initdbBuffer, dataBuffer] = await Promise.all([
    Bun.file(wasmPath).arrayBuffer(),
    Bun.file(initdbWasmPath).arrayBuffer(),
    Bun.file(dataPath).arrayBuffer(),
  ])
  // Create PGlite instance with pre-loaded modules
  const db = await PGlite.create(dataDir, {
    ...options,
    pgliteWasmModule: await WebAssembly.compile(wasmBuffer), // not `wasmModule`
    initdbWasmModule: await WebAssembly.compile(initdbBuffer),
    fsBundle: new Blob([dataBuffer]),
  })
  return db
}
  1. index.ts
import { migrate as pgliteMigrate } from 'drizzle-orm/pglite/migrator'
import * as schema from './schema'

const client = await createPGlite(".pglite")
const db = pgliteDriver({ client, schema, casing: 'snake_case' })
await pgliteMigrate(db, { migrationsFolder: './drizzle' })
  1. Dockerfile
FROM oven/bun:alpine AS build
COPY package.json bun.lock bunfig.toml ./
RUN bun ci
COPY ./ ./
RUN bun build --compile index.ts --outfile dist/app

FROM alpine:3.23
WORKDIR /work
RUN mkdir /data
RUN apk add --no-cache libstdc++ 
ENV DATABASE_URL="/data/pglite.data"
COPY --from=build /work/dist/app /work/app
COPY --from=build /work/drizzle /work/drizzle
ENTRYPOINT ["./app"]

Logs

server-1  | 75484 |   if (h(this, Me3))
server-1  | 75485 |     throw new Error("PGlite single mode already running");
server-1  | 75486 |   this.mod._pgl_startPGlite(), x(this, Me3, true);
server-1  | 75487 | }, st2 = function(r) {
server-1  | 75488 |   let a2 = [...r.startParams, "-D", r.pgDataFolder, this.mod.ENV.PGDATABASE];
server-1  | 75489 |     throw new Error("PGlite failed to initialize properly");
server-1  |                   ^
server-1  | error: PGlite failed to initialize properly
server-1  |       at st2 (/$bunfs/root/index.js:75489:11)
server-1  |       at <anonymous> (/$bunfs/root/index.js:75381:166)
server-1  |       at async create (/$bunfs/root/index.js:75129:20)
server-1  |       at async createPGlite (/$bunfs/root/index.js:75711:30)
server-1  | 
server-1  | Bun v1.3.14 (Linux arm64)

Details

  • PGlite version: 0.5.1
  • OS version: docker of alpine
  • node, bun, deno or browser version: bun

Additional context
Add any other context about the problem here.

Other
If you want to help fix this, you might want to try debugging the issue yourself! Follow the instructions here to build a debug version of PGlite which you can then use in an interactive debugging session.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions