Using a PreToolUse hook to block SQL sent from Claude Code to a database before execution
Claude Code が DB に流す SQL を、PreToolUse フックで実行前に一度止める
A developer published a method using Claude Code's PreToolUse hook to intercept dangerous statements before SQL execution, working with their open-source tool SQLMegane to block statements such as UPDATE/DELETE without WHERE. The hook extracts SQL from Bash commands (psql, mysql, etc.) and MCP tool inputs; SQLMegane uses pure syntax parsing plus rule-based decisions, not an LLM, returning deny when dangerous, and switching to ask for human confirmation when SQL cannot be extracted or parsing fails. The author verified on Claude Code 2.1.278 that it blocked an unconditional UPDATE. The author also lists limitations: the hook can only extract SQL from simple single commands beginning with clients such as psql and mysql; compound commands and variable substitution switch to ask; SQL assembled inside application code or migration tools is not visible and is passed through directly; and it cannot replace permission-side protections such as read-only roles and connection isolation.