Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL Error 42P02: Undefined Parameter Fix

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

PostgreSQL error 42P02 means the server found a parameter placeholder like $1, $2 or $3 in your SQL, but no value was supplied for that position. The fix is to make the placeholder numbers and the values you bind line up exactly: same count, same order, no gaps, and no placeholders inside string literals. This guide walks through finding the mismatch, correcting it in raw SQL, and fixing the same problem when it comes from an application driver, an ORM, or a prepared statement.

What You'll Need

  • Access to the database client or application code that produces the query (psql, pgAdmin, your app's repository layer, or the ORM query builder).
  • The exact SQL text that fails, including the placeholder numbers.
  • Permission to run the query yourself in a test environment so you can reproduce the error without touching production data.
  • A way to view the full server error message, not just a generic "query failed" line from your framework.

Step-by-Step

  1. Read the full error text. A 42P02 message typically reads something like there is no parameter $2. That number is the key clue: it tells you which placeholder PostgreSQL could not resolve. Write it down before changing anything.
  2. Count the placeholders in the SQL. Scan the statement for every $n token. PostgreSQL numbers parameters starting at $1 and expects them to be contiguous: if you use $1 and $3, the server still expects a $2 to exist. A gap is one of the most common causes of 42P02.
  3. Compare that count with the values you bind. In psql, check the PREPARE statement and its type list. In application code, count the arguments passed to the query function. If the SQL has three placeholders and your code passes two values, the third placeholder is undefined.
  4. Check the order and numbering. Placeholders must match the order of the bound values. If you reordered columns in the SQL but not the values array, the numbers still exist but point at the wrong data. Rewrite the placeholders so they run $1, $2, $3 in the order the values are supplied.
  5. Look for placeholders inside quotes. A common mistake is writing '$1' as a string literal instead of $1 as a parameter. Inside single quotes PostgreSQL treats it as text, so the real parameter is never defined and the statement can fail with 42P02. Remove the quotes around the placeholder and keep the value as a bound parameter.
  6. Reproduce the failure in isolation. Copy the exact SQL into psql and run it with a PREPARE statement, for example PREPARE test(int, text) AS SELECT * FROM users WHERE id = $1 AND name = $2;. If that succeeds, the SQL is fine and the problem is in how your application sends it. If it fails, the SQL itself is malformed.
  7. Fix the application layer. In most drivers the query method takes the SQL string and an array or list of values. Make sure the array length equals the highest placeholder number, and that no value is accidentally dropped by a conditional branch. A frequent bug is building the SQL dynamically but forgetting to add the matching value when an optional filter is enabled.
  8. Check ORM-generated SQL. If you use an ORM or query builder, log the final SQL and the bound parameters. Some builders emit placeholders for optional clauses that are later removed, or reuse a numbered placeholder across a union. Compare the logged SQL with the logged parameter list and adjust the query builder call so both sides match.
  9. Re-run the query and confirm. Execute the corrected statement in a test environment first. Then run it through the application path that originally failed. A successful run with no 42P02 confirms the placeholder and value lists are aligned.

Common Mistakes

  • Skipping a number. Using $1 and $3 without $2 leaves a hole the server cannot fill.
  • Quoting the placeholder. '$1' is a literal string, not a parameter. It will not be bound, and the real parameter may never be defined.
  • Mismatched counts after editing. Adding a WHERE clause with a new placeholder but forgetting to add the corresponding value is the single most common cause in application code.
  • Reusing a placeholder across statements. Each prepared statement has its own parameter list. A $1 in one statement does not carry over to another.
  • Assuming the driver renumbers. Most drivers pass placeholders through unchanged. If your SQL says $5, you must supply five values even if only one is used.

Tips

  • Keep placeholders sequential and in the same order as the values. It makes the mismatch obvious at a glance.
  • Log both the SQL and the parameter array together whenever a query fails. The pair is what reveals the bug.
  • When building SQL dynamically, append the placeholder and the value in the same branch of code so they cannot drift apart.
  • Use a test query in psql with PREPARE to validate the SQL independently of your application framework.
  • If you switch between positional styles, remember that some drivers use ? or :name and convert them internally. Check the driver documentation for the exact syntax it expects.

Troubleshooting

If the error persists after aligning the placeholders, work through these checks in order. First, confirm you are editing the query that actually runs: cached prepared statements, connection pools, and ORM query caches can serve an older version of the SQL. Restart the application or clear the relevant cache, then retry.

Second, inspect the server log for the full statement. PostgreSQL logs the failing SQL in many configurations, and the logged text may differ from what you expect, especially if a framework rewrites placeholders. Compare the logged text with your source.

Third, test the same statement through a different client, such as psql. If psql succeeds and the application fails, the problem is in the driver or the binding layer, not in PostgreSQL itself. Check the driver version and its documentation for how it handles parameter arrays.

Fourth, if you use a connection pooler, confirm it is not rewriting or stripping parameters. Some poolers in transaction mode can interfere with prepared statements; check the pooler's documentation for its prepared statement settings.

Finally, if the error mentions a parameter number higher than any you wrote, look for generated SQL from a library, trigger, or view definition that adds its own placeholders. Search the codebase for the statement text rather than assuming your hand-written query is the one being sent.

Frequently Asked Questions

What does error 42P02 actually mean?

It is PostgreSQL's "undefined parameter" error. The SQL references a parameter placeholder such as $2, but the server has no value or type defined for that position.

Does 42P02 mean my data is corrupted?

No. It is a query-construction error, not a storage error. The database is fine; the statement and its bound values do not match.

Why does the query work in psql but fail in my app?

psql and your application bind parameters differently. The app may be dropping a value, quoting the placeholder, or sending a different SQL text than the one you tested.

Can I use $1 and $3 without $2?

No. PostgreSQL expects contiguous numbering. Use $1, $2, $3 in order, or renumber the placeholders and the values together.

Is 42P02 the same as a syntax error?

No. A syntax error is a different SQLSTATE class. 42P02 specifically concerns parameter placeholders that were never defined.

Where should I look first in application code?

Start where the query is built. Compare the number of placeholders in the SQL string with the length of the values array passed to the query function; that mismatch is the usual cause.