// PostgreSQL error reference

PostgreSQL 42601: syntax error at or near

PostgreSQL couldn't parse the statement. The token it names, and the caret under it, mark where parsing failed; the real mistake is often just before it.

SQLSTATE
42601
Name
syntax_error
Category
Syntax Error or Access Rule Violation
Error message
syntax error at or near "FROM"
LINE 1: SELECT id, name, FROM users;
                         ^

What it means

The parser read the statement up to the token in the message and found something that can't come next. syntax error at end of input means the statement stopped where more was expected, such as a missing closing parenthesis.

The position is where PostgreSQL noticed the problem, which is often one token after the actual mistake: a trailing comma before FROM, a missing comma between columns, or a reserved word used as a name.

Common causes

  • A trailing comma before FROM, ) or WHERE, or a missing comma between items.
  • A reserved word used as a column or table name without quotes, such as user, order or group.
  • Syntax from another database: TOP, [brackets], backticks, LIMIT x, y, IFNULL.
  • Unbalanced parentheses or quotes, which shows up as an error at end of input.
  • Client-side placeholders (?, :name, %s) sent to the server without the driver replacing them; PostgreSQL itself uses $1, $2.

How to fix it

  1. Look at the token named in the message and the one just before it; remove the stray comma or add the missing one.

    SELECT id, name FROM users;
  2. Quote reserved words used as identifiers, or rename them.

    SELECT "user", "order" FROM audit_log;
  3. Rewrite other dialects' syntax in PostgreSQL form.

    -- SQL Server: SELECT TOP 10 * FROM [orders]
    SELECT * FROM orders LIMIT 10;
    -- MySQL: LIMIT 20, 10
    SELECT * FROM orders LIMIT 10 OFFSET 20;
  4. For PL/pgSQL bodies, use dollar quoting so quotes inside the body don't end the string.

    CREATE FUNCTION greet(name text) RETURNS text
    LANGUAGE plpgsql AS $$
    BEGIN
        RETURN 'Hello, ' || name;
    END;
    $$;

Reproduce it safely

SELECT id, FROM (VALUES (1)) v(id);  -- ERROR: syntax error at or near "FROM"

Related errors

More syntax error or access rule violation errors

The code and message text are from the PostgreSQL documentation, Appendix A: PostgreSQL Error Codes. Browse the full PostgreSQL error list or search every database error.

// fewer errors, faster fixes

Work on PostgreSQL in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for PostgreSQL and the other databases you use.