· 8 months ago · Mar 10, 2025, 11:10 AM
1
2import psycopg2
3import sys
4
5DB_PARAMS = {
6 'dbname': 'postgres',
7 'user': 'climate_solutions',
8 'password': 'jb#AEzBGZCd@umxF',
9 'host': 'coredata-live-cluster.cluster-cgkjj9hgdmot.eu-west-1.rds.amazonaws.com',
10 'port': 5432
11}
12
13def get_table_info():
14 try:
15 # Connect to the database
16 conn = psycopg2.connect(**DB_PARAMS)
17 cursor = conn.cursor()
18
19 # Get list of tables
20 cursor.execute("""
21 SELECT tablename FROM pg_tables WHERE schemaname = 'public';
22 """)
23 tables = cursor.fetchall()
24
25 for table in tables:
26 table_name = table[0]
27
28 # Get row count safely
29 cursor.execute(f"SELECT COUNT(*) FROM \"{table_name}\";")
30 row_count = cursor.fetchone()[0]
31
32 # Get table size safely
33 cursor.execute(f"SELECT pg_size_pretty(pg_total_relation_size('\"{table_name}\"'));")
34 table_size = cursor.fetchone()[0]
35
36 print(f"Table: {table_name}, Rows: {row_count}, Size: {table_size}")
37
38 cursor.close()
39 conn.close()
40 except psycopg2.Error as e:
41 print(f"Database Error: {e}")
42 sys.exit(1)
43 except Exception as e:
44 print(f"Unexpected Error: {e}")
45 sys.exit(1)
46
47def test_aws():
48 print("Running in AWS")
49 get_table_info()
50