Sudarshan Kalebere
Sudarshan Kalebere

Reputation: 3929

How can I create enum with default value in postgres

I want to create type enum with default value in postgres, yes I have seen previous questions asked on this enum have checked that. or please anyone can suggest me how to accomplish following table structure basically i need like this

create type status as enum('pending','approved','declined'); //here i want default value to be set as pending.

How to accomplish this in table. I am using codeigniter with postgresql. any suggestions guys?

Upvotes: 2

Views: 5319

Answers (1)

Frank Heikens
Frank Heikens

Reputation: 127116

The default must be in the table definition:

CREATE TYPE status AS ENUM ('pending', 'approved', 'declined');

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

INSERT INTO t(id) VALUES (default) RETURNING *; -- show the result

Upvotes: 8

Related Questions