Spaces:
Runtime error
Runtime error
File size: 2,251 Bytes
7647b38 | 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 | """
Database Inspection Script
Connect to PostgreSQL and examine existing structure
"""
import asyncio
import asyncpg
async def inspect_database():
"""Connect to PostgreSQL and inspect tables"""
# Connection string
conn_string = "postgres://avnadmin:AVNS_-lwx_03K2q6KDrMmSoX@stagingmirchi1-stagingmirchi.f.aivencloud.com:14387/defaultdb?sslmode=require"
try:
conn = await asyncpg.connect(conn_string)
print("β
Connected to PostgreSQL database\n")
# Get all tables
tables = await conn.fetch("""
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name
""")
print(f"π Found {len(tables)} tables:\n")
for table in tables:
table_name = table['table_name']
print(f"\n{'='*60}")
print(f"TABLE: {table_name}")
print('='*60)
# Get columns
columns = await conn.fetch("""
SELECT column_name, data_type, character_maximum_length, is_nullable, column_default
FROM information_schema.columns
WHERE table_schema = 'public' AND table_name = $1
ORDER BY ordinal_position
""", table_name)
for col in columns:
dtype = col['data_type']
if col['character_maximum_length']:
dtype += f"({col['character_maximum_length']})"
nullable = "NULL" if col['is_nullable'] == 'YES' else "NOT NULL"
default = f"DEFAULT {col['column_default']}" if col['column_default'] else ""
print(f" {col['column_name']}: {dtype} {nullable} {default}")
# Get row count
try:
count = await conn.fetchval(f'SELECT COUNT(*) FROM "{table_name}"')
print(f"\n π Row count: {count}")
except:
pass
await conn.close()
print("\n\nβ
Inspection complete!")
except Exception as e:
print(f"β Error: {e}")
if __name__ == "__main__":
asyncio.run(inspect_database())
|