Skip to content

Instantly share code, notes, and snippets.

@FazelMan
FazelMan / pascal_to_camel.sql
Created May 20, 2023 12:55
Rename PascalCase tables and columns to snake_case in PostgreSQL
-- Rename tables
DO $$
DECLARE
table_record RECORD;
BEGIN
-- Loop through each table
FOR table_record IN (SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE') LOOP
EXECUTE FORMAT('ALTER TABLE public.%I RENAME TO %I', table_record.table_name, lower(regexp_replace(table_record.table_name, '(.)([A-Z])', '\1_\2', 'g')));
END LOOP;
END $$;