Is it possible to add a custom constraint in PostgreSQL

postgresql

Now I have a table store some rss subscribe url, sometimes the url end with '/', now I replace all url without end with '/'. to make sure the sub url did not end with '/'. I want to add a custom constraint in the rss sub source table. If the sub url end with '/', throw error to the app. I have read the PostgreSQL 13 docs, and found it have many constraint like uniq constraint check constraint with the int data type. but how to add an constraint with a string data type and make sure the string did not end with '/'? is there any way to do this action?

Best Answer

ALTER TABLE t ADD CONSTRAINT c
    CHECK ( url NOT LIKE '%/');

As suggested by VĂ©race in comment, an alternative would be a generated column as:

ALTER TABLE T ADD COLUMN proper_url TEXT 
    GENERATED ALWAYS AS ( regexp_replace(url, '/$', '')) STORED;