.Maui Blog

Postgresql, managing ENUM types

List of useful commands for working with ENUM in postgresql.

Create an enum:

CREATE TYPE languages AS ENUM ('python', 'php', 'java');

Adding an element to an enum:

ALTER TYPE languages ADD VALUE 'go';
ALTER TYPE languages ADD VALUE 'scala' BEFORE 'php';
ALTER TYPE languages ADD VALUE 'javascript' AFTER 'java';

Remove an element from an enum:

DELETE FROM pg_enum
WHERE enumlabel = 'javascript'
AND enumtypid = ( SELECT oid FROM pg_type WHERE typname = 'languages');

Display all elements in an enum:

SELECT enum_range(NULL::languages)

Delete an enum:

DROP TYPE languages

Exit mobile version