Home > Net >  Postgres Enums not working when type is int
Postgres Enums not working when type is int

Time:01-24

I am trying to create an enum like below

create type oddNum as enum (1,3,5,7)

However I am getting the below error.

ERROR: syntax error at or near "1" LINE 1: create type oddNum as enum (1,3,5,7)

Postgres documentation says nothing about enum typing being restricted to varchar. What is the problem here, and why is it not letting me create this enum.

CodePudding user response:

As documented in the manual the elements of an enum can only be strings.

The hint is this part:

CREATE TYPE name AS ENUM
    ( [ 'label' [, ... ] ] )

Note the 'label' which clearly identifies a string in SQL.


If you want to restrict values of a column to odd integers only (maybe in a certain range), a check constraint would be a better option.

  • Related