PostgreSQL 17.12Lab-verified · 2026-12-01

PostgreSQL error 42601: syntax error at or near

PostgreSQL could not parse the statement; the token in quotes is where parsing stopped.

PostgreSQL 17.12
42601: syntax error at or near ","

Jump to a cause

  1. 01MySQL backticks around names
  2. 02A reserved word such as order used as a column name
  3. 03A comma left before FROM
  4. 04MySQL ON DUPLICATE KEY UPDATE copied as is
  5. 05MySQL-style LIMIT 10, 5
  6. 06An unescaped single quote inside a string ('O'Brien')

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-12-01 · container image postgres:17

42601 is a plain syntax error. The message names the token where PostgreSQL gave up, and the LINE and ^ lines show its position. The real mistake is often just before that token, not at it.

Look at the word or symbol right before the ^: a stray comma, a reserved word, or syntax from another database.

01MySQL backticks around names

SQL copied from MySQL wraps the column and table names in backticks (the reproduce statement below). PostgreSQL does not treat backticks as quotes, so the statement fails. Note that the message does not mention the backtick: it says syntax error at or near "," and the ^ points at the comma after the first column.

In PostgreSQL, identifiers are quoted with double quotes: SELECT "id", "name" FROM "users". For plain lowercase names you can also drop the quotes.

Setup
Setuprun as lab_a
CREATE TABLE users (id int PRIMARY KEY, name text, email text);
INSERT INTO users VALUES (1, 'Kim', 'kim@example.com');
OUTPUT
CREATE TABLE
INSERT 0 1
Reproducerun as lab_a
SELECT `id`, `name` FROM `users`;
✕ ERROR
ERROR:  42601: syntax error at or near ","
LINE 1: SELECT `id`, `name` FROM `users`;
                   ^
Check the causerun as lab_a
SELECT column_name, quote_ident(column_name) AS pg_quoted FROM information_schema.columns WHERE table_name = 'users' ORDER BY ordinal_position;
OUTPUT
 column_name | pg_quoted
-------------+-----------
 id          | id
 name        | name
 email       | email
(3 rows)

The columns id, name, email exist, and quote_ident returns them unchanged: plain lowercase names need no quotes in PostgreSQL, and when quotes are needed they are double quotes, not backticks.

Fix: run the corrected statementrun as lab_a
SELECT "id", "name" FROM "users";
✓ SUCCESS
 id | name
----+------
  1 | Kim
(1 row)

02A reserved word such as order used as a column name

CREATE TABLE menu (..., order int) fails with syntax error at or near "order" because ORDER is a reserved word (as in ORDER BY). The same happens with words like group or select.

A name that is not reserved, such as sort_order, works and can be used in queries without quotes.

Reproducerun as lab_a
CREATE TABLE menu (id int PRIMARY KEY, title text, order int);
✕ ERROR
ERROR:  42601: syntax error at or near "order"
LINE 1: ...REATE TABLE menu (id int PRIMARY KEY, title text, order int)...
                                                             ^
Check the causerun as lab_a
SELECT word, catcode, catdesc FROM pg_get_keywords() WHERE word IN ('order', 'group', 'title', 'sort_order') ORDER BY word;
OUTPUT
 word  | catcode | catdesc
-------+---------+----------
 group | R       | reserved
 order | R       | reserved
(2 rows)

pg_get_keywords() lists order and group with catcode R (reserved). title and sort_order are not in the result, so they are not keywords and can be used as column names.

Fixrun as lab_a
CREATE TABLE menu (id int PRIMARY KEY, title text, sort_order int);
OUTPUT
CREATE TABLE
After the fixrun as lab_a
INSERT INTO menu VALUES (1, 'Home', 1), (2, 'About', 2);
SELECT id, title, sort_order FROM menu ORDER BY sort_order;
✓ SUCCESS
INSERT 0 2
 id | title | sort_order
----+-------+------------
  1 | Home  |          1
  2 | About |          2
(2 rows)

03A comma left before FROM

After removing the last column from a SELECT list, the comma before it stayed: amount, followed by FROM orders. PostgreSQL expects another column after the comma and stops at FROM, reported as LINE 4: FROM orders.

The error points at FROM, but the fix is on the line above: remove the comma after amount.

Setup
Setuprun as lab_a
CREATE TABLE orders (id int PRIMARY KEY, status text, amount numeric);
INSERT INTO orders VALUES (1, 'paid', 1200), (2, 'cancelled', 500);
OUTPUT
CREATE TABLE
INSERT 0 2
Reproducerun as lab_a
SELECT id,
       status,
       amount,
FROM orders
ORDER BY id;
✕ ERROR
ERROR:  42601: syntax error at or near "FROM"
LINE 4: FROM orders
        ^
Check the causerun as lab_a
SELECT ordinal_position, column_name FROM information_schema.columns WHERE table_name = 'orders' ORDER BY ordinal_position;
OUTPUT
 ordinal_position | column_name
------------------+-------------
                1 | id
                2 | status
                3 | amount
(3 rows)

The table has three columns and amount is the last one (position 3). Nothing should follow it in the SELECT list, so the comma after amount is the leftover.

Fix: run the corrected statementrun as lab_a
SELECT id,
       status,
       amount
FROM orders
ORDER BY id;
✓ SUCCESS
 id |  status   | amount
----+-----------+--------
  1 | paid      |   1200
  2 | cancelled |    500
(2 rows)

04MySQL ON DUPLICATE KEY UPDATE copied as is

An upsert ported from MySQL, INSERT ... ON DUPLICATE KEY UPDATE qty = VALUES(qty), failed with syntax error at or near "DUPLICATE". PostgreSQL has no such syntax.

Setup
Setuprun as lab_a
CREATE TABLE stock (sku text PRIMARY KEY, qty int);
INSERT INTO stock VALUES ('A-100', 5);
OUTPUT
CREATE TABLE
INSERT 0 1
Reproducerun as lab_a
INSERT INTO stock (sku, qty) VALUES ('A-100', 7)
ON DUPLICATE KEY UPDATE qty = VALUES(qty);
✕ ERROR
ERROR:  42601: syntax error at or near "DUPLICATE"
LINE 2: ON DUPLICATE KEY UPDATE qty = VALUES(qty);
           ^
Check the causerun as lab_a
SELECT conname, pg_get_constraintdef(oid) AS definition FROM pg_constraint WHERE conrelid = 'stock'::regclass;
OUTPUT
  conname   |    definition
------------+-------------------
 stock_pkey | PRIMARY KEY (sku)
(1 row)

stock has the primary key stock_pkey on sku. That unique key is what ON CONFLICT (sku) needs to detect the duplicate row.

Naming the conflict key (here the primary key sku) and writing ON CONFLICT (sku) DO UPDATE SET qty = EXCLUDED.qty gave INSERT 0 1 and changed qty from 5 to 7. MySQL VALUES(qty) becomes EXCLUDED.qty in PostgreSQL.

Fix: run the corrected statementrun as lab_a
INSERT INTO stock (sku, qty) VALUES ('A-100', 7)
ON CONFLICT (sku) DO UPDATE SET qty = EXCLUDED.qty;
SELECT * FROM stock;
✓ SUCCESS
INSERT 0 1
  sku  | qty
-------+-----
 A-100 |   7
(1 row)

05MySQL-style LIMIT 10, 5

Paging with MySQL syntax LIMIT 10, 5 (skip 10, take 5) failed with LIMIT #,# syntax is not supported. That is 42601 too, and the HINT says Use separate LIMIT and OFFSET clauses.

Setup
Setuprun as lab_a
CREATE TABLE posts (id int PRIMARY KEY, title text);
INSERT INTO posts SELECT g, 'post ' || g FROM generate_series(1, 30) g;
OUTPUT
CREATE TABLE
INSERT 0 30
Reproducerun as lab_a
SELECT id, title FROM posts ORDER BY id LIMIT 10, 5;
✕ ERROR
ERROR:  42601: LIMIT #,# syntax is not supported
LINE 1: SELECT id, title FROM posts ORDER BY id LIMIT 10, 5;
                                                ^
HINT:  Use separate LIMIT and OFFSET clauses.
Check the causerun as lab_a
SELECT count(*) AS total, min(id), max(id) FROM posts;
OUTPUT
 total | min | max
-------+-----+-----
    30 |   1 |  30
(1 row)

The table has 30 rows with id 1 to 30, so skipping 10 and taking 5 should return ids 11 to 15.

Splitting it into LIMIT 5 OFFSET 10 returned rows 11 to 15. Note the numbers swap places: MySQL LIMIT skip, take is LIMIT take OFFSET skip.

Fix: run the corrected statementrun as lab_a
SELECT id, title FROM posts ORDER BY id LIMIT 5 OFFSET 10;
✓ SUCCESS
 id |  title
----+---------
 11 | post 11
 12 | post 12
 13 | post 13
 14 | post 14
 15 | post 15
(5 rows)

06An unescaped single quote inside a string ('O'Brien')

Inserting the name O'Brien as 'O'Brien' failed with syntax error at or near "Brien". The second quote ended the string, so Brien was read as SQL. The ^ points at Brien.

Setup
Setuprun as lab_a
CREATE TABLE customers (id int PRIMARY KEY, name text);
OUTPUT
CREATE TABLE
Reproducerun as lab_a
INSERT INTO customers VALUES (1, 'O'Brien');
✕ ERROR
ERROR:  42601: syntax error at or near "Brien"
LINE 1: INSERT INTO customers VALUES (1, 'O'Brien');
                                            ^
Check the causerun as lab_a
SELECT quote_literal($$O'Brien$$) AS sql_literal;
OUTPUT
 sql_literal
-------------
 'O''Brien'
(1 row)

quote_literal shows how PostgreSQL writes O'Brien as a SQL string: 'O''Brien', with the inner quote doubled.

Double a single quote inside a string: 'O''Brien' stored O'Brien as is. In application code, pass values as bind parameters instead of pasting them into SQL.

Fix: run the corrected statementrun as lab_a
INSERT INTO customers VALUES (1, 'O''Brien');
SELECT * FROM customers;
✓ SUCCESS
INSERT 0 1
 id |  name
----+---------
  1 | O'Brien
(1 row)

FAQ

How do I fix syntax error at or near in PostgreSQL?

Read the token in the message and look at what comes right before the ^ position. Common causes are a trailing comma, a reserved word used as a name, and MySQL syntax such as backticks.

Can I use backticks in PostgreSQL?

No. PostgreSQL quotes identifiers with double quotes, like "users". Backticks cause a 42601 syntax error.

Why do I get syntax error at or near "order"?

order is a reserved word in PostgreSQL. Use a different column name such as sort_order.

Why does the error point at FROM when FROM is correct?

PostgreSQL reports the token where parsing failed. With amount, before FROM, it expected another column and found FROM. Remove the comma on the previous line.

What replaces ON DUPLICATE KEY UPDATE in PostgreSQL?

INSERT ... ON CONFLICT (key) DO UPDATE SET col = EXCLUDED.col. In the test, this updated the existing row.

How do I fix LIMIT #,# syntax is not supported?

Change LIMIT 10, 5 to LIMIT 5 OFFSET 10. The order of the numbers is the reverse of MySQL.

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