ERROR: syntax error at or near (PostgreSQL) — how to fix
ERROR: syntax error at or near "user"
LINE 1: SELECT * FROM user WHERE id = 1;
^
Quick fix: Look at the token in quotes and the text just before it. Quote reserved words used as identifiers (SELECT * FROM "user"), replace MySQL syntax (backticks, AUTO_INCREMENT), use your driver's placeholders ($1 in node-postgres, %s in psycopg) and don't use placeholders for table or column names. Remove trailing commas before ) or FROM.
What it means
The parser read your SQL token by token and hit one that can't appear at that point. It reports that token (at or near "user"), and psql also prints the line with a ^ under it. The actual mistake is either that token or something right before it: a missing comma, an extra comma, or an unclosed parenthesis or quote.
Long queries are much easier to read once formatted. Paste them into the SQL formatter to spot misplaced commas and parentheses.
Cause 1: a reserved word used as a table or column name
user, order, group, table, select, limit, offset and others are reserved:
SELECT * FROM user; -- syntax error at or near "user"
SELECT id, order FROM purchases; -- syntax error at or near "order"
Fix: quote the identifier with double quotes, or rename it:
SELECT * FROM "user";
SELECT id, "order" FROM purchases;
ALTER TABLE "user" RENAME TO users; -- better long-term
Double-quoted names are case-sensitive: "User" and "user" are different tables.
Cause 2: MySQL syntax in PostgreSQL
| MySQL | PostgreSQL |
|---|---|
`name` (backticks) |
"name" |
id INT AUTO_INCREMENT PRIMARY KEY |
id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY (or SERIAL) |
LIMIT 10, 20 |
LIMIT 20 OFFSET 10 |
INSERT ... ON DUPLICATE KEY UPDATE |
INSERT ... ON CONFLICT (col) DO UPDATE SET ... |
ENGINE=InnoDB at the end of CREATE TABLE |
remove it |
UNSIGNED |
remove it (use a CHECK (col >= 0) constraint if needed) |
Strings use single quotes. "text" is an identifier in PostgreSQL, so WHERE name = "bob" gives column "bob" does not exist, not a syntax error. Use 'bob'.
Cause 3: the wrong placeholder style
Each driver has its own placeholder syntax, and the wrong one reaches the server as invalid SQL:
// node-postgres: $1, $2 ...
await client.query('SELECT * FROM users WHERE id = $1', [id]);
# psycopg2 / psycopg 3: %s (or %(name)s)
cur.execute("SELECT * FROM users WHERE id = %s", (user_id,))
? placeholders (from SQLite/MySQL examples) don't work with these drivers.
Cause 4: a placeholder used for a table or column name
Parameters can only stand in for values. This fails with syntax error at or near "$1":
client.query('SELECT * FROM $1', [tableName]);
Fix: check the identifier against a list of allowed names in code, then insert it with proper quoting: pg-format's %I in Node, or psycopg.sql.Identifier in Python:
from psycopg2 import sql
cur.execute(sql.SQL("SELECT * FROM {}").format(sql.Identifier(table)))
Cause 5: commas, parentheses and semicolons
SELECT id, name, FROM users; -- near "FROM": trailing comma
CREATE TABLE t (id int, name text,); -- near ")": trailing comma
SELECT 1 SELECT 2; -- near "SELECT": missing ;
A missing semicolon in a script makes the error point at the start of the next statement. A missing closing quote can push the error to the very end of the input (syntax error at end of input).
Cause 6: features from a newer PostgreSQL version
Syntax added in a later release, for example MERGE (PostgreSQL 15), fails as a syntax error on older servers. Check with SELECT version();.
Last updated 2026-12-04.