Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL Error 23514: Check Violation Fix

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

PostgreSQL error 23514 means a CHECK constraint was violated: the data you tried to insert or update does not satisfy the condition defined for that column or table. The fix is to either change the data to meet the constraint, or alter or drop the constraint if it is no longer correct. You do not need to restart the server or restore a backup in most cases; the error is a data validation failure, not a corruption or crash.

What You'll Need

  • Access to a PostgreSQL client (psql, pgAdmin, DBeaver, or your application's database tool) with a user that can read the table definition and, if needed, alter constraints.
  • The exact error message, which usually names the constraint and the table.
  • The values you were trying to insert or update, so you can compare them with the constraint definition.

Step-by-Step

  1. Read the full error message. PostgreSQL typically reports something like ERROR: new row for relation "orders" violates check constraint "orders_total_positive" and may include DETAIL: Failing row contains (...). Note the table name and constraint name exactly. If your client hides details, run the same statement in psql to see the full output.
  2. Find the constraint definition. In psql, run \d+ table_name and look for the CHECK constraint. Alternatively, query information_schema.check_constraints or pg_constraint joined to pg_class to see the expression. For example: SELECT conname, pg_get_constraintdef(oid) FROM pg_constraint WHERE conrelid = 'table_name'::regclass AND contype = 'c'; This shows the exact condition, such as CHECK (total >= 0) or CHECK (status IN ('new','paid','shipped')).
  3. Compare your data to the condition. Check the value you supplied for the constrained column. Common mismatches: negative numbers where the constraint requires non-negative, NULL where the constraint requires a value (unless the check allows NULL), a string not in the allowed list, or a date outside a range. If the failing row is shown in DETAIL, compare each column.
  4. Correct the data and retry. Adjust your INSERT or UPDATE statement so the value satisfies the condition. For example, if the constraint is CHECK (quantity > 0), change a zero quantity to a positive number or handle the zero case in application logic. If the value comes from a form or API, add validation before the database call to give a clearer message.
  5. If the constraint is wrong, alter it. Only do this if you are certain the rule is outdated or incorrect. To replace a constraint, first drop it: ALTER TABLE table_name DROP CONSTRAINT constraint_name; then add the corrected one: ALTER TABLE table_name ADD CONSTRAINT constraint_name CHECK (new_condition); You can also use NOT VALID to add a constraint without checking existing rows, then validate later with ALTER TABLE ... VALIDATE CONSTRAINT .... Dropping or altering a constraint changes data integrity rules; test on a staging copy first and back up the table if it is large or critical.
  6. Check existing rows if you are adding a constraint. Adding a CHECK constraint scans the table and fails if any existing row violates it. Run a SELECT with the inverse condition to find offenders before adding the constraint. For example, SELECT * FROM table_name WHERE NOT (condition); Fix or exclude those rows first.
  7. Update application code and migrations. If the constraint is part of a migration, ensure the migration order and data cleanup steps are correct. If your ORM generates the constraint, check that the model definition matches the database. Keep the constraint name stable so error messages remain useful.

Common Mistakes

  • Dropping the constraint as a first response. This removes a safety check and can allow bad data into the table. Fix the data or correct the constraint definition instead.
  • Ignoring NULL semantics. A CHECK constraint passes if the expression evaluates to NULL. For example, CHECK (price > 0) allows NULL price. If you need to forbid NULL, add NOT NULL or write the check as CHECK (price IS NOT NULL AND price > 0).
  • Assuming the error is a server problem. Error 23514 is a data validation error. Restarting PostgreSQL will not fix it.
  • Forgetting about triggers or rules. A trigger may insert or update rows that violate a constraint, causing the error even when your direct statement looks correct. Check for triggers on the table.
  • Using the wrong constraint name. Constraint names are case-sensitive when quoted and may be auto-generated. Always copy the name from the error or the catalog query.
  • Altering a constraint without a backup. Dropping and re-adding a constraint can lock the table and, if the new condition is stricter, fail partway. Back up and test on a copy first.

Tips

  • Name your constraints explicitly in migrations (for example CONSTRAINT orders_total_positive CHECK (total >= 0)) so errors are easier to trace.
  • Use NOT VALID when adding a constraint to a large table to avoid a long lock, then validate during a maintenance window.
  • Add application-level validation that mirrors the database constraint to catch problems before they reach PostgreSQL.
  • Log the failing row values in your application when this error occurs; it speeds up diagnosis without exposing sensitive data in logs.
  • Review CHECK constraints after schema changes or data imports; imported data often violates rules that were fine for normal traffic.

Troubleshooting

If the error persists after correcting the data, confirm you are connected to the right database and schema; the same table name can exist in multiple schemas. Check for triggers that modify the row after your statement. If you cannot find the constraint with \d+, query pg_constraint directly, as the constraint may be inherited or defined on a parent table. If you are adding a constraint and it fails, run the inverse SELECT to list offending rows, fix them, then retry. If you must drop a constraint in production, do it during low traffic, take a backup first, and re-add the corrected constraint immediately. If the error appears only from one application, inspect that application's INSERT and UPDATE statements and its ORM model for mismatched column types or default values. If you are unsure whether a constraint is still needed, review the business rule with the team that owns the data before removing it.

Frequently Asked Questions

What does PostgreSQL error 23514 mean?

It is the SQLSTATE for check_violation: a row failed a CHECK constraint defined on a table or column.

Can I disable a CHECK constraint temporarily?

PostgreSQL does not have a per-constraint disable switch like some other databases. You can drop the constraint and re-add it, or use ALTER TABLE ... DISABLE TRIGGER only for triggers, not CHECK constraints. Dropping is the practical option, but it removes validation for all sessions until re-added.

Why does my CHECK constraint allow NULL values?

In SQL, a CHECK constraint passes when the condition evaluates to NULL, which happens when a column is NULL. Add NOT NULL or include an explicit IS NOT NULL test if NULLs should be rejected.

Does error 23514 affect existing rows?

No, it is raised for the row being inserted or updated. Existing rows are only checked when you add or validate a constraint.

How do I find the constraint name?

The error message usually includes it. You can also run \d+ table_name in psql or query pg_constraint for the table.

Will restarting PostgreSQL fix it?

No. The error comes from data not meeting a rule, so you must change the data or the constraint.