Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-29 · container image postgres:17
$1, $2 … are positional parameters. They only mean something inside a prepared statement, a function body, or a query sent with bind values by a driver. When PostgreSQL parses $n and there is no n-th parameter, it stops with 42P02.
The three runs below are the usual causes. The caret (^) in the error points at the parameter that has no value.
01Running a query with $1 directly (copied from app code or a log)
SELECT id, amount FROM orders WHERE id = $1; typed into psql failed with there is no parameter $1. A plain query has no parameters, so $1 has nothing to refer to.
To run it as-is, prepare it with a parameter type: PREPARE order_by_id(int) AS ... WHERE id = $1; and then EXECUTE order_by_id(2);, which returned the row for id 2. Or replace $1 with a literal value.
Setup
CREATE TABLE orders (id int PRIMARY KEY, amount numeric);
INSERT INTO orders VALUES (1, 100), (2, 250);CREATE TABLE
INSERT 0 2SELECT id, amount FROM orders WHERE id = $1;ERROR: 42P02: there is no parameter $1
LINE 1: SELECT id, amount FROM orders WHERE id = $1;
^PREPARE order_by_id(int) AS SELECT id, amount FROM orders WHERE id = $1;PREPAREPREPARE order_by_id(int) AS SELECT id, amount FROM orders WHERE id = $1;
EXECUTE order_by_id(2);PREPARE
id | amount
----+--------
2 | 250
(1 row)02EXECUTE in PL/pgSQL with $1 but without USING
The function ran EXECUTE 'SELECT amount FROM orders WHERE id = $1' INTO v;. The function's own argument is not passed into dynamic SQL automatically, so the call failed with 42P02, and CONTEXT showed PL/pgSQL function order_amount(integer) line 4 at EXECUTE.
Adding USING p_id to the EXECUTE passed the value as $1, and SELECT order_amount(2) returned 250.
Setup
CREATE TABLE orders (id int PRIMARY KEY, amount numeric);
INSERT INTO orders VALUES (1, 100), (2, 250);
CREATE FUNCTION order_amount(p_id int) RETURNS numeric LANGUAGE plpgsql AS $$
DECLARE v numeric;
BEGIN
EXECUTE 'SELECT amount FROM orders WHERE id = $1' INTO v;
RETURN v;
END $$;CREATE TABLE
INSERT 0 2
CREATE FUNCTIONSELECT order_amount(2);ERROR: 42P02: there is no parameter $1
LINE 1: SELECT amount FROM orders WHERE id = $1
^
QUERY: SELECT amount FROM orders WHERE id = $1
CONTEXT: PL/pgSQL function order_amount(integer) line 4 at EXECUTECREATE OR REPLACE FUNCTION order_amount(p_id int) RETURNS numeric LANGUAGE plpgsql AS $$
DECLARE v numeric;
BEGIN
EXECUTE 'SELECT amount FROM orders WHERE id = $1' INTO v USING p_id;
RETURN v;
END $$;CREATE FUNCTIONSELECT order_amount(2); order_amount
--------------
250
(1 row)03A SQL function body uses a parameter number it does not have
The function was declared with two arguments (int, int) but the body used $1 AND $3. CREATE FUNCTION itself failed with there is no parameter $3.
Giving the arguments names (p_from int, p_to int) and using those names in the body avoided the numbering mistake, and SELECT orders_between(1, 2) returned 2.
Setup
CREATE TABLE orders (id int PRIMARY KEY, amount numeric);
INSERT INTO orders VALUES (1, 100), (2, 250);CREATE TABLE
INSERT 0 2CREATE FUNCTION orders_between(int, int) RETURNS bigint LANGUAGE sql AS $$
SELECT count(*) FROM orders WHERE id BETWEEN $1 AND $3;
$$;ERROR: 42P02: there is no parameter $3
LINE 2: SELECT count(*) FROM orders WHERE id BETWEEN $1 AND $3;
^CREATE FUNCTION orders_between(p_from int, p_to int) RETURNS bigint LANGUAGE sql AS $$
SELECT count(*) FROM orders WHERE id BETWEEN p_from AND p_to;
$$;CREATE FUNCTIONSELECT orders_between(1, 2); orders_between
----------------
2
(1 row)FAQ
How do I test a query with $1 in psql?
Use PREPARE and EXECUTE: PREPARE q(int) AS SELECT ... WHERE id = $1; EXECUTE q(2);. Or replace $1 with a literal value.
Why does EXECUTE in my PL/pgSQL function say there is no parameter $1?
Dynamic SQL only sees values you pass with USING. Write EXECUTE '... WHERE id = $1' INTO v USING p_id;.
Should I use $1 or argument names in functions?
Names are easier to read and avoid using a number the function does not have. In the run above, named arguments in a LANGUAGE sql function worked.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12