Useful datatype commands
Basic commands
-- Show all types with type name like
SELECT typname FROM pg_type WHERE typname LIKE '%statustype%';
-- Show all enum types with value like
SELECT * FROM pg_enum WHERE enumlabel LIKE '%TODO%';
-- Select all values of data type
SELECT e.enumlabel, e.enumsortorder FROM pg_enum e JOIN pg_type t ON e.enumtypid = t.oid WHERE t.typname = 'statustype';
-- Add value to an existing data type
ALTER TYPE statustype ADD VALUE 'NEW_STATUS';
-- Remove value from data type
DELETE FROM pg_enum WHERE enumlabel = 'NEW_STATUS' AND enumtypid = (SELECT oid FROM pg_type WHERE typname = 'statustype');
-- Drop data type
DROP TYPE statustype;
Query all enum types
select n.nspname as enum_schema,
t.typname as enum_name,
json_agg(e.enumlabel) as enum_values
from pg_type t
join pg_enum e on t.oid = e.enumtypid
join pg_catalog.pg_namespace n ON n.oid = t.typnamespace
group by n.nspname, t.typname;
Find all columns using specific datatype
select col.table_schema,
col.table_name,
col.column_name,
col.udt_name,
col.data_type
from information_schema.columns col
join information_schema.tables tab on tab.table_schema = col.table_schema
and tab.table_name = col.table_name
and tab.table_type = 'BASE TABLE'
where col.udt_name = 'statustype'
and col.table_schema not in ('information_schema', 'pg_catalog')
order by col.table_schema,
col.table_name,
col.ordinal_position;