PostgreSQL · sdk · fg-0279

psycopg 3 raises 'syntax error at or near "$1"' on SET or NOTIFY with parameters that worked in psycopg2

If you see psycopg.errors.SyntaxError: syntax error at or near "$1", the cause and the fix for PostgreSQL are below, last verified 2026-10-15.

Verified Oct 2026trust 50/100
Symptom
psycopg.errors.SyntaxError: syntax error at or near "$1"psycopg.errors.SyntaxError: cannot insert multiple commands into a prepared statement
Cause
psycopg 3 sends parameters to the server separately, and PostgreSQL accepts them only in plain SELECT and data change statements, one statement per call. psycopg2 merged values into the text on the client, so both patterns used to work.
Fix
Use the function forms set_config and pg_notify, split multi-statement strings into separate execute calls, or use ClientCursor for client-side binding.
Apply
conn.execute("SELECT set_config('TimeZone', %s, false)", ["UTC"]) conn.execute("SELECT pg_notify(%s, %s)", ["chan", "42"])
Affected
psycopg 3.x, when porting code from psycopg2
Sources

Last verified 2026-10-15 · Confirmed by 0 builders

Related traps

Have your agent know this before it fails

$ /plugin marketplace add getfootgun/footgun
$ /plugin install footgun@footgun