gopglite

package module
v0.0.0-...-b649918 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 28, 2026 License: MIT Imports: 21 Imported by: 0

README

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

Documentation

Overview

Package gopglite provides a database/sql driver for PGlite, an embedded PostgreSQL database compiled to WebAssembly.

This package uses wazero (a pure Go WebAssembly runtime) to run PGlite directly without requiring Node.js or any external processes.

Usage

To use gopglite with database/sql:

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

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

	// Use db as a standard *sql.DB
}

Data Directory Options

The data source name (DSN) specifies where to store the database:

  • ":memory:" - In-memory database (data lost when closed)
  • "/path/to/dir" - Persistent storage in the specified directory

Package gopglite implements the PostgreSQL wire protocol for PGlite.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

Conn implements driver.Conn for PGlite.

func (*Conn) Begin

func (c *Conn) Begin() (driver.Tx, error)

Begin implements driver.Conn.

func (*Conn) BeginTx

func (c *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error)

BeginTx implements driver.ConnBeginTx.

func (*Conn) Close

func (c *Conn) Close() error

Close implements driver.Conn.

func (*Conn) ExecContext

func (c *Conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error)

ExecContext implements driver.ExecerContext.

func (*Conn) Ping

func (c *Conn) Ping(ctx context.Context) error

Ping implements driver.Pinger.

func (*Conn) Prepare

func (c *Conn) Prepare(query string) (driver.Stmt, error)

Prepare implements driver.Conn.

func (*Conn) PrepareContext

func (c *Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error)

PrepareContext implements driver.ConnPrepareContext.

func (*Conn) QueryContext

func (c *Conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error)

QueryContext implements driver.QueryerContext.

type Connector

type Connector struct {
	// contains filtered or unexported fields
}

Connector manages a PGlite instance and provides connections to it.

func NewConnector

func NewConnector(dataDir string) (*Connector, error)

NewConnector creates a new Connector for the given data directory.

func (*Connector) Close

func (c *Connector) Close() error

Close shuts down the PGlite instance.

func (*Connector) Connect

func (c *Connector) Connect(ctx context.Context) (driver.Conn, error)

Connect implements driver.Connector.

func (*Connector) Driver

func (c *Connector) Driver() driver.Driver

Driver implements driver.Connector.

type DB

type DB struct {
	// contains filtered or unexported fields
}

DB represents a PGlite database instance.

func Open

func Open(userDataDir string) (*DB, error)

Open opens a PGlite database. The dataDir parameter is currently ignored - an in-memory temporary directory is used.

func (*DB) Close

func (db *DB) Close() error

func (*DB) Exec

func (db *DB) Exec(ctx context.Context, query string, args ...any) (*Result, error)

func (*DB) Query

func (db *DB) Query(ctx context.Context, query string, args ...any) (*Rows, error)

type Driver

type Driver struct{}

Driver implements database/sql/driver.Driver for PGlite.

func (*Driver) Open

func (d *Driver) Open(name string) (driver.Conn, error)

Open implements driver.Driver.

func (*Driver) OpenConnector

func (d *Driver) OpenConnector(name string) (driver.Connector, error)

OpenConnector implements driver.DriverContext.

type Result

type Result struct {
	// contains filtered or unexported fields
}

Result implements driver.Result.

func (*Result) LastInsertId

func (r *Result) LastInsertId() (int64, error)

func (*Result) RowsAffected

func (r *Result) RowsAffected() (int64, error)

type Rows

type Rows struct {
	// contains filtered or unexported fields
}

Rows implements driver.Rows.

func (*Rows) Close

func (r *Rows) Close() error

func (*Rows) Columns

func (r *Rows) Columns() []string

func (*Rows) Next

func (r *Rows) Next(dest []driver.Value) error

type Stmt

type Stmt struct {
	// contains filtered or unexported fields
}

Stmt implements driver.Stmt.

func (*Stmt) Close

func (s *Stmt) Close() error

func (*Stmt) Exec

func (s *Stmt) Exec(args []driver.Value) (driver.Result, error)

func (*Stmt) ExecContext

func (s *Stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error)

func (*Stmt) NumInput

func (s *Stmt) NumInput() int

func (*Stmt) Query

func (s *Stmt) Query(args []driver.Value) (driver.Rows, error)

func (*Stmt) QueryContext

func (s *Stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error)

type Tx

type Tx struct {
	// contains filtered or unexported fields
}

Tx implements driver.Tx.

func (*Tx) Commit

func (t *Tx) Commit() error

func (*Tx) Rollback

func (t *Tx) Rollback() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL