OPessoa Blog

How to create enums with postgres.

Posted on 2016-11-17 16:02:23

Enum, an enumerated type is a data type consisting of a set of named values called elementsmembersenumeral, or enumerators of the type.[Wiki]
Sometimes we need fixed values as constants in a db column. Enum solves this issue.

Like the moon fases:
  • New
  • Waxing Crescent
  • First quarter
  • Waxing Guibbous
  • Full
  • Waning Guibbous
  • Last Quarter
  • Waning Crescent

So, we can do:

CREATE TYPE moon_phase AS ENUM ('New','Waxing Crescent','First quarter','Waxing Guibbous','Full','Waning Guibbous','Last Quarter','Waning Crescent');
CREATE TABLE person ( name text, birthday timestamp, moon moon_phase);