40
  types = { # add your custom types here
          'attendance': ('Notconfirmed','Coming', 'Notcoming', 'Maycome',),
          }

  CREATE TYPE attendance AS ENUM types;

The above query creates enum type attendance with enumlabels mentioned in types. How to create a type with default label? In this case the I want to create attendance type with default value Notconfirmed.

smac89
  • 39,374
  • 15
  • 132
  • 179
RaviKiran
  • 753
  • 1
  • 6
  • 13

3 Answers3

88

I was trying the same as you, and I got answer in stackoverflow only, It is possible to create ENUM with default value. Here is what I got for you.

CREATE TYPE status AS ENUM ('Notconfirmed','Coming', 'Notcoming', 'Maycome');

CREATE TABLE t (
    id serial,
    s status default 'Notconfirmed' -- <==== default value
);

INSERT INTO t(id) VALUES (default) RETURNING *; 

This worked for me like a charm.

Sudarshan Kalebere
  • 3,813
  • 3
  • 34
  • 64
  • 3
    Or for an existing table ``` CREATE TYPE statuses AS ENUM ('Notconfirmed','Coming', 'Notcoming', 'Maycome'); ALTER TABLE t ADD status statuses default 'Notconfirmed'; ``` – Michael Brawn May 21 '19 at 14:24
  • 3
    ERROR: cannot use column reference in DEFAULT expression – The Fool Dec 11 '21 at 12:09
8

In addition to Sudarshan's words...

In case someone needs an example in different schema:

CREATE TABLE schema_name.table_name ( -- 
    id serial,
    s schema_name.type_name default 'Notconfirmed'::schema_name.type_name
);
Norman Edance
  • 352
  • 4
  • 14
-1

I am not sure why we need to have below query in our table. I have tried without this line still works.

INSERT INTO t(id) VALUES (default) RETURNING *;
ferdousulhaque
  • 179
  • 2
  • 10