Skip to content

bradfitz/gopglite

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gopglite ALPHA ALPHA WIP EXPERIMENT

A Go database/sql/driver for PGlite, an embedded PostgreSQL compiled to WebAssembly.

DO NOT USE -- this is largely an AI-generated experiment.

Features

  • Pure Go - Uses wazero as the WebAssembly runtime (no cgo required)
  • Embedded PostgreSQL - Full PostgreSQL 16 running in-process
  • Standard database/sql interface - Works with existing Go database code
  • PL/pgSQL support - Full procedural language support
  • Extensions - Includes encoding converters, snowball text search, etc.

Installation

go get github.com/bradfitz/gopglite

Usage

package main

import (
    "database/sql"
    "fmt"
    _ "github.com/bradfitz/gopglite"
)

func main() {
    db, err := sql.Open("pglite", ":memory:")
    if err != nil {
        panic(err)
    }
    defer db.Close()

    // Create a table
    _, err = db.Exec(`CREATE TABLE users (
        id SERIAL PRIMARY KEY,
        name TEXT NOT NULL
    );`)
    if err != nil {
        panic(err)
    }

    // Insert data
    _, err = db.Exec(`INSERT INTO users (name) VALUES ('Alice');`)
    if err != nil {
        panic(err)
    }

    // Query data back
    rows, err := db.Query(`SELECT id, name FROM users;`)
    if err != nil {
        panic(err)
    }
    defer rows.Close()

    for rows.Next() {
        var id int64
        var name string
        if err := rows.Scan(&id, &name); err != nil {
            panic(err)
        }
        fmt.Printf("id=%d, name=%s\n", id, name)
    }
    // Output: id=1, name=Alice
}

Status

Working:

  • Opening databases
  • Executing DDL (CREATE TABLE, etc.)
  • Executing DML (INSERT, UPDATE, DELETE)
  • Reading query results (via PostgreSQL wire protocol)
  • Transactions (BEGIN, COMMIT, ROLLBACK)
  • PL/pgSQL functions
  • PostgreSQL extensions

Future:

  • Prepared statements with parameter binding (currently uses query string formatting)

How it Works

This library embeds a WASI build of PGlite (PostgreSQL compiled to WebAssembly) and runs it using the wazero runtime. The WASI build includes:

  • The PostgreSQL server binary (postgres.wasi)
  • A pre-initialized data directory with system catalogs
  • Extension libraries (encoding converters, text search, etc.)

When you open a database, the library:

  1. Extracts the embedded PGlite environment to a temp directory
  2. Creates a wazero runtime with WASI support
  3. Mounts the filesystem directories
  4. Calls pg_initdb to initialize/resume the database
  5. Executes queries via the interactive_one function

Credits

License

MIT License - see LICENSE

About

ALPHA ALPHA AI EXPERIMENT -- Go bindings for pglite (Postgres wasm)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors