A simple Python script that anonymizes PostgreSQL SQL queries by replacing table names, column names and function names while preserving query structure.
Designed to safely share SQL queries with LLMs without leaking schema details.
- Anonymizes:
- table names
- column names
- function names
- Preserves SQL keywords and structure
- Context-aware (distinguishes tables vs columns in UPDATE / SET / WHERE)
- Works with multi-statement SQL files
- PostgreSQL-oriented
- Python 3.8+
sqlparse
Install dependency:
pip install sqlparse-
Put your SQL query (or queries) into
input.txt -
Run the script:
python anonymize_sql.py- The anonymized SQL will be written to
output.txt
UPDATE users
SET status = 'ready'
WHERE user_id = 1;UPDATE table_1
SET column_1 = 'ready'
WHERE column_2 = 1;- SQL keywords are preserved
- String and numeric literals are not anonymized
- The same identifier is always replaced with the same anonymized name
- Intended for analysis, debugging and LLM usage --- not for execution
- Uses heuristics (not a full SQL AST)
- Limited support for advanced PostgreSQL features (e.g. JSONB operators)
- Not suitable for dynamic SQL
MIT