Last active
September 7, 2025 01:28
-
-
Save fitzy1321/b230d02a04d4715b252bf21bf3c37dbc to your computer and use it in GitHub Desktop.
Postgres find all enum types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- 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