Search code examples
postgresqlvarchar

Inputted string must be exactly 10 characters


I am creating a column in postgres that must only accept 10 characters. VARCHAR(10) works in the sense that it allows data to be a maximum of 10 characters, however i cant seem to figure out a way to define a minimum.

How can one do this using 11.5 psql?


Solution

  • Add a constraint to the table that checks the length:

    ALTER TABLE tbl ADD CONSTRAINT check_min_len CHECK (length(col) >= 10);