skip to Main Content

As far as I see the Postgresql documentation on enum types only says:

Enumerated (enum) types are data types that comprise a static, ordered set of values.

So I tried the following:
create type foo as enum (1, 2, 3);, but got

[42601] ERROR: syntax error at or near "1"

Position: 26

My assumption is that enums only support text as value, but I don’t really see this in the documentation. Can someone confirm my assumption? And if it is in the documentation please point me there.

2

Answers


  1. They aren’t text – they are enumerated values.

    Different enum types aren’t comparable either – they are different types.

    => CREATE TYPE traffic_light AS enum ('red', 'yellow', 'green');
    CREATE TYPE
    => CREATE TYPE rainbow AS enum ('red', 'yellow', 'green');
    CREATE TYPE
    => SELECT traffic_light 'red' = rainbow 'red';
    ERROR:  operator does not exist: traffic_light = rainbow
    LINE 1: SELECT traffic_light 'red' = rainbow 'red';
                                       ^
    HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.
    

    Now, a lot of C-derived programming languages have enums as named identifiers for integers, but they are an actual type here.

    You do enter literal enum values by quoting them, like almost every type apart from numbers and booleans. That’s obviously necessary if you want enumerated values to consist of something other than digits. But they aren’t text.

    Login or Signup to reply.
  2. Enum labels have to be string constants.

    This is documented here:

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

    Single quotes imply a string constant.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search