Symptoms
A row failed a CHECK constraint.
- The error is written to the server log and returned to the client carrying
SQLSTATE 23514. - Any driver (libpq, JDBC, psycopg, npgsql, pgx) surfaces this code in its error object so you can branch on it programmatically.
- PL/pgSQL can trap it by name:
EXCEPTION WHEN check_violation THEN.
Environment
Severity: ERROR | PostgreSQL versions: 12, 13, 14, 15, 16, 17
Reproduce with the exact statement and read the full message in the server log (raise log_min_messages / set log_min_error_statement for more context).
Root Cause
The value did not satisfy a CHECK expression on the table or a domain.
Common causes:
- Data outside the allowed range or set.
- A newly added constraint that existing or incoming data violates.
- A domain constraint on the type.
Diagnostic Queries
Recovery
Steps to resolve 23514:
- Read the constraint:
SELECT conname, pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = '...';. - Correct the input data to satisfy the rule.
- If the rule itself is wrong, alter or drop the constraint.
- On large tables add it as
NOT VALID, fix data, thenVALIDATE CONSTRAINT.
Reference: PostgreSQL error codes — Class 23 (Integrity Constraint Violation).
Was this helpful?
Thanks — noted. This helps keep the database accurate.