Skip to content

Instantly share code, notes, and snippets.

@honzapav
Created April 20, 2024 09:54
Show Gist options
  • Save honzapav/61ffb11b5a381e346c85e40a5ab67033 to your computer and use it in GitHub Desktop.
Save honzapav/61ffb11b5a381e346c85e40a5ab67033 to your computer and use it in GitHub Desktop.
SELECT
cols.table_schema,
cols.table_name,
cols.column_name,
cols.data_type,
cols.column_default,
(
SELECT CASE
WHEN COUNT(*) > 0 THEN 'Yes'
ELSE 'No'
END
FROM information_schema.table_constraints AS tc
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_schema = tc.constraint_schema
AND ccu.constraint_name = tc.constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY'
AND ccu.table_schema = cols.table_schema
AND ccu.table_name = cols.table_name
AND ccu.column_name = cols.column_name
) AS is_foreign_key,
cols.is_nullable
FROM information_schema.columns AS cols
WHERE cols.table_schema NOT IN ('pg_catalog', 'information_schema')
AND cols.table_catalog = 'staging' -- Replace 'YourDatabaseName' with your actual database name
ORDER BY
cols.table_schema,
cols.table_name,
cols.ordinal_position;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment