Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-29 · container image postgres:17
23514 (check_violation) means a CHECK constraint rejected a row. For INSERT and UPDATE the message is "new row for relation ... violates check constraint ..." and DETAIL shows the failing row. For ALTER TABLE ADD CONSTRAINT the message is different: "... is violated by some row", because rows already in the table fail the check.
The constraint name in the message is the key: check its definition with pg_get_constraintdef. 4 cases reproduced on PostgreSQL 17.
01INSERT with a value outside the CHECK (negative price)
price int CHECK (price >= 0) and the row has -500. The constraint got the automatic name products_price_check (table_column_check). DETAIL: Failing row contains (1, refund adj, -500).
Setup
CREATE TABLE products (id int PRIMARY KEY, name text, price int CHECK (price >= 0));CREATE TABLEINSERT INTO products VALUES (1, 'refund adj', -500);ERROR: 23514: new row for relation "products" violates check constraint "products_price_check"
DETAIL: Failing row contains (1, refund adj, -500).
SCHEMA NAME: lab_a
TABLE NAME: products
CONSTRAINT NAME: products_price_checkSELECT conname, pg_get_constraintdef(oid) FROM pg_constraint WHERE conrelid = 'products'::regclass AND contype = 'c'; conname | pg_get_constraintdef
----------------------+----------------------
products_price_check | CHECK ((price >= 0))
(1 row)pg_constraint with pg_get_constraintdef shows the rule: CHECK ((price >= 0)). Insert a value that satisfies it (here 0). If negative values are valid for your business, the constraint itself has to change; that was not tested here.
INSERT INTO products VALUES (1, 'refund adj', 0);
SELECT * FROM products;INSERT 0 1
id | name | price
----+------------+-------
1 | refund adj | 0
(1 row)02ALTER TABLE ADD CONSTRAINT CHECK when existing rows break it
Adding CHECK (age BETWEEN 0 AND 150) checks every existing row. Two rows (age -1 and 200) fail, so the ALTER is rejected with "is violated by some row". No DETAIL row is shown, so you have to find them yourself.
Setup
CREATE TABLE members (id int PRIMARY KEY, email text, age int);
INSERT INTO members VALUES (1, 'a@x.com', 30), (2, 'b@x.com', -1), (3, 'c@x.com', 200);CREATE TABLE
INSERT 0 3ALTER TABLE members ADD CONSTRAINT members_age_chk CHECK (age BETWEEN 0 AND 150);ERROR: 23514: check constraint "members_age_chk" of relation "members" is violated by some row
SCHEMA NAME: lab_a
TABLE NAME: members
CONSTRAINT NAME: members_age_chkSELECT * FROM members WHERE NOT (age BETWEEN 0 AND 150); id | email | age
----+---------+-----
2 | b@x.com | -1
3 | c@x.com | 200
(2 rows)ALTER TABLE members ADD CONSTRAINT members_age_chk CHECK (age BETWEEN 0 AND 150) NOT VALID;
UPDATE members SET age = NULL WHERE NOT (age BETWEEN 0 AND 150);ALTER TABLE
UPDATE 2Find the rows with WHERE NOT (age BETWEEN 0 AND 150) (ids 2 and 3). Add the constraint with NOT VALID (applies to new rows only), clean up the data (here set to NULL; NULL passes a CHECK), then ALTER TABLE members VALIDATE CONSTRAINT members_age_chk; succeeded.
ALTER TABLE members VALIDATE CONSTRAINT members_age_chk;
SELECT * FROM members ORDER BY id;ALTER TABLE
id | email | age
----+---------+-----
1 | a@x.com | 30
2 | b@x.com |
3 | c@x.com |
(3 rows)03UPDATE that pushes a value out of range (stock below 0)
Stock is 3 and qty = qty - 5 would make -2. DETAIL: Failing row contains (bolt, -2). The whole UPDATE fails.
Setup
CREATE TABLE stock (item text PRIMARY KEY, qty int NOT NULL, CONSTRAINT stock_qty_check CHECK (qty >= 0));
INSERT INTO stock VALUES ('bolt', 3);CREATE TABLE
INSERT 0 1UPDATE stock SET qty = qty - 5 WHERE item = 'bolt';ERROR: 23514: new row for relation "stock" violates check constraint "stock_qty_check"
DETAIL: Failing row contains (bolt, -2).
SCHEMA NAME: lab_a
TABLE NAME: stock
CONSTRAINT NAME: stock_qty_checkSELECT item, qty FROM stock WHERE item = 'bolt'; item | qty
------+-----
bolt | 3
(1 row)Put the condition in the WHERE: UPDATE stock SET qty = qty - 5 WHERE item = 'bolt' AND qty >= 5; returned UPDATE 0 (not enough stock, no error), and qty - 2 ... AND qty >= 2 returned UPDATE 1, leaving 1. Check the row count in the application.
UPDATE stock SET qty = qty - 5 WHERE item = 'bolt' AND qty >= 5;
UPDATE stock SET qty = qty - 2 WHERE item = 'bolt' AND qty >= 2;
SELECT * FROM stock;UPDATE 0
UPDATE 1
item | qty
------+-----
bolt | 1
(1 row)04New status value not in CHECK (status IN (...))
The allowed list is NEW, PAID, SHIPPED. Inserting CANCELLED breaks orders_status_check.
Setup
CREATE TABLE orders (id int PRIMARY KEY, status text CHECK (status IN ('NEW', 'PAID', 'SHIPPED')));CREATE TABLEINSERT INTO orders VALUES (1, 'CANCELLED');ERROR: 23514: new row for relation "orders" violates check constraint "orders_status_check"
DETAIL: Failing row contains (1, CANCELLED).
SCHEMA NAME: lab_a
TABLE NAME: orders
CONSTRAINT NAME: orders_status_checkALTER TABLE orders DROP CONSTRAINT orders_status_check;
ALTER TABLE orders ADD CONSTRAINT orders_status_check CHECK (status IN ('NEW', 'PAID', 'SHIPPED', 'CANCELLED'));ALTER TABLE
ALTER TABLEDrop the constraint and add it again with the new value: ALTER TABLE orders DROP CONSTRAINT orders_status_check; then ADD CONSTRAINT orders_status_check CHECK (status IN ('NEW', 'PAID', 'SHIPPED', 'CANCELLED')). The INSERT then worked.
INSERT INTO orders VALUES (1, 'CANCELLED');
SELECT * FROM orders;INSERT 0 1
id | status
----+-----------
1 | CANCELLED
(1 row)FAQ
How do I fix violates check constraint in PostgreSQL?
Look at the constraint named in the message with SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = '...'. Then fix the value, or drop and re-add the constraint if the rule itself has to change.
How do I add a CHECK constraint when existing rows violate it?
ADD CONSTRAINT ... NOT VALID first (only new rows are checked), fix the old rows, then ALTER TABLE ... VALIDATE CONSTRAINT.
How do I find the rows that violate a check constraint?
Select with the negated condition, e.g. WHERE NOT (age BETWEEN 0 AND 150). Rows where the column is NULL are not returned and do not violate the CHECK.
How do I add a value to a CHECK IN list?
Drop the constraint and add it again with the longer list, as in the CANCELLED example. The INSERT worked right after.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12