PostgreSQL 42601: syntax error at or near
PostgreSQL couldn't parse the statement. The token it names, and the caret under it, mark where parsing failed; the real mistake is often just before it.
- SQLSTATE
42601- Name
syntax_error
What it means
The parser read the statement up to the token in the message and found something that can't come next. syntax error at end of input means the statement stopped where more was expected, such as a missing closing parenthesis.
The position is where PostgreSQL noticed the problem, which is often one token after the actual mistake: a trailing comma before FROM, a missing comma between columns, or a reserved word used as a name.
Common causes
- A trailing comma before
FROM,)orWHERE, or a missing comma between items. - A reserved word used as a column or table name without quotes, such as
user,orderorgroup. - Syntax from another database:
TOP,[brackets], backticks,LIMIT x, y,IFNULL. - Unbalanced parentheses or quotes, which shows up as an error at end of input.
- Client-side placeholders (
?,:name,%s) sent to the server without the driver replacing them; PostgreSQL itself uses$1,$2.
How to fix it
-
Look at the token named in the message and the one just before it; remove the stray comma or add the missing one.
SELECT id, name FROM users;
-
Quote reserved words used as identifiers, or rename them.
SELECT "user", "order" FROM audit_log;
-
Rewrite other dialects' syntax in PostgreSQL form.
-- SQL Server: SELECT TOP 10 * FROM [orders] SELECT * FROM orders LIMIT 10; -- MySQL: LIMIT 20, 10 SELECT * FROM orders LIMIT 10 OFFSET 20;
-
For PL/pgSQL bodies, use dollar quoting so quotes inside the body don't end the string.
CREATE FUNCTION greet(name text) RETURNS text LANGUAGE plpgsql AS $$ BEGIN RETURN 'Hello, ' || name; END; $$;
Reproduce it safely
SELECT id, FROM (VALUES (1)) v(id); -- ERROR: syntax error at or near "FROM"
Related errors
42703column does not exist42P01relation does not exist42883function does not exist102SQL Server: Incorrect syntax near1064MySQL: You have an error in your SQL syntaxORA-00933Oracle: SQL command not properly ended
More syntax error or access rule violation errors
42501permission denied42846cannot coerce42803column must appear in the GROUP BY clause42830invalid foreign key42804datatype mismatch42P18could not determine data type of parameter42P02there is no parameter42704undefined object
The code and message text are from the PostgreSQL documentation, Appendix A: PostgreSQL Error Codes. Browse the full PostgreSQL error list or search every database error.
Work on PostgreSQL in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for PostgreSQL and the other databases you use.