Code Beautifier
syntax error at or near ","

Fix "syntax error at or near ","" in PostgreSQL

PostgreSQL's most common syntax error: a comma where the grammar expects something else. The trailing-comma trap in SELECT lists and VALUES, and the fix.

Input that triggers it

SELECT id, name, email,
FROM users
WHERE active = true;
Open SQL Formatter on its own page
Draft saved locally.

Local workspace

Named projects in IndexedDB · Local only — never synced to our servers. Worksp

Open manager

Batch workspace

Format multiple files locally in one run.

SQL dialect:
Indent size:
Keyword case:
sql
Formatted Outputsql

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 (...), or GROUP 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 AND belongs in a WHERE clause, or between two statements.
  • A missing expression between two commas, SELECT a,, b.

How to fix it

  1. 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.
  2. Delete the comma that has no item after it, or supply the missing item.
  3. 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.
  4. 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 Server Incorrect 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