What the error means
PostgreSQL reports syntax errors by quoting the token it was looking at when the grammar stopped making sense: syntax error at or near ",", followed by LINE n: and a caret under the exact position. It means a comma appeared where the parser expected an expression, a keyword, or the end of a clause. The comma itself is rarely the whole story; it is the last comma before something that should not follow a comma.
In the example, the SELECT list ends with email,. After a comma, the parser expects another column expression. It gets the keyword FROM, which cannot be a column, and reports the comma that led it there.
Why it happens
- A trailing comma in a SELECT list, usually after deleting the last column or commenting it out with
--and leaving the comma on the line above. - A trailing comma in
VALUES (...),INSERT (...), orGROUP BY, for the same editing reasons. - A trailing comma in a CTE list or a column definition:
CREATE TABLE t (a int, b text,). - A comma used where
ANDbelongs in a WHERE clause, or between two statements. - A missing expression between two commas,
SELECT a,, b.
How to fix it
- Paste the statement into the formatter above with the dialect set to PostgreSQL. The formatter lays each list item on its own line, which makes a dangling comma at the end of a list impossible to miss.
- Delete the comma that has no item after it, or supply the missing item.
- If a column was commented out with
--, move the comma: PostgreSQL ignores the rest of the line after--, including any comma that followed the column name. - Run the statement again. If a different position is reported, repeat; each fix reveals the next.
The corrected example:
SELECT id, name, email
FROM users
WHERE active = true;
If it still fails
LINE 2:in PostgreSQL's message counts from the start of the statement as sent, which for a multi-statement script may not be the line in your file. Format the single failing statement on its own.- The same mistake in MySQL produces
You have an error in your SQL syntax ... near 'FROM users', and in SQL ServerIncorrect syntax near the keyword 'FROM'. The cause is identical. - Generated SQL from a CSV or JSON import is a common source; CSV to SQL emits INSERT statements without trailing commas, per dialect, if the hand-written version keeps failing.
Related errors
Unclosed quotation mark
Double any single quote inside a string literal ('O''Brien') and make sure every opening quote has a closing one in the same statement.
Incorrect syntax near ")"
Match every ( to a ), remove the extra closer, and never leave IN () or VALUES () empty; SQL Server reports the parenthesis it could not pair.