Cyber Monday: 2XCYBER doubles your top-up, up to $2,000.Double my top-up

nc://postgres

v0.1.0Pinned
Backnodecode-ai
ThomSharktools: add context7, exa, notion, postgres and playwright cells (#184)719ef6f· 2 Dec1 commit
test1 filecell.lisp4.9 KBcli.lisp9.7 KBsql.lisp9.0 KBtools.lisp11.7 KB

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

  1. Install the PostgreSQL client, psql 16 or later (the postgresql-client package, or postgresql.org/download).
  2. Put a connection URL in Nodecode's environment as POSTGRES_URL: postgresql://user:password@host:5432/dbname?sslmode=require.
  3. 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 SELECT grants 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 before psql runs. Then every statement goes through psql's \bind, the extended protocol, where the server itself refuses a second statement.
  • The first keyword must read: SELECT, WITH, VALUES, TABLE, EXPLAIN or SHOW. Anything else is refused unless allow_writes is on. psql's backslash commands are always refused.
  • A statement that reads runs inside BEGIN TRANSACTION READ ONLY in a session whose default_transaction_read_only is on, and is rolled back. The session's settings (statement_timeout, standard_conforming_strings, default_transaction_read_only) are SET as its first commands, so an options= parameter in the URL cannot turn them off. A data-modifying WITH, SELECT ... INTO or EXPLAIN ANALYZE of 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 to psql as PGPASSWORD, so it is in no argument list. psql runs 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.

Add nc://postgres

  1. 1

    Install Nodecode

    Once per machine.

    $ curl -fsSL https://nodecode.ai/install.sh | sh
  2. 2

    Add the cell

    Pinned at 719ef6f.

    $ nodecode add nc://postgres

    Or tell the agent add the postgres cell.