Skip to content

Instantly share code, notes, and snippets.

@jordotech
Created April 24, 2018 14:09
Show Gist options
  • Save jordotech/36978bf4b0c6d3bd29e2687d34dc5f82 to your computer and use it in GitHub Desktop.
Save jordotech/36978bf4b0c6d3bd29e2687d34dc5f82 to your computer and use it in GitHub Desktop.
ER export psql query
SELECT 'postgresql' AS dbms,t.table_catalog,t.table_schema,t.table_name,c.column_name,c.ordinal_position,c.data_type,c.character_maximum_length,n.constraint_type,k2.table_schema,k2.table_name,k2.column_name FROM information_schema.tables t NATURAL LEFT JOIN information_schema.columns c LEFT JOIN(information_schema.key_column_usage k NATURAL JOIN information_schema.table_constraints n NATURAL LEFT JOIN information_schema.referential_constraints r)ON c.table_catalog=k.table_catalog AND c.table_schema=k.table_schema AND c.table_name=k.table_name AND c.column_name=k.column_name LEFT JOIN information_schema.key_column_usage k2 ON k.position_in_unique_constraint=k2.ordinal_position AND r.unique_constraint_catalog=k2.constraint_catalog AND r.unique_constraint_schema=k2.constraint_schema AND r.unique_constraint_name=k2.constraint_name WHERE t.TABLE_TYPE='BASE TABLE' AND t.table_schema NOT IN('information_schema','pg_catalog') \g /tmp/er.txt;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment