How to fix Undefined parameter in PostgreSQL
PostgreSQL error 42P02 occurs when a prepared statement references a parameter placeholder like $1 but no actual value is supplied. Fix by providing all bind variables or removing unused placeholders.
Quick fix
Fixes 42P02
-- This prepared statement expects 1 parameter:
PREPARE user_query AS SELECT * FROM users WHERE id = $1;
-- Wrong - no parameter value provided:
EXECUTE user_query;
-- Correct - parameter value provided:
EXECUTE user_query(123);- Check parameter count in your prepared statement
- Verify all parameter values are defined and not null/undefined
- Use explicit type casting if PostgreSQL cannot infer the type
- Do not use parameter placeholders for identifiers or keywords
-- This prepared statement expects 1 parameter:PREPARE user_query AS SELECT * FROM users WHERE id = $1;-- Wrong - no parameter value provided:EXECUTE user_query;-- Correct - parameter value provided:EXECUTE user_query(123);Undefined parameter
What this error means
PostgreSQL uses parameter placeholders ($1, $2, etc.) in prepared statements to represent values that will be provided at query execution time. The "Undefined parameter" error (error code 42P02) occurs when PostgreSQL encounters a reference to a parameter placeholder in your query but you haven't supplied the corresponding value when executing the statement. This commonly happens with the first parameter ($1), which is why you may see "there is no parameter $1" errors. Unlike some databases that use ? as placeholders, PostgreSQL's numbered placeholders require explicit values for each referenced number.
Typical symptoms
- ERROR: there is no parameter $1 when executing EXPLAIN on parameterized queries
- PSQLException or PG::UndefinedParameter exceptions in application code
- Query fails immediately without reaching the database execution stage
- Parameter-binding issues when using prepared statements or ORM query builders
- Monitoring tools or log analyzers fail when capturing queries with missing parameter values
Common causes
- Not providing a value for a parameter placeholder when executing the query
- Parameter count mismatch: declaring more parameters in the prepared statement than values supplied
- Using EXPLAIN or query analysis tools on parameterized queries without providing actual parameter values
- Incorrect parameter binding in application code, especially when the parameter number is wrong
- Passing undefined or uninitialized variables to parameterized queries in Node.js or similar environments
- Using parameter placeholders in contexts where they are not allowed (identifiers, keywords, or certain SQL clauses)
How to fix "Undefined parameter"
Count how many parameter placeholders ($1, $2, etc.) appear in your SQL query. Verify that you're supplying exactly that many values when executing.
Example of missing parameter:
-- This prepared statement expects 1 parameter:
PREPARE user_query AS SELECT * FROM users WHERE id = $1;
-- Wrong - no parameter value provided:
EXECUTE user_query;
-- Correct - parameter value provided:
EXECUTE user_query(123);Ensure every variable bound to a parameter placeholder is properly initialized. In Node.js, undefined values can cause this error even if the count matches.
Example with node-postgres:
const userId = undefined; // WRONG - will cause error
const query = "SELECT * FROM users WHERE id = $1";
await client.query(query, [userId]); // ERROR: undefined parameter
// Correct:
const userId = 42; // or null if that's intentional
const query = "SELECT * FROM users WHERE id = $1";
await client.query(query, [userId]); // WorksSometimes PostgreSQL cannot determine the data type from context alone, especially with NULL comparisons or complex expressions. Cast the parameter explicitly.
Example:
-- May cause error if $1 type is ambiguous:
PREPARE check_value AS SELECT * FROM data WHERE value IS $1;
-- Use explicit casting:
PREPARE check_value AS SELECT * FROM data WHERE value IS $1::boolean;Parameters can only be used for literal values in SELECT, INSERT, UPDATE, DELETE, and VALUES statements. You cannot use them for table names, column names, or SQL keywords.
Wrong:
-- ERROR: cannot use $1 for identifiers
PREPARE bad_query AS SELECT * FROM $1;
PREPARE bad_query AS SELECT $1 FROM users;Correct:
-- Use parameters only for values
PREPARE good_query AS SELECT * FROM users WHERE id = $1;If you're analyzing a prepared statement with EXPLAIN, you must supply parameter values. Alternatively, use PostgreSQL 16+ which supports EXPLAIN (GENERIC_PLAN).
Example:
-- Without values - will error:
EXPLAIN SELECT * FROM users WHERE id = $1;
-- With values - works:
EXPLAIN SELECT * FROM users WHERE id = 123;
-- PostgreSQL 16+ - generic plan (no values needed):
EXPLAIN (GENERIC_PLAN) SELECT * FROM users WHERE id = $1;Advanced notes
Parameter type inference is a PostgreSQL feature where the database automatically determines parameter types from context. In ambiguous cases like "WHERE value IS NULL" or "WHERE value = ANY($1)", PostgreSQL may not be able to infer the type and will throw this error. In these situations, explicit type casting (e.g., $1::integer) is required. For monitoring tools that capture queries from pg_stat_statements, parameter values are intentionally omitted for security and performance reasons, which is why tools analyzing those queries must handle parameterized forms differently. Some tools now support PostgreSQL 16's GENERIC_PLAN feature for more accurate query analysis without needing actual parameter values.
Frequently asked questions
- What does "42P02" mean?
- ) in prepared statements to represent values that will be provided at query execution time. The "Undefined parameter" error (error code 42P02) occurs when PostgreSQL encounters a reference to a parameter placeholder in your query but you haven't supplied the corresponding value when executing the statement. This commonly happens with the first parameter ($1), which is why you may see "there is no parameter $1" errors.
- How do I fix "42P02"?
- PostgreSQL error 42P02 occurs when a prepared statement references a parameter placeholder like $1 but no actual value is supplied. Fix by providing all bind variables or removing unused placeholders.
- What causes "42P02"?
- Not providing a value for a parameter placeholder when executing the query Parameter count mismatch: declaring more parameters in the prepared statement than values supplied Using EXPLAIN or query analysis tools on parameterized queries without providing actual parameter values
- Is the "42P02" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- ๐PostgreSQL Official Documentation
- ๐PostgreSQL Error Codes Appendix(Official Docs)
- ๐PostgreSQL PREPARE Documentation(Official Docs)