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
CREATE TABLE users (id int PRIMARY KEY, name text, email text);
INSERT INTO users VALUES (1, 'Kim', 'kim@example.com');CREATE TABLE
INSERT 0 1SELECT `id`, `name` FROM `users`;ERROR: 42601: syntax error at or near ","
LINE 1: SELECT `id`, `name` FROM `users`;
^SELECT column_name, quote_ident(column_name) AS pg_quoted FROM information_schema.columns WHERE table_name = 'users' ORDER BY ordinal_position; 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.
SELECT "id", "name" FROM "users"; 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.
CREATE TABLE menu (id int PRIMARY KEY, title text, order int);ERROR: 42601: syntax error at or near "order"
LINE 1: ...REATE TABLE menu (id int PRIMARY KEY, title text, order int)...
^SELECT word, catcode, catdesc FROM pg_get_keywords() WHERE word IN ('order', 'group', 'title', 'sort_order') ORDER BY word; 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.
CREATE TABLE menu (id int PRIMARY KEY, title text, sort_order int);CREATE TABLEINSERT INTO menu VALUES (1, 'Home', 1), (2, 'About', 2);
SELECT id, title, sort_order FROM menu ORDER BY sort_order;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
CREATE TABLE orders (id int PRIMARY KEY, status text, amount numeric);
INSERT INTO orders VALUES (1, 'paid', 1200), (2, 'cancelled', 500);CREATE TABLE
INSERT 0 2SELECT id,
status,
amount,
FROM orders
ORDER BY id;ERROR: 42601: syntax error at or near "FROM"
LINE 4: FROM orders
^SELECT ordinal_position, column_name FROM information_schema.columns WHERE table_name = 'orders' ORDER BY ordinal_position; 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.
SELECT id,
status,
amount
FROM orders
ORDER BY id; 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
CREATE TABLE stock (sku text PRIMARY KEY, qty int);
INSERT INTO stock VALUES ('A-100', 5);CREATE TABLE
INSERT 0 1INSERT INTO stock (sku, qty) VALUES ('A-100', 7)
ON DUPLICATE KEY UPDATE qty = VALUES(qty);ERROR: 42601: syntax error at or near "DUPLICATE"
LINE 2: ON DUPLICATE KEY UPDATE qty = VALUES(qty);
^SELECT conname, pg_get_constraintdef(oid) AS definition FROM pg_constraint WHERE conrelid = 'stock'::regclass; 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.
INSERT INTO stock (sku, qty) VALUES ('A-100', 7)
ON CONFLICT (sku) DO UPDATE SET qty = EXCLUDED.qty;
SELECT * FROM stock;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
CREATE TABLE posts (id int PRIMARY KEY, title text);
INSERT INTO posts SELECT g, 'post ' || g FROM generate_series(1, 30) g;CREATE TABLE
INSERT 0 30SELECT id, title FROM posts ORDER BY id LIMIT 10, 5;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.SELECT count(*) AS total, min(id), max(id) FROM posts; 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.
SELECT id, title FROM posts ORDER BY id LIMIT 5 OFFSET 10; 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
CREATE TABLE customers (id int PRIMARY KEY, name text);CREATE TABLEINSERT INTO customers VALUES (1, 'O'Brien');ERROR: 42601: syntax error at or near "Brien"
LINE 1: INSERT INTO customers VALUES (1, 'O'Brien');
^SELECT quote_literal($$O'Brien$$) AS sql_literal; 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.
INSERT INTO customers VALUES (1, 'O''Brien');
SELECT * FROM customers;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