Requires: libpq (auto-installed via minks install kse_pg)
#include "kse_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);
// test_pg.ks - PostgreSQL plugin tests
// Requires a running PostgreSQL server.
// Update the connection details below to match your setup.
//
// After installing:
// minks install kse_pg (run once from inside pg_plugin/)
// minks test_pg.ks
//
// Without installing:
// minks --plugin-path . test_pg.ks
#include "kse_pg"
Konsol:Print("=== PostgreSQL Plugin Test ===");
Konsol:Print("");
Var:Number db;
try {
PG:Connect("localhost", "postgres", "secret", "mydb", db);
} catch (PGException e) {
Konsol:Print("Connect failed: ${e.message}");
Konsol:Exit(1);
}
Konsol:Print("Connected, handle: ${db}");
Konsol:Print("");
Var:Boolean ok;
Var:String rows;
Var:String row;
Var:Number num;
Var:String errMsg;
// -- Setup --------------------------------------------------------------------
PG:Exec(db, "DROP TABLE IF EXISTS ks_test_users", ok);
PG:Exec(db, "CREATE TABLE ks_test_users (id SERIAL PRIMARY KEY, name TEXT, score INTEGER)", ok);
Konsol:Print("Table created: ${ok}");
Konsol:Print("");
// -- Insert / LastInsertId / RowsAffected ------------------------------------
Konsol:Print("--- Insert");
PG:Exec(db, "INSERT INTO ks_test_users (name, score) VALUES ('Alice', 95)", ok);
PG:LastInsertId(db, num);
Konsol:Print("Insert Alice: ${ok}, lastId: ${num}");
PG:Exec(db, "INSERT INTO ks_test_users (name, score) VALUES ('Bob', 80)", ok);
PG:LastInsertId(db, num);
Konsol:Print("Insert Bob: ${ok}, lastId: ${num}");
PG:Exec(db, "INSERT INTO ks_test_users (name, score) VALUES ('Carol', 88)", ok);
PG:LastInsertId(db, num);
Konsol:Print("Insert Carol: ${ok}, lastId: ${num}");
Konsol:Print("");
// -- Query / QueryOne --------------------------------------------------------
Konsol:Print("--- Query");
PG:Query(db, "SELECT * FROM ks_test_users ORDER BY id", rows);
Konsol:Print(rows);
Konsol:Print("");
Konsol:Print("--- QueryOne");
PG:QueryOne(db, "SELECT * FROM ks_test_users WHERE name = 'Alice'", row);
Konsol:Print(row);
Konsol:Print("");
PG:QueryOne(db, "SELECT * FROM ks_test_users WHERE name = 'Nobody'", row);
Konsol:Print("QueryOne (missing): '${row}'");
Konsol:Print("");
// -- Update / RowsAffected ---------------------------------------------------
Konsol:Print("--- Update / RowsAffected");
PG:Exec(db, "UPDATE ks_test_users SET score = score + 5 WHERE score < 90", ok);
PG:RowsAffected(db, num);
Konsol:Print("Updated ${num} row(s)");
PG:Query(db, "SELECT * FROM ks_test_users ORDER BY id", rows);
Konsol:Print(rows);
Konsol:Print("");
// -- Delete ------------------------------------------------------------------
Konsol:Print("--- Delete");
PG:Exec(db, "DELETE FROM ks_test_users WHERE name = 'Bob'", ok);
PG:RowsAffected(db, num);
Konsol:Print("Deleted ${num} row(s)");
Konsol:Print("");
// -- Error -------------------------------------------------------------------
Konsol:Print("--- Error");
PG:Exec(db, "INSERT INTO ks_test_users (nonexistent_col) VALUES (1)", ok);
Konsol:Print("Bad insert ok: ${ok}");
PG:Error(db, errMsg);
Konsol:Print("Error: ${errMsg}");
Konsol:Print("");
// -- Cleanup / Close ---------------------------------------------------------
PG:Exec(db, "DROP TABLE ks_test_users", ok);
Konsol:Print("Table dropped: ${ok}");
PG:Close(db);
Konsol:Print("");
Konsol:Print("=== done ===");
| 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"