This directory contains the pg_kalam PostgreSQL extension.
The extension is built with pgrx and targets PostgreSQL 16 through the pg16 feature.
CREATE EXTENSION pg_kalam;- A
pg_kalamforeign data wrapper registered by the extension install SQL - A PostgreSQL-side bridge to a running KalamDB server over gRPC
- Testing and development on the same machine: build and install natively with
cargo pgrx install - Building a Linux image or using macOS with Dockerized PostgreSQL: compile with
./pg/docker/build-fast.shor./pg/docker/build.sh - Production install into a Linux PostgreSQL server: build Linux artifacts in Docker, then copy
pg_kalam.so,pg_kalam.control, andpg_kalam--*.sqlinto that server's PostgreSQL directories - Running both KalamDB and PostgreSQL together: use
cd pg/docker && docker compose up -d
- Rust toolchain compatible with this workspace
cargo-pgrxversion0.16.1- PostgreSQL 16 server and development headers
- A working
pg_configfor the PostgreSQL 16 instance you want to install into
- Docker Desktop on macOS, or Docker Engine on Linux
- Enough memory for a Rust build inside Docker
Run these commands from the repository root.
cargo install cargo-pgrx --version "=0.16.1" --locked
cargo pgrx init --pg16 "$(command -v pg_config)"
cargo pgrx install \
-p kalam-pg-extension \
-c "$(command -v pg_config)" \
--no-default-features \
--profile release-pg \
-F pg16What cargo pgrx install does:
- builds the extension as
pg_kalam - copies
pg_kalam.sointo PostgreSQL's library directory - copies
pg_kalam.controland the generated upgrade SQL into PostgreSQL's extension directory
After changing extension code, rerun the same cargo pgrx install command.
The PostgreSQL extension talks to a running KalamDB server. Start one before creating the foreign server.
cd backend
cp server.example.toml server.toml
KALAMDB_SERVER_HOST=0.0.0.0 \
KALAMDB_JWT_SECRET="replace-with-a-32-char-secret" \
cargo run --bin kalamdb-serverDefault endpoints:
- HTTP API:
http://127.0.0.1:8080 - PostgreSQL extension gRPC target: configure the host and port you expose for KalamDB
If you need a containerized KalamDB server instead of a local Rust process, use one of the Docker options below.
Once the extension artifacts are installed into the target PostgreSQL instance, connect as a superuser and run:
CREATE EXTENSION IF NOT EXISTS pg_kalam;Then create the remote server definition:
CREATE SERVER kalam_server
FOREIGN DATA WRAPPER pg_kalam
OPTIONS (
host '127.0.0.1',
port '9188',
auth_header 'Bearer <your-kalamdb-token-or-shared-secret>'
);Verify the extension loaded correctly:
SELECT pg_kalam_version(), pg_kalam_compiled_mode();If you need to reinstall during development:
DROP EXTENSION IF EXISTS pg_kalam CASCADE;
CREATE EXTENSION pg_kalam;Use this flow when:
- PostgreSQL runs in Docker
- you are on macOS but need Linux extension artifacts
- you want a repeatable Linux build for testing or packaging
From the repository root:
./pg/docker/build-fast.shUseful variants:
./pg/docker/build-fast.sh --compile
./pg/docker/build-fast.sh --runtime
./pg/docker/build-fast.sh --rebuild-baseWhat this does:
- builds the extension inside a Linux builder container
- caches Cargo registry, git, and target directories in Docker volumes
- writes installable artifacts to
pg/docker/artifacts/ - builds the
kalamdb-pg:latestruntime image
If you want a clean multi-stage build without the cached builder flow:
./pg/docker/build.shEquivalent raw Docker command:
docker build -f pg/docker/Dockerfile -t kalamdb-pg:latest .After building the image, start the full stack:
cd pg/docker
docker compose up -dThis compose stack starts:
kalamdbon host ports8088and9188postgreswith thepg_kalamextension image on host port5433
On first startup, pg/docker/init.sql automatically runs:
CREATE EXTENSION IF NOT EXISTS pg_kalam;CREATE SERVER IF NOT EXISTS kalam_server ...
Connect with:
psql "postgresql://kalamdb:kalamdb123@127.0.0.1:5433/kalamdb"Then verify:
SELECT pg_kalam_version(), pg_kalam_compiled_mode();
\des+For production, treat the extension artifacts as platform-specific binaries.
Important constraints:
- the extension must match the PostgreSQL major version of the target server
- the extension must match the target OS and CPU architecture
- a
.sobuilt on macOS cannot be loaded into Linux PostgreSQL
Build Linux artifacts in Docker and install those into the target Linux PostgreSQL instance.
From the repository root:
./pg/docker/build-fast.sh --compileThis produces:
pg/docker/artifacts/pg_kalam.sopg/docker/artifacts/pg_kalam.controlpg/docker/artifacts/pg_kalam--*.sql
Install them into the target PostgreSQL server:
install -m 755 pg/docker/artifacts/pg_kalam.so "$(pg_config --pkglibdir)/pg_kalam.so"
install -m 644 pg/docker/artifacts/pg_kalam.control "$(pg_config --sharedir)/extension/pg_kalam.control"
install -m 644 pg/docker/artifacts/pg_kalam--*.sql "$(pg_config --sharedir)/extension/"Then restart PostgreSQL if required by your environment and run:
CREATE EXTENSION IF NOT EXISTS pg_kalam;If you want PostgreSQL with the extension already installed in a container, build and run the provided runtime image:
./pg/docker/build-fast.sh
docker run --name kalamdb-pg \
-e POSTGRES_USER=kalamdb \
-e POSTGRES_PASSWORD=kalamdb123 \
-e POSTGRES_DB=kalamdb \
-p 5433:5432 \
-d kalamdb-pg:latestThen connect and install the extension or let your own init SQL handle it.
If PostgreSQL is running elsewhere and you only need a KalamDB server for the extension to connect to, you can run the server container by itself.
From the repository root:
docker build -f docker/build/Dockerfile -t kalamdb:local .docker run -d \
--name kalamdb \
-p 8080:8080 \
-p 9188:9188 \
-e KALAMDB_SERVER_HOST=0.0.0.0 \
-e KALAMDB_JWT_SECRET="replace-with-a-32-char-secret" \
-e KALAMDB_ALLOW_REMOTE_SETUP=true \
-e KALAMDB_SECURITY_TRUSTED_PROXY_RANGES="10.0.0.0/8,172.16.0.0/12,192.168.0.0/16" \
-e KALAMDB_NODE_ID=1 \
-e KALAMDB_CLUSTER_RPC_ADDR=0.0.0.0:9188 \
-e KALAMDB_CLUSTER_API_ADDR=http://127.0.0.1:8080 \
-v kalamdb_data:/data \
kalamdb:localVerify the container is healthy:
curl http://127.0.0.1:8080/v1/api/healthcheckIf PostgreSQL runs in Docker on the same host, use host.docker.internal as the FDW host value when the PostgreSQL container connects back to a KalamDB server running on macOS.
If you already have a KalamDB server running elsewhere, you can run just the PostgreSQL container:
docker run --name kalamdb-pg \
-e POSTGRES_USER=kalamdb \
-e POSTGRES_PASSWORD=kalamdb123 \
-e POSTGRES_DB=kalamdb \
-p 5433:5432 \
-d kalamdb-pg:latestThen connect:
psql "postgresql://kalamdb:kalamdb123@127.0.0.1:5433/kalamdb"And install the extension manually:
CREATE EXTENSION IF NOT EXISTS pg_kalam;
CREATE SERVER kalam_server
FOREIGN DATA WRAPPER pg_kalam
OPTIONS (
host 'host.docker.internal',
port '9188',
auth_header 'Bearer <your-kalamdb-token-or-shared-secret>'
);could not access file "$libdir/pg_kalam": the extension was not installed into the same PostgreSQL instance you are usingextension "pg_kalam" is not available: the.controland SQL files are missing from PostgreSQL's extension directoryincompatible library: the extension was built for the wrong OS, CPU architecture, or PostgreSQL major versionKalamDB server is not running or unreachable ...: verify the KalamDB container or process is up and that the FDWhostandportare correct- gRPC connection problems in Docker: for a PostgreSQL container connecting to a host process on macOS, use
host.docker.internalinstead of127.0.0.1