This error occurs when PostgreSQL's parser reaches the end of a statement unexpectedly. It typically indicates incomplete SQL syntax, missing parentheses, unclosed quotes, or incorrect parameter placeholders.
Fixes 42601
SELECT * FROM users WHERE (age > 21 AND name = 'John'SELECT * FROM users WHERE (age > 21 AND name = 'John'ERROR: syntax error at end of input
On this page
PostgreSQL terminates commands with a semicolon or end of input. When the parser encounters the end of input before all expected syntax elements are provided, it raises a syntax error. This is commonly seen in prepared statements using incorrect parameter syntax, unclosed parentheses, unmatched quotes, or incomplete multi-line queries.
Review your SQL statement for matching opening and closing parentheses. Count opening ( and closing ) to ensure they're balanced.
Problematic example:
SELECT * FROM users WHERE (age > 21 AND name = 'John'Fixed version:
SELECT * FROM users WHERE (age > 21 AND name = 'John')PostgreSQL uses $1, $2, $3 placeholders, not ?. If migrating from MySQL or using generic SQL libraries, update parameter syntax.
Incorrect (MySQL style):
WHERE email = ?Correct (PostgreSQL):
WHERE email = $1Ensure all string literals have matching opening and closing quotes (both single ' and double ").
Problematic:
WHERE username = 'adminFixed:
WHERE username = 'admin'Ensure all SQL statements contain complete syntax. For INSERT, include VALUES. For SELECT, include column references and FROM clause.
Incomplete:
INSERT INTO users (name, email)Complete:
INSERT INTO users (name, email) VALUES ('John', '[email protected]')If writing PL/pgSQL functions or procedures, ensure correct syntax:
- Do NOT put semicolon immediately after BEGIN
- Use EXECUTE for dynamic queries inside PL/pgSQL
Incorrect:
CREATE FUNCTION test() AS $$
BEGIN;
SELECT 1;
END;Correct:
CREATE FUNCTION test() AS $$
BEGIN
SELECT 1;
END;
$$ LANGUAGE plpgsql;Isolate the problem by testing a simplified version of your query in psql or a SQL client like pgAdmin. Build complexity gradually.
Start with:
SELECT 1;Then add table references, filters, and parameters incrementally to identify where the syntax breaks.
JDBC Driver Compatibility: If using JDBC with PostgreSQL 14+, ensure your driver version supports the BEGIN ATOMIC syntax for SQL-standard function bodies. Some older JDBC drivers may not parse this correctly and report misleading 'end of input' errors even though the syntax is valid.
Dynamic Query Construction: When building SQL dynamically (e.g., concatenating strings), ensure no intermediate state creates invalid syntax. For example, incomplete WHERE clauses or trailing commas will cause this error.