-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
33 lines (27 loc) · 1.25 KB
/
data.py
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
import csv
import sqlalchemy
# Connect to the database
engine = sqlalchemy.create_engine('postgresql://username:password@localhost:5432/db_name')
# Get a list of all table names
table_names = engine.table_names()
# Open a file for writing
with open('data_dictionary.csv', 'w', newline='') as csvfile:
# Create a CSV writer
fieldnames = ['Table Name', 'Column Name', 'Data Type', 'Primary Key', 'Nullable', 'Default Value']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
# Write the header row
writer.writeheader()
# Get the information for each table
for table_name in table_names:
# Get the table's metadata
metadata = sqlalchemy.MetaData(bind=engine, reflect=True)
table = sqlalchemy.Table(table_name, metadata, autoload=True)
# Write the information for each column in the table
for column in table.columns:
writer.writerow({'Table Name': table_name,
'Column Name': column.name,
'Data Type': column.type,
'Primary Key': column.primary_key,
'Nullable': column.nullable,
'Default Value': column.server_default
})