A Go database/sql/driver for PGlite, an embedded PostgreSQL compiled to WebAssembly.
DO NOT USE -- this is largely an AI-generated experiment.
- 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.
go get github.com/bradfitz/gopglitepackage 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
}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)
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:
- Extracts the embedded PGlite environment to a temp directory
- Creates a wazero runtime with WASI support
- Mounts the filesystem directories
- Calls
pg_initdbto initialize/resume the database - Executes queries via the
interactive_onefunction
- PGlite - The PostgreSQL WASM build by ElectricSQL
- pglite-bindings - Language bindings for PGlite
- pglite-go - Initial Go proof-of-concept
- wazero - Pure Go WebAssembly runtime
MIT License - see LICENSE