Skip to content

Instantly share code, notes, and snippets.

@fitzy1321
Last active September 7, 2025 01:28
Show Gist options
  • Select an option

  • Save fitzy1321/b230d02a04d4715b252bf21bf3c37dbc to your computer and use it in GitHub Desktop.

Select an option

Save fitzy1321/b230d02a04d4715b252bf21bf3c37dbc to your computer and use it in GitHub Desktop.
Postgres find all enum types
-- Return a bool if enum exists
CREATE OR REPLACE FUNCTION enum_exists(enum_name text, enum_schema text DEFAULT 'public')
RETURNS boolean
LANGUAGE sql
AS $$
SELECT EXISTS (
SELECT 1
FROM pg_type t
JOIN pg_namespace n ON t.typnamespace = n.oid
WHERE t.typname = enum_name
AND n.nspname = enum_schema
);
$$;
-- Find all enum types
SELECT n.nspname AS enum_schema,
t.typname AS enum_name,
string_agg(e.enumlabel, ', ' ORDER BY e.enumsortorder) 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
ORDER BY n.nspname, t.typname;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment