nodecode-postgres
PostgreSQL through the psql client: list schemas and tables, describe a table's columns, constraints and indexes, read a query plan, and run queries. Read-only unless the operator opts in. Written for Nodecode around psql; no PostgreSQL code or binary is included.
Get it
Tell the agent "add the postgres cell", or run nodecode add nc://postgres.
Set it up
- Install the PostgreSQL client,
psql16 or later (thepostgresql-clientpackage, or postgresql.org/download). - Put a connection URL in Nodecode's environment as
POSTGRES_URL:postgresql://user:password@host:5432/dbname?sslmode=require. - Connect as a role that can only read. The cell's read-only mode is a gate in front of the database; a role with only
SELECTgrants is the guarantee.
See config.example.jsonc:
| Key | Default | What it does |
|---|---|---|
command |
"psql" |
The psql binary: a name on PATH or an absolute path. |
url_env |
"POSTGRES_URL" |
The environment variable holding the connection URL. |
url |
none | A URL written here instead; the environment variable wins. |
allow_writes |
false |
Run statements that do not read (INSERT, UPDATE, DELETE, DDL), committed. |
timeout_seconds |
30 |
One statement's statement_timeout, and the psql run's ceiling. |
max_rows |
200 |
How many rows one answer fetches. |
max_chars |
30000 |
How many characters one answer carries. |
Use it
| Tool | For |
|---|---|
postgres_schemas |
The schemas, system ones left out, with owner and how many tables and views each holds. |
postgres_tables |
One schema's (default public) tables, views, materialized views and foreign tables, with estimated rows, size on disk and comment. |
postgres_describe |
One table's columns (type, null, default, comment) and constraints; public.users or schema names the schema. |
postgres_indexes |
One table's indexes with their definitions. |
postgres_explain |
A statement's plan; analyze runs it inside a read-only transaction and reports real timings and buffers. |
postgres_query |
One statement, its rows as CSV up to max_rows. |
SELECT (read-only): 5 rows (more exist: only the first 5 fetched; narrow the query or add LIMIT) id,name,email 1,user1,u1@example.com 2,user2,u2@example.com ...
NULL is shown as NULL. /postgres checks the connection and names the server version, the user, the database, and whether the cell is read-only.
How it stays read-only
- One statement per call, checked twice. The cell splits SQL the way PostgreSQL's lexer does (identifiers and
$tag$names past ASCII, quoted strings,E''strings, quoted identifiers,$tag$dollar quotes, nested comments,--comments ending at a newline or carriage return), refuses a string, quote or comment left open, and refuses a call holding a second statement beforepsqlruns. Then every statement goes throughpsql's\bind, the extended protocol, where the server itself refuses a second statement. - The first keyword must read:
SELECT,WITH,VALUES,TABLE,EXPLAINorSHOW. Anything else is refused unlessallow_writesis on.psql's backslash commands are always refused. - A statement that reads runs inside
BEGIN TRANSACTION READ ONLYin a session whosedefault_transaction_read_onlyis on, and is rolled back. The session's settings (statement_timeout,standard_conforming_strings,default_transaction_read_only) areSETas its first commands, so anoptions=parameter in the URL cannot turn them off. A data-modifyingWITH,SELECT ... INTOorEXPLAIN ANALYZEof a write is refused by the server. - Rows come through a cursor fetched up to
max_rows+ 1, so a large table is never read whole; the answer says when more exist. - Read-only transactions still allow some functions with effects outside the database's tables (signalling backends, for one). That is why a read-only role is the real guarantee.
The connection
- The URL comes only from the environment or the section, never from the model.
- Its password is taken out of the URL (the user part, or a
password=parameter) and handed topsqlasPGPASSWORD, so it is in no argument list.psqlruns with-X(no~/.psqlrc) and-w(never prompts). - Every answer and every error has the password (decoded, as the URL wrote it, and percent-encoded either way) and the URL scrubbed out.
- A missing URL, a missing
psql, a refused login and a timeout each say what to do.
Test
.github/test-cell.sh tools/nodecode-postgres
The tests run offline: psql is stubbed through the cell's *run* seam, so the statement splitter (against the SQL that once slipped past it) and keyword gate, each tool's psql commands, \bind and session settings, the password's path, the scrubbing, read-only refusals and row clipping are asserted without a database.
License
MIT. See LICENSE. PostgreSQL is under the PostgreSQL License and is not included.