pgnode
Local LLM (Ollama) + PostgreSQL Agent
An offline-first AI agent that converts natural language into validated SQL queries and executes them safely on your PostgreSQL database.
Overview
pgnode is a local AI-powered database operator. It connects to your PostgreSQL instance and allows you to interact with your data using plain English while ensuring safety, control, and privacy.
- No external APIs
- No data leaves your system
- Fully local using Ollama
Core Features
- Natural language → SQL conversion
- Safe query execution with validation layer
- Schema-aware query generation
- Works with existing PostgreSQL databases (pgAdmin compatible)
- CLI-first interface (fast and developer-friendly)
- Fully offline with local LLM
Architecture
User Prompt
↓
Agent (planner)
↓
SQL Generator (LLM)
↓
Validator (safety layer)
↓
Query Executor (PostgreSQL)
↓
Response
Tech Stack
Core
- Python
- PostgreSQL
- Ollama (local LLM runtime)
Libraries
- SQLAlchemy → DB interaction
- psycopg2 → PostgreSQL adapter
- Typer → CLI interface
- FastAPI (optional) → API layer
- LlamaIndex / FAISS (optional) → schema-aware retrieval
Environment
Create .env in project root:
OLLAMA_HOST=http://127.0.0.1:11434DATABASE_URL=postgresql+psycopg2://user:pass@localhost:5432/dbnameLLM_MODEL=deepseek-coder:6.7b(optional)
Run CLI
Install from source while developing:
pip install -e .
First-time setup:
pgnode connect
pgnode doctor
connect saves your database URL, Ollama host, and exact local model name to your user config. Environment variables still override saved config when present.
SSH tunnel databases are supported too. Choose ssh during pgnode connect or use flags:
pgnode connect \
--connection-type ssh \
--database-url "postgresql://user:pass@internal-db:5432/dbname" \
--ssh-host "bastion.example.com" \
--ssh-port 22 \
--ssh-user "ubuntu" \
--ssh-key-path "~/.ssh/id_rsa" \
--remote-host "127.0.0.1" \
--remote-port 5432 \
--local-port 0 \
--model "deepseek-coder-v2:16b"
Activate venv once:
source venv/bin/activate
Interactive conversation (context kept only in current session):
./pgnode run
or simply:
./pgnode
Useful chat commands:
/historyshow recent turns/clearclear current session context/exitor/quitleave session- Natural language meta-questions also work, e.g.:
what question did i ask you lastwhich query did you execute lastlast result
One-shot mode (no prior context):
./pgnode run "list all users with limit 5"
SQL-only generation (no execution):
./pgnode sql "top 5 customers by revenue last month"
Explain mode (SQL + short reasoning, no execution):
./pgnode explain "monthly revenue trend"
Schema helpers:
./pgnode tables
./pgnode describe Product
Environment and connectivity checks:
./pgnode config
./pgnode config-set --model "deepseek-coder-v2:16b"
./pgnode config-set --database-url "postgresql://user:pass@localhost:5432/dbname"
./pgnode config-set --connection-type ssh --ssh-host "bastion.example.com" --ssh-user "ubuntu" --ssh-key-path "~/.ssh/id_rsa" --remote-host "127.0.0.1" --remote-port 5432
./pgnode doctor
./pgnode models
Persistent local history:
./pgnode history
./pgnode rerun 12
Write behavior:
./pgnode run "update users set phone='999' where id=1"
INSERT/UPDATE now require confirmation by default. Use --yes to skip prompt.
./pgnode run --yes "update users set phone='999' where id=1"
Release files for pgnode 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pgnode-0.1.0.tar.gz | 40.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pgnode-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 81.4 kB
Release files / pgnode-0.1.0.tar.gz
| Download URL | pgnode-0.1.0.tar.gz |
|---|---|
| Size | 40.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
de1ee2dc84075d602279823750db0c34315091194572aff3d07881f6bb87e09b
|
|
BLAKE2b-256 checksum How to use checksums |
24838ee5a018897e8d4b2bea2025e3c70756039f1646e63a17861243504e7726
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 16, 2026.
Transparency logRelease files / pgnode-0.1.0-py3-none-any.whl
| Download URL | pgnode-0.1.0-py3-none-any.whl |
|---|---|
| Size | 40.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4b1c0245b0d52da68ae5a4ea385f046c6bf6a03a24a75d73f9a55540915b2ab8
|
|
BLAKE2b-256 checksum How to use checksums |
7efff0e2c02c63c6acba583920cf5155d2271cca8aec1dccd765d6055ad94c85
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 16, 2026.
Transparency log