A wire-compatible PostgreSQL server that routes all queries to an LLM instead of a real database. Clients connect with any Postgres client (psql, etc.) using the LLM provider as username, API key as password, and model name as database. Each connection maintains its own LLM chat session for cross-query consistency.
go install ./... # build (binary: ghostgres)
ghostgres # run on default port 5432
ghostgres -port 5433 -log-level debug # custom port + debug logging
go fmt ./... # always run after editing Go codeThere are no tests yet.
PGPASSWORD="sk-..." psql -h localhost -U openai -d gpt-5.4Username = LLM provider (currently only "openai"), password = API key, database = model name. Reasoning effort can be set via PGOPTIONS="reasoning_effort=high".
See docs/plan.md for the detailed implementation plan, protocol specifics, and post-MVP roadmap.
Client (psql) → Postgres Wire Protocol → Server → OpenAI API (structured outputs) → LLM
All code lives in two packages:
cmd/ghostgres/main.go— thin entry point: flag parsing, signal handling, graceful shutdowninternal/— all server logic
- server.go — TCP listener, accepts connections, manages goroutine lifecycle via
sync.WaitGroup - connection.go — per-connection handler: auth → startup → query loop. Creates an
LLMClientper connection - auth.go — cleartext password auth (needed to get the raw API key), sends startup parameter messages
- query.go — receives SQL, calls
LLMClient.Query(), translatesLLMResponseinto pgproto3 wire messages (RowDescription, DataRow, CommandComplete) - llm.go — OpenAI client wrapper. Maintains chat history per connection. Uses structured outputs (JSON schema generated from
LLMResponseviagithub.com/google/jsonschema-go). Auto-selects lowest reasoning effort per model family - protocol.go — helpers:
typeNameToOID(viapgtype.Map),buildRowDescription,buildDataRow,buildErrorResponse - logging.go — logger is always passed via
context.Context, never stored on structs. UseLoggerFromContext(ctx)/ContextWithLogger(ctx, logger)
- Logger via context: All functions receive
context.Contextand extract the logger. Fields are added with.With()and the enriched logger is put back into context. Usesgo.uber.org/zap. - Log message casing: Log messages start with a capital letter. Error strings start with a lowercase letter (per Go convention).
- Simple Query Protocol only: No extended query protocol (no prepared statements/parameter binding).
- Structured outputs: LLM returns JSON matching
LLMResponsestruct (array ofResultSetwith columns, rows as[]*stringfor null support, and command tags). - Model reasoning effort defaults:
modelReasoningEffortPrefixesin llm.go maps model prefixes to their lowest reasoning effort. Non-reasoning models get no effort set.
github.com/jackc/pgx/v5/pgproto3— Postgres wire protocol (Backend type for server-side)github.com/jackc/pgx/v5/pgtype— type name → OID lookupsgithub.com/openai/openai-go/v3— OpenAI API client with structured outputsgithub.com/google/jsonschema-go— JSON schema generation from Go structsgo.uber.org/zap— structured logging