PostgreSQL-aware SQL tooling for Python strings and SQL files in VS Code
pyPgSense helps you move faster when SQL lives inside Python code. It detects SQL-like strings, opens them in a dedicated SQL editor, and adds PostgreSQL-aware completion, validation, and execution workflows.
Why pyPgSense
Detect inline SQL in Python assignments and call arguments.
Open detected SQL with an Open SQL CodeLens for focused editing.
Get SQL syntax highlighting inside common Python SQL string patterns.
Receive keyword, table, and column completions from your PostgreSQL schema.
Validate SQL docs against PostgreSQL using PREPARE diagnostics.
Run selected SQL and inspect results in a side-by-side webview.
Manage connections from a dedicated sidebar view, saved securely in VS Code Secret Storage.
Quick Start
Open a Python or SQL file in VS Code.
Configure your database with pyPgSense: Set PostgreSQL Connection or from the pyPgSense sidebar connection form.
In Python, place SQL in a string that pyPgSense can detect.
Click Open SQL CodeLens to open that SQL in a SQL editor.
Use pyPgSense: Run Selected SQL to execute selection/full SQL and review results.
SQL diagnostics run automatically for SQL documents.
Example Workflow
Python file
query = """
SELECT id, email
FROM users
WHERE active = true
"""
Open the SQL via the Open SQL CodeLens, then use completion, linting, and run commands in the SQL editor.
Command Reference
Command
Purpose
pyPgSense: Open Inline SQL
Opens detected inline SQL in a side-by-side SQL editor.
pyPgSense: Run SQL
Executes selected SQL (or full document if nothing is selected).
pyPgSense: Set PostgreSQL Connection
Saves, tests, or clears PostgreSQL connection details.
pyPgSense: Refresh Schema
Reloads tables/columns used for schema-aware completions.
Settings
Setting
Description
pypgsense.postgres.connectionString
Optional fallback connection string. Prefer the command/UI so credentials are saved in Secret Storage.
Inline SQL Detection Rules
pyPgSense currently detects SQL-like text from:
Right-hand side of Python assignment and annotated_assignment nodes.
First argument of Python function calls.
Heuristic SQL checks (SQL keyword/context matching) to reduce false positives.
Current Scope
SQL linting and execution are applied to SQL editors, including docs opened via Open SQL.
SQL syntax injection in Python includes assignments to query, sql, statement, stmt, and raw_sql.
SQL syntax injection in Python includes calls like execute(...), executemany(...), fetch(...), fetchrow(...), fetchval(...), fetchone(...), and fetchall(...).
Editing in the opened SQL document does not write changes back into the original Python string.
Requirements
VS Code ^1.97.1
A reachable PostgreSQL database for schema-aware completion, linting, and execution