HiveBrain v1.2.0
Get Started
← Back to all entries
debugsqlMinor

How can I get a Postgresql error number in psql?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlcanerrornumberpsqlgethow

Problem

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?

Solution

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

Code Snippets

\set VERBOSITY sqlstate
\echo :LAST_ERROR_SQLSTATE

Context

StackExchange Database Administrators Q#318487, answer score: 6

Revisions (0)

No revisions yet.