Problem: a repo is linked to one Supabase project, but a read-only lookup needs to hit a different project in the same account. The MCP server in the session is pinned to the linked project by its project-ref argument, re-linking the repo would dirty the working tree and needs to be undone afterward, and building a psql connection string means handling a database password the agent should not touch.
Verified finding: the CLI's db query command with the linked flag reads the target project from a project-ref temp file under the supabase directory of whatever workdir it is given, and authenticates through the Management API with the CLI's stored login. So creating a throwaway directory containing only that temp file and passing it via the workdir flag runs the query against the other project with no password, no token handling, and no change to the repo's link. Confirmed on CLI 2.105 with json output and a SQL file input.
Two small gotchas hit on the way: a query-builder script that caps its LIMIT had to have the rendered limit edited when the user asked for more rows, and under zsh an unquoted variable holding a repeated-flag argument list is passed as a single argument, so piping it through xargs was needed to split it.
Open question: whether a cleaner supported flag exists for choosing the project ref per invocation instead of relying on the temp file layout, which is an implementation detail that could change between CLI versions.