What is PostgreSQL error code 42P02 (undefined_parameter)?<\/h2>PostgreSQL raises error 42P02, condition name undefined_parameter, when a SQL statement refers to a positional parameter such as $1, $2, or $3 that has not been provided a value at execution time.<\/p>
The server attempts to substitute the supplied bind variables into the query plan.
If it cannot find a value for a referenced placeholder, it fails fast with undefined_parameter to prevent ambiguous execution.<\/p>
What Causes This Error?<\/h3>Missing bind variables, mismatched parameter counts between PREPARE/EXECUTE, dynamic SQL in PL\/pgSQL without USING, and ORM misconfiguration are the most common triggers.<\/p>
How to Fix PostgreSQL Error 42P02<\/h3>Verify the number of placeholders matches the number of supplied arguments. Use PostgreSQL PREPARE ... EXECUTE syntax properly, or in PL\/pgSQL attach variables with USING.
Remove unused placeholders if no value is required.<\/p>
Common Scenarios and Solutions<\/h3>Applications that construct SQL strings manually, migrations that forget default values, and stored procedures that concatenate queries often surface this error. Supplying the missing parameter or rewriting the query removes the problem.<\/p>
Best Practices to Avoid This Error<\/h3>Always rely on parameterized queries, static SQL, and linting tools. Validate parameter counts in test suites.
IDEs such as Galaxy highlight unbound parameters before a query runs, preventing runtime failures.<\/p>
Related Errors and Solutions<\/h3>Errors 42703 (undefined_column) and 42601 (syntax_error) also arise from typos or mismatched query placeholders. The resolution pattern—locate and correct the invalid reference—mirrors the fix for 42P02.<\/p>
.