Skip to content

Instantly share code, notes, and snippets.

@hauleth hauleth/
Created Apr 17, 2018

What would you like to do?
Script to generate DOT graph of dependencies between tables in PostgreSQL
psql -qX "$@" <<EOF
\t on
\timing off
\echo 'Digraph F{'
\echo 'ranksep=1.0; size="18.5, 15.5"; rankdir=LR;'
'"' || tc.table_name || '"->"' || ccu.table_name || '" [label="' || tc.constraint_name || '"];'
information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu ON
tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage AS ccu ON
ccu.constraint_name = tc.constraint_name
WHERE constraint_type = 'FOREIGN KEY';
\echo '}'

This comment has been minimized.

Copy link

commented Apr 18, 2018

usage example pg_graph db_name | dot -Tsvg > out.svg


This comment has been minimized.

Copy link

commented Mar 27, 2019


Would there be any point to adding functions, procedures, and views?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.