Skip to content

Instantly share code, notes, and snippets.

@giovannicandido
Last active June 7, 2017 09:07
Show Gist options
  • Save giovannicandido/046a6142eb8872712b948598cc4e01ce to your computer and use it in GitHub Desktop.
Save giovannicandido/046a6142eb8872712b948598cc4e01ce to your computer and use it in GitHub Desktop.
Rename pascal case tables to snake case in postgresql
SELECT ddlsql || regexp_replace(name, E'^_',E'','g') As ddlsql FROM (SELECT 'ALTER TABLE ' || quote_ident(t.table_schema) || '.'
|| quote_ident(t.table_name) || ' RENAME TO ' As ddlsql, quote_ident(lower(regexp_replace(t.table_name, E'([A-Z])', E'\_\\1','g'))) || ';' as name
FROM information_schema.tables As t
WHERE t.table_schema NOT IN('information_schema', 'pg_catalog')
AND t.table_name <> lower(t.table_name)
ORDER BY t.table_schema, t.table_name) x;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment