title: Sort tables in order of dependency - Postgres
body: <p>Purpose is to allow a insert script to add data to all tables in schema, such that it doesn't create any conflict in constraint. i take tables from information_schema.tables and take constraints from information_schema.table_constraints but not sure how to compare to sort tables in order of foreign key constraints. Please favour. </p>
  
  <p>The below produces duplicate table names:</p>
  
  <pre><code>select a.table_name,b.ordinal_position
  from information_schema.tables a left outer join
       information_schema.key_column_usage b
       on a.table_name = b.table_name
  </code></pre>