feat(db): add supabase db query command for executing SQL#4955
Open
Rodriguespn wants to merge 1 commit intodevelopfrom
Open
feat(db): add supabase db query command for executing SQL#4955Rodriguespn wants to merge 1 commit intodevelopfrom
supabase db query command for executing SQL#4955Rodriguespn wants to merge 1 commit intodevelopfrom
Conversation
Add a new CLI command that allows executing raw SQL against local and remote databases, designed for seamless use by AI coding agents without requiring MCP server configuration. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Pull Request Test Coverage Report for Build 23109567892Details
💛 - Coveralls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add
supabase db query [sql]command for executing raw SQL against local and remote Supabase databases.Why do we need this if we already have
execute_sqlin the MCP server?The MCP server is great for agents to securely interact with remote projects. The user has to follow the OAuth flow to authenticate the agent and then reload the agent session to load the MCP tools into context. This allows the agent to interact with the user's remote project without managing api keys and it's one of the advandages of using MCP over the CLI.
When working locally, there is no auth flow needed to connect to
localhost:54321/mcp, but the agent still requires the human to reload the agent session to load the MCP tools into context, when setting up the MCP initial connection. This adds friction to a path that should be fully agentic (no human in the loop).For this, the AI team suggests having a
db queryCLI command that allows the agent to interact with the database like theexecute_sqlMCP tool does.Example use case: local schema management. The agent changes the schema of the database by running DDL commands and, once it determines the schema is stable, runs
db diff --localto inspect schema changes and inform the migration name. With the current solution, we need theexecute_sqlMCP tool configured to run the queries. With this command, this development path only needs the CLI — no MCP configuration needed.Prompt injection safety
To prevent prompt injections, the default output format is JSON, where we wrap every response in a safety envelope — the same approach used by the
execute_sqlMCP tool output. The warning message reads:Implementation
supabase db query "SELECT ...", default): Uses pgx (direct Postgres wire protocol). pgx makes more sense than pg-meta for local because pg-meta runs as a Docker container inside thesupabase startstack — using it would require discovering the container port, authenticating with the service-role JWT, and making HTTP requests. pgx simply connects tolocalhost:54322with the password from config: direct TCP, no Docker dependency, no HTTP overhead, and consistent with every other localdbsubcommand (push,pull,diff,lint,test,reset,dump).supabase db query "SELECT ..." --linked): Uses the Management API (POST /v1/projects/{ref}/database/query), authenticated with the access token fromsupabase login. No database password needed — zero credential friction for agents.Usage
Test plan
go test ./internal/db/query/...— 17 unit tests covering:--file, stdin pipe, no input, file not foundcc @gregnr @mattrossman