pg — PostgreSQL client

Requires: libpq (auto-installed via minks install pg)

#include "pg"

Query results are returned as a JSON string — parse with the Json module.


Method Args Out Description
PG:Connect host, user, pass, db Number Connect; returns handle. Throws PGException on failure
PG:ConnectPort host, port, user, pass, db Number Connect with explicit port
PG:ConnectUrl connStr Number Connect via libpq connection string (e.g. "host=... dbname=...")
PG:Close handle void Close the connection
PG:Exec handle, sql Boolean Run DDL / DML; true = success
PG:Query handle, sql String SELECT → JSON array of row objects
PG:QueryOne handle, sql String SELECT → first row as JSON object, or ""
PG:RowsAffected handle Number Rows changed by last Exec
PG:LastInsertId handle Number Last value from any sequence used in the session (SELECT lastval())
PG:Error handle String Last error message, "" if none

Throws PGException on connection failure. See Object — Exception.

Var:Number db;
Var:Boolean ok;
Var:String rows;
Var:Number lastId;

try {
    PG:Connect("localhost", "postgres", "secret", "mydb", db);
} catch (PGException e) {
    Konsol:Print("connect failed: ${e.message}");
}

PG:Exec(db, "CREATE TABLE IF NOT EXISTS users (id SERIAL PRIMARY KEY, name TEXT)", ok);
PG:Exec(db, "INSERT INTO users (name) VALUES ('Alice')", ok);
PG:LastInsertId(db, lastId);
Konsol:Print("inserted id: ${lastId}");

PG:Query(db, "SELECT * FROM users ORDER BY id", rows);
Konsol:Print(rows);   // [{"id":1,"name":"Alice"}]

PG:Close(db);

Building from source

Platform Command
Debian/Ubuntu sudo apt install libpq-dev
Fedora/RHEL sudo dnf install libpq-devel
macOS brew install libpq
MSYS2 MINGW64 pacman -S mingw-w64-x86_64-postgresql
MSYS2 UCRT64 pacman -S mingw-w64-ucrt-x86_64-postgresql
MSYS2 CLANG64 pacman -S mingw-w64-clang-x86_64-postgresql
MSYS2 CLANGARM64 pacman -S mingw-w64-clang-aarch64-postgresql

The Makefile auto-detects via pg_config. For native Windows, install PostgreSQL and override PG_DIR if not using the default path (C:/Program Files/PostgreSQL/16):

make -C pg_plugin PG_DIR="C:/your/install/path"