skip to content

reading an error

ERROR, the caret, DETAIL, HINT and the five-character SQLSTATE. postgres errors say more than they look.

address
everything.start.reading_errors
part
part 1, start here
chapter
chapter 6 of 7
reading time
8 min

a postgres error is more than its first line. there's the message, a caret under the exact spot it gave up, often a DETAIL (the facts of this case) and a HINT (a guess at the fix), and always a five-character SQLSTATE whose first two characters say what kind of problem it is.

read all of it. it's usually right, and it usually tells you the fix.

the picture

you know how an error message feels like being shouted at in red, so you skim the first line and start guessing?

think of a lab report instead. there's a one-line result at the top (the message), a pointer to where in the sample it was found (the caret), the measurements (DETAIL), the doctor's suggestion (HINT), and a diagnosis code that means the same thing in every hospital (the SQLSTATE). you'd read the whole report. read the whole error.

error =
part =
shop=# SELECT nmae FROM customers WHERE id = 42;
ERROR: 42703: column "nmae" does not exist
LINE 1: SELECT nmae FROM customers WHERE id = 42;
^
HINT: Perhaps you meant to reference the column "customers.name".
LOCATION: errorMissingColumn, parse_relation.c:3870

sqlstatefive characters that name the problem, the same in every language postgres speaks. the first two are the class (42: you wrote something postgres can't accept, 23: the data broke a rule). code checks these, never the words.

real output, a postgres 18.6 server · psql 18.6, \set VERBOSITY verbose, on a throwaway server
pick an error, then a part. these are psql's printouts with \set VERBOSITY verbose, which shows every field the server sent. the run buttons on this site show the everyday ones: severity, SQLSTATE, message, caret, DETAIL and HINT.

the caret

the caret points at the character where postgres stopped understanding you. it's a position in your statement, so it works across lines:

sqldb=shop
SELECT nmae
FROM customers
WHERE id = 42;
output, from postgres 18.6
ERROR 42703: column "nmae" does not exist
1 │ SELECT nmae
at line 1, column 8
HINT: Perhaps you meant to reference the column "customers.name".

column nmae doesn't exist, the caret sits under it, and the HINT guesses what you meant: postgres compares the name against every column in scope and suggests a close one. a value of the wrong type gets the caret too, under the literal that didn't fit:

sqldb=shop
SELECT name FROM customers WHERE id = 'forty-two';
output, from postgres 18.6
ERROR 22P02: invalid input syntax for type integer: "forty-two"
1 │ SELECT name FROM customers WHERE id = 'forty-two';
at line 1, column 39

but "where postgres stopped understanding" isn't always "where you went wrong". the caret marks the first spot that made no sense, and sometimes the mistake is just before it:

predict the output
sql
SELECT name, city, FROM customers;
sqldb=shop
SELECT name, city, FROM customers;
output, from postgres 18.6
ERROR 42601: syntax error at or near "FROM"
1 │ SELECT name, city, FROM customers;
at line 1, column 20

DETAIL and HINT

DETAIL is facts: which key, which row, which value. it turns "something about a foreign key" into "this exact value is missing from that exact table":

sqldb=shop
INSERT INTO orders (customer_id, status, ordered_at)
VALUES (99999, 'placed', '2026-10-28 09:00+00');
output, from postgres 18.6
ERROR 23503: insert or update on table "orders" violates foreign key constraint "orders_customer_id_fkey"
DETAIL: Key (customer_id)=(99999) is not present in table "customers".

the same constraint from the other side: Ada has orders, so she can't be deleted while they point at her.

sqldb=shop
DELETE FROM customers WHERE id = 42;
output, from postgres 18.6
ERROR 23503: update or delete on table "customers" violates foreign key constraint "orders_customer_id_fkey" on table "orders"
DETAIL: Key (id)=(42) is still referenced from table "orders".

and for a CHECK, DETAIL prints the whole row that would have been written, so you can see which value broke it (here, a status that isn't on the list):

sqldb=shop
UPDATE orders SET status = 'lost' WHERE id = 1;
output, from postgres 18.6
ERROR 23514: new row for relation "orders" violates check constraint "orders_status_check"
DETAIL: Failing row contains (1, 1, lost, 2025-07-01 01:24:00+00, null).

HINT is a suggestion, and postgres's own style guide says hints are for advice that might not always apply. this one is half right: the fix isn't a cast, it's spelling length correctly.

sqldb=shop
SELECT lenght(name) FROM customers WHERE id = 42;
output, from postgres 18.6
ERROR 42883: function lenght(text) does not exist
1 │ SELECT lenght(name) FROM customers WHERE id = 42;
at line 1, column 8
HINT: No function matches the given name and argument types. You might need to add explicit type casts.

some errors come with neither, because the message says it all. this one you'll meet a lot, and GROUP BY explains why it's right:

sqldb=shop
SELECT country, name, count(*) FROM customers GROUP BY country;
output, from postgres 18.6
ERROR 42803: column "customers.name" must appear in the GROUP BY clause or be used in an aggregate function
1 │ SELECT country, name, count(*) FROM customers GROUP BY country;
at line 1, column 17

SQLSTATE

every error carries a five-character code. the first two characters are the class, the last three the exact condition: 23505 is class 23 (integrity constraint violation), condition 505 (unique violation). the scheme comes from the SQL standard, so other databases that follow it use the same classes; postgres added its own codes where the standard had none, and marks them with a P (42P01, 22P02, 25P02).

23class: integrity constraint violation
505unique_violation

who defined it: not the standard: borrowed (often from IBM's DB2) or invented. in pl/pgsql you can catch it as unique_violation.

all 262 codes and 43 classes, from postgres 18.6's errcodes.txt
pick a code or type one. all 262 codes postgres 18.6 defines are in here, with the name pl/pgsql knows each one by, and who defined it: the standard, postgres, or somebody else.

why care about a code when there's a perfectly good message? because the message is for people and the code is for programs. the message can be translated (lc_messages) and can be reworded between versions; the code doesn't change. code that handles errors checks the code. pl/pgsql even lets you catch errors by name:

sqldb=shop
DO $$
BEGIN
  INSERT INTO customers (name, email, country, created_at)
  VALUES ('Ada Again', 'ada@example.com', 'GB', '2026-10-28 09:00+00');
EXCEPTION WHEN unique_violation THEN
  RAISE NOTICE 'caught %: %', SQLSTATE, SQLERRM;
END $$;
output, from postgres 18.6
NOTICE: caught 23505: duplicate key value violates unique constraint "customers_email_key"
DO

unique_violation is 23505's condition name. your driver gives you the same code (err.code in node-postgres, e.sqlstate in psycopg, getSQLState() in JDBC), plus the constraint name as a separate field.

where errors come from

the code doesn't say which part of postgres raised an error, but psql's verbose mode does: LOCATION is the C function and source line that gave up. here are five errors, one after another, on a real server:

psql
shop=# \set VERBOSITY verbose
shop=# SELEC name FROM customers;
ERROR:  42601: syntax error at or near "SELEC"
LINE 1: SELEC name FROM customers;
        ^
LOCATION:  scanner_yyerror, scan.l:1240
shop=# SELECT nmae FROM customers WHERE id = 42;
ERROR:  42703: column "nmae" does not exist
LINE 1: SELECT nmae FROM customers WHERE id = 42;
               ^
HINT:  Perhaps you meant to reference the column "customers.name".
LOCATION:  errorMissingColumn, parse_relation.c:3870
shop=# SELECT name FROM customers WHERE id = 'forty-two';
ERROR:  22P02: invalid input syntax for type integer: "forty-two"
LINE 1: SELECT name FROM customers WHERE id = 'forty-two';
                                              ^
LOCATION:  pg_strtoint32_safe, numutils.c:618
shop=# INSERT INTO customers (name, email, country, created_at) VALUES ('Ada Again', 'ada@example.com', 'GB', '2026-10-28');
ERROR:  23505: duplicate key value violates unique constraint "customers_email_key"
DETAIL:  Key (email)=(ada@example.com) already exists.
SCHEMA NAME:  public
TABLE NAME:  customers
CONSTRAINT NAME:  customers_email_key
LOCATION:  _bt_check_unique, nbtinsert.c:673
shop=# SELECT 1 / 0;
ERROR:  22012: division by zero
LOCATION:  int4div, int.c:872

hover a stage.

hover a stage. the same five errors, placed where postgres raised them (from their LOCATION lines). everything left of execute fails before a single row is read.

an error during execution can come after real work was done. it doesn't matter: a statement that fails leaves nothing behind. this UPDATE divides every price by 100 - id, so it gets through the products before 100, then hits zero:

sqldb=shop
UPDATE products SET price = price / (100 - id);
SELECT id, price FROM products WHERE id IN (1, 99, 101) ORDER BY id;
output, from postgres 18.6
#1 · ERROR 22012
ERROR 22012: division by zero
#2 · SELECT 3
idint4pricenumeric
149.00
99135.48
101177.05
(3 rows)

product 1 still costs 49.00. every row the UPDATE had already changed was thrown away with it.

the error after the error

inside a transaction, the first error breaks the whole transaction, and postgres refuses everything after it until you end it:

sqldb=shop
BEGIN;
SELECT nmae FROM customers;
SELECT name FROM customers WHERE id = 42;
ROLLBACK;
SELECT name FROM customers WHERE id = 42;
output, from postgres 18.6
#1 · BEGIN
#2 · ERROR 42703
ERROR 42703: column "nmae" does not exist
2 │ SELECT nmae FROM customers;
at line 2, column 8
HINT: Perhaps you meant to reference the column "customers.name".
#3 · ERROR 25P02
ERROR 25P02: current transaction is aborted, commands ignored until end of transaction block
#4 · ROLLBACK
#5 · SELECT 1
nametext
Ada Lovelace
(1 row)

25P02 (in failed sql transaction) is never the real problem. scroll up to the first error, fix that, and ROLLBACK. psql even tells you in the prompt (shop=!#, see psql), and BEGIN, COMMIT and ROLLBACK covers savepoints, which let you recover from one error without losing the whole transaction.

keylines

  • read the whole error: message, caret, DETAIL, HINT, SQLSTATE.
  • the caret marks where postgres gave up. the mistake can be one step to its left.
  • DETAIL is facts, HINT is a guess.
  • the first two characters of a SQLSTATE are the class. programs should check the code, not the words.
  • a failed statement leaves nothing behind, and 25P02 means an earlier error. fix that one.