Skip to content

Instantly share code, notes, and snippets.

@jrgifford
Forked from hauleth/pg_graph.sh
Created May 29, 2025 14:18
Show Gist options
  • Save jrgifford/67da8e35feb54662bb2580bc23a5326a to your computer and use it in GitHub Desktop.
Save jrgifford/67da8e35feb54662bb2580bc23a5326a to your computer and use it in GitHub Desktop.
Script to generate DOT graph of dependencies between tables in PostgreSQL
#!/bin/sh
psql -qX "$@" <<EOF
\t on
\timing off
\echo 'Digraph F{'
\echo 'ranksep=1.0; size="18.5, 15.5"; rankdir=LR;'
SELECT
'"' || tc.table_name || '"->"' || ccu.table_name || '" [label="' || tc.constraint_name || '"];'
FROM
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 '}'
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment