-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplete_database_schema_dump.sql
More file actions
227 lines (221 loc) · 9.35 KB
/
complete_database_schema_dump.sql
File metadata and controls
227 lines (221 loc) · 9.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
-- =====================================================
-- COMPLETE DATABASE SCHEMA DUMP
-- =====================================================
-- Run this ONE query to get everything about your database
-- Copy the entire result and share it with me
WITH table_info AS (
SELECT
t.table_schema,
t.table_name,
c.column_name,
c.ordinal_position,
c.data_type,
c.character_maximum_length,
c.numeric_precision,
c.numeric_scale,
c.is_nullable,
c.column_default,
CASE WHEN pk.column_name IS NOT NULL THEN 'YES' ELSE 'NO' END as is_primary_key,
CASE WHEN fk.column_name IS NOT NULL THEN
fk.foreign_table_schema || '.' || fk.foreign_table_name || '(' || fk.foreign_column_name || ')'
ELSE NULL END as foreign_key_reference
FROM information_schema.tables t
JOIN information_schema.columns c
ON t.table_name = c.table_name
AND t.table_schema = c.table_schema
LEFT JOIN (
SELECT ku.table_schema, ku.table_name, ku.column_name
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage ku
ON tc.constraint_type = 'PRIMARY KEY'
AND tc.constraint_name = ku.constraint_name
AND tc.table_schema = ku.table_schema
) pk ON c.table_name = pk.table_name
AND c.table_schema = pk.table_schema
AND c.column_name = pk.column_name
LEFT JOIN (
SELECT
tc.table_schema,
kcu.table_name,
kcu.column_name,
ccu.table_schema AS foreign_table_schema,
ccu.table_name AS foreign_table_name,
ccu.column_name AS foreign_column_name
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
ON tc.constraint_name = kcu.constraint_name
AND tc.table_schema = kcu.table_schema
JOIN information_schema.constraint_column_usage ccu
ON ccu.constraint_name = tc.constraint_name
AND ccu.table_schema = tc.table_schema
WHERE tc.constraint_type = 'FOREIGN KEY'
) fk ON c.table_name = fk.table_name
AND c.table_schema = fk.table_schema
AND c.column_name = fk.column_name
WHERE t.table_schema = 'public'
AND t.table_type = 'BASE TABLE'
)
SELECT
table_name as "TABLE",
column_name as "COLUMN",
ordinal_position as "POS",
data_type as "TYPE",
COALESCE(
CASE
WHEN character_maximum_length IS NOT NULL
THEN data_type || '(' || character_maximum_length || ')'
WHEN numeric_precision IS NOT NULL
THEN data_type || '(' || numeric_precision ||
CASE WHEN numeric_scale > 0 THEN ',' || numeric_scale ELSE '' END || ')'
ELSE data_type
END,
data_type
) as "FULL_TYPE",
is_nullable as "NULLABLE",
column_default as "DEFAULT",
is_primary_key as "PK",
foreign_key_reference as "FK_REFERENCE"
FROM table_info
ORDER BY table_name, ordinal_position;
-- =====================================================
-- ALTERNATIVE: JSON FORMAT (Easier to copy/paste)
-- =====================================================
-- Run this if you want JSON output instead
SELECT jsonb_pretty(
jsonb_agg(
jsonb_build_object(
'table', table_name,
'columns', (
SELECT jsonb_agg(
jsonb_build_object(
'name', column_name,
'position', ordinal_position,
'type', data_type,
'full_type', COALESCE(
CASE
WHEN character_maximum_length IS NOT NULL
THEN data_type || '(' || character_maximum_length || ')'
WHEN numeric_precision IS NOT NULL
THEN data_type || '(' || numeric_precision ||
CASE WHEN numeric_scale > 0 THEN ',' || numeric_scale ELSE '' END || ')'
ELSE data_type
END,
data_type
),
'nullable', is_nullable = 'YES',
'default', column_default,
'is_primary_key', CASE WHEN pk.column_name IS NOT NULL THEN true ELSE false END,
'foreign_key', CASE WHEN fk.column_name IS NOT NULL THEN
jsonb_build_object(
'table', fk.foreign_table_name,
'column', fk.foreign_column_name
)
ELSE NULL END
) ORDER BY ordinal_position
)
FROM information_schema.columns c2
LEFT JOIN (
SELECT ku.table_schema, ku.table_name, ku.column_name
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage ku
ON tc.constraint_type = 'PRIMARY KEY'
AND tc.constraint_name = ku.constraint_name
AND tc.table_schema = ku.table_schema
) pk ON c2.table_name = pk.table_name
AND c2.table_schema = pk.table_schema
AND c2.column_name = pk.column_name
LEFT JOIN (
SELECT
kcu.table_schema,
kcu.table_name,
kcu.column_name,
ccu.table_name AS foreign_table_name,
ccu.column_name AS foreign_column_name
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
ON tc.constraint_name = kcu.constraint_name
AND tc.table_schema = kcu.table_schema
JOIN information_schema.constraint_column_usage ccu
ON ccu.constraint_name = tc.constraint_name
AND ccu.table_schema = tc.table_schema
WHERE tc.constraint_type = 'FOREIGN KEY'
) fk ON c2.table_name = fk.table_name
AND c2.table_schema = fk.table_schema
AND c2.column_name = fk.column_name
WHERE c2.table_schema = 'public'
AND c2.table_name = t.table_name
),
'indexes', (
SELECT jsonb_agg(
jsonb_build_object(
'name', indexname,
'definition', indexdef
)
)
FROM pg_indexes
WHERE schemaname = 'public'
AND tablename = t.table_name
),
'constraints', (
SELECT jsonb_agg(
jsonb_build_object(
'name', constraint_name,
'type', constraint_type
)
)
FROM information_schema.table_constraints
WHERE table_schema = 'public'
AND table_name = t.table_name
)
)
)
) as complete_schema
FROM information_schema.tables t
WHERE t.table_schema = 'public'
AND t.table_type = 'BASE TABLE'
ORDER BY t.table_name;
-- =====================================================
-- SIMPLE VERSION (Just the essentials)
-- =====================================================
-- If the above queries are too complex, use this simpler one:
SELECT
t.table_name,
c.column_name,
c.data_type,
CASE WHEN c.character_maximum_length IS NOT NULL
THEN c.data_type || '(' || c.character_maximum_length || ')'
ELSE c.data_type END as full_type,
c.is_nullable,
c.column_default,
CASE WHEN pk.column_name IS NOT NULL THEN 'PK' ELSE '' END as key,
CASE WHEN fk.column_name IS NOT NULL
THEN 'FK→' || fk.foreign_table_name || '.' || fk.foreign_column_name
ELSE '' END as references
FROM information_schema.tables t
JOIN information_schema.columns c
ON t.table_name = c.table_name
AND t.table_schema = c.table_schema
LEFT JOIN (
SELECT ku.table_schema, ku.table_name, ku.column_name
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage ku
ON tc.constraint_type = 'PRIMARY KEY'
AND tc.constraint_name = ku.constraint_name
) pk ON c.table_name = pk.table_name AND c.table_schema = pk.table_schema AND c.column_name = pk.column_name
LEFT JOIN (
SELECT
kcu.table_schema,
kcu.table_name,
kcu.column_name,
ccu.table_name AS foreign_table_name,
ccu.column_name AS foreign_column_name
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage ccu
ON ccu.constraint_name = tc.constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY'
) fk ON c.table_name = fk.table_name AND c.table_schema = fk.table_schema AND c.column_name = fk.column_name
WHERE t.table_schema = 'public'
AND t.table_type = 'BASE TABLE'
ORDER BY t.table_name, c.ordinal_position;