4

How can I access the numeric code of an error in psql?

For example, in this case I would like to get the value 23503 ("foreign_key_violation") as documented here: https://www.postgresql.org/docs/current/errcodes-appendix.html

postgres=# insert into t values('bb');
ERROR:  insert or update on table "t" violates foreign key constraint "fd"

-- How can I see the numeric error code here?

2 Answers 2

6

You can set the VERBOSITY special variable to sqlstate, which will cause the SQLSTATE code to be printed after any failed statement in the current session:

\set VERBOSITY sqlstate

Alternatively, you can print the SQLSTATE code on demand:

\echo :LAST_ERROR_SQLSTATE
5

mustaccio's answer is good, but if all that you need is to see the SQLSTATE of the error you just got in psql, the simplest way is to run

\errverbose

Note that the SQLSTATE is not always a number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.