Terminal-native local AI agent for PostgreSQL databases.
Project description
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"
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pgnode-0.1.0.tar.gz.
File metadata
- Download URL: pgnode-0.1.0.tar.gz
- Upload date:
- Size: 40.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
de1ee2dc84075d602279823750db0c34315091194572aff3d07881f6bb87e09b
|
|
| MD5 |
873ca13a512216bcd18dff33460be1c2
|
|
| BLAKE2b-256 |
24838ee5a018897e8d4b2bea2025e3c70756039f1646e63a17861243504e7726
|
Provenance
The following attestation bundles were made for pgnode-0.1.0.tar.gz:
Publisher:
release.yml on chayan-mann/pgnode
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pgnode-0.1.0.tar.gz -
Subject digest:
de1ee2dc84075d602279823750db0c34315091194572aff3d07881f6bb87e09b - Sigstore transparency entry: 1554211017
- Sigstore integration time:
-
Permalink:
chayan-mann/pgnode@c86e40a30173f3ce87dffc05cefe4dfd83cdb369 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/chayan-mann
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@c86e40a30173f3ce87dffc05cefe4dfd83cdb369 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pgnode-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pgnode-0.1.0-py3-none-any.whl
- Upload date:
- Size: 40.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b1c0245b0d52da68ae5a4ea385f046c6bf6a03a24a75d73f9a55540915b2ab8
|
|
| MD5 |
ad9b3a4dcdad1dec6abaade608490b46
|
|
| BLAKE2b-256 |
7efff0e2c02c63c6acba583920cf5155d2271cca8aec1dccd765d6055ad94c85
|
Provenance
The following attestation bundles were made for pgnode-0.1.0-py3-none-any.whl:
Publisher:
release.yml on chayan-mann/pgnode
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pgnode-0.1.0-py3-none-any.whl -
Subject digest:
4b1c0245b0d52da68ae5a4ea385f046c6bf6a03a24a75d73f9a55540915b2ab8 - Sigstore transparency entry: 1554211026
- Sigstore integration time:
-
Permalink:
chayan-mann/pgnode@c86e40a30173f3ce87dffc05cefe4dfd83cdb369 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/chayan-mann
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@c86e40a30173f3ce87dffc05cefe4dfd83cdb369 -
Trigger Event:
release
-
Statement type: