Postgres check if enum value exists

To check if an enum value exists in PostgreSQL, you can use the enum_range function to fetch all the possible values of the enum type and then check if the value you are looking for exists in the returned list.

Here’s an example of how you can perform this check:


-- Let's say you have an enum type called 'status' with possible values 'active', 'inactive', 'deleted'
SELECT unnest(enum_range(NULL::status)) AS enum_value
-- This will return a table with a single column 'enum_value' containing all the possible enum values

-- Now you can check if a specific enum value exists by filtering the result
SELECT EXISTS (
  SELECT 1
  FROM unnest(enum_range(NULL::status)) AS enum_value
  WHERE enum_value = 'active'
) AS value_exists
-- This will return 'true' if the value 'active' exists in the enum type, otherwise 'false'
  

Leave a comment