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: 42703: column "nmae" does not existLINE 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.
\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:
SELECT nmae
FROM customers
WHERE id = 42;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:
SELECT name FROM customers WHERE id = 'forty-two';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:
SELECT name, city, FROM customers;SELECT name, city, FROM customers;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":
INSERT INTO orders (customer_id, status, ordered_at)
VALUES (99999, 'placed', '2026-10-28 09:00+00');the same constraint from the other side: Ada has orders, so she can't be deleted while they point at her.
DELETE FROM customers WHERE id = 42;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):
UPDATE orders SET status = 'lost' WHERE id = 1;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.
SELECT lenght(name) FROM customers WHERE id = 42;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:
SELECT country, name, count(*) FROM customers GROUP BY country;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).
who defined it: not the standard: borrowed (often from IBM's DB2) or invented. in pl/pgsql you can catch it as unique_violation.
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:
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 $$;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:
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:872hover a stage.
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:
UPDATE products SET price = price / (100 - id);
SELECT id, price FROM products WHERE id IN (1, 99, 101) ORDER BY id;| idint4 | pricenumeric |
|---|---|
| 1 | 49.00 |
| 99 | 135.48 |
| 101 | 177.05 |
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:
BEGIN;
SELECT nmae FROM customers;
SELECT name FROM customers WHERE id = 42;
ROLLBACK;
SELECT name FROM customers WHERE id = 42;| nametext |
|---|
| Ada Lovelace |
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.