You can define ASCII as ordinal 1 to 127 for this purpose, so the following query will identify a string with the values "non-ascii":
SELECT exists(SELECT 1 from regexp_split_to_table('abcdéfg','') x where ascii(x) not between 1 and 127);
but this is unlikely to be super-efficient, and using subqueries will force you to do this in a trigger, not in a CHECK constraint.
Instead, I would use a regex. If you want all printable characters, you can use a range in a control limit, for example:
CHECK (my_column ~ '^[ -~]*$')
this will fit everything from place to tilde , which is an ASCII printable range.
If you want all ASCII printable and non-printable, you can use byte screens :
CHECK (my_column ~ '^[\x00-\x7F]*$')
The most strictly correct approach is convert_to(my_string, 'ascii') , and let an exception occur if it fails ... but PostgreSQL does not offer ASCII encoding (i.e. 7 bits), so the approach is not possible.
source share