November 11 · Parameterize, transact, and make it runnable
Can a receiver run one useful database task without editing SQL or exposing a password?
Today’s sequence follows Alex Reinhart’s Using SQL from Code. Examples use current Psycopg 3 patterns, environment-based secrets, and the Fall 2026 client databases.
Current reference: Psycopg parameters and pipeline mode.
You should be able to:
Mixing all three into one giant script makes failures hard to locate.
Never commit passwords, tokens, or a .env file containing them.
Context managers close resources; the connection block also manages the transaction.
Read DATABASE_URL, connect, and print database, user, and server time. Then deliberately misspell the variable name and describe the failure.
No project tables yet. Prove configuration and connectivity first.
Unsafe:
Safe:
The driver adapts the value and its type.
Table and column names are SQL structure, not values.
Prefer a fixed allowlist when identifiers come from users.
Avoid loading an unbounded result just because fetchall() is convenient.
Convert one project query to accept two values from Python—for example geography and date range—without string formatting.
Test an ordinary value, a value with an apostrophe, and a value that returns zero rows.
Clean exit commits. An exception rolls the transaction back.
Logging an exception is not recovery. Re-raise unless you have a defined safe response.
Nested transaction contexts use savepoints. Use them only when continuing is valid.
Psycopg 3 can use pipeline mode internally for executemany(). Batch size still needs measurement and failure semantics.
Inside one transaction, insert a valid test row and then a row that violates a constraint. Verify that neither remains.
Record the exception class and the query that proves rollback.
It should validate inputs, emit useful progress, return a nonzero exit status on failure, and document expected output.
Put one parameterized client report and one transaction test in the builder handoff pack.
The receiver should not edit source code to change a geography or date range.
Homework 2 asks for:
Friday: hand off the system. Monday: learn full-text search by extending the one you receive.
Adapted from Alex Reinhart · MADS Computing