PostgreSQL Error 42P02: Undefined Parameter

PostgreSQL error 42P02 (Undefined Parameter) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.

SQLSTATE 42P02 PostgreSQL Last verified 2026-08-19

Quick Answer

Count positional parameters ($1, $2, ...) and bind exactly that many values. If that does not apply, ensure the driver's parameter placeholders map to the correct positions — the full checklist is below.

Error Code

SQLSTATE: 42P02
Official name: Undefined Parameter
Service: PostgreSQL

What does this error mean?

The referenced prepared-statement parameter does not exist.

Common Causes

How to Fix

  1. Count positional parameters ($1, $2, ...) and bind exactly that many values
  2. Ensure the driver's parameter placeholders map to the correct positions
  3. Re-prepare the statement if its parameter list changed

Code Examples

Match parameter count sql
PREPARE find(id int) AS SELECT * FROM users WHERE id = $1;
EXECUTE find(42);

The number of $N placeholders in PREPARE must equal the EXECUTE arguments; mismatches raise 42P02.

Framework-Specific Fixes

sqlalchemy

Pass params as a dict matching named placeholders; do not mix styles.

session.execute(text('SELECT * FROM u WHERE id = :id'), {'id': 1})
supabase

When calling RPCs with .rpc(), pass all declared parameters.

await supabase.rpc('get_user', { user_id: 1 })

You Might Also Like

Frequently Asked Questions

Why am I seeing PostgreSQL error 42P02?

Most often this happens when referencing $N where N exceeds the parameter count in a prepared statement, or when binding fewer parameters than the statement expects.

How do I fix PostgreSQL error 42P02?

Count positional parameters ($1, $2, ...) and bind exactly that many values.

Which frameworks have documented fixes for error 42P02?

This page documents fixes for: sqlalchemy, supabase.

Official Sources

This page is based on the official PostgreSQL documentation linked below and adds practical troubleshooting guidance on top.