Files
proxmox-lldap/inspect_db.py
2026-01-02 21:19:56 -08:00

72 lines
2.3 KiB
Python

import psycopg2
import os
import sys
# Hardcoding the path since we know it
SECRETS_FILE = "/mnt/secrets/postgres.env"
def load_env_file(filepath):
config = {}
if not os.path.exists(filepath):
print(f"❌ Error: Secrets file not found at {filepath}")
sys.exit(1)
with open(filepath, 'r') as f:
for line in f:
if '=' in line and not line.strip().startswith('#'):
key, value = line.strip().split('=', 1)
# Strip quotes
if (value.startswith('"') and value.endswith('"')) or \
(value.startswith("'") and value.endswith("'")):
value = value[1:-1]
config[key.strip()] = value
return config
if __name__ == "__main__":
print("--- 🔍 Database Inspection Tool ---")
env = load_env_file(SECRETS_FILE)
print(f"Connecting to: {env.get('DB_HOST')}:{env.get('DB_PORT')} (DB: {env.get('DB_NAME')})")
try:
conn = psycopg2.connect(
dbname=env.get("DB_NAME"),
user=env.get("DB_USER"),
password=env.get("DB_PASS"),
host=env.get("DB_HOST"),
port=env.get("DB_PORT", "5432")
)
cur = conn.cursor()
# 1. Get Column Names for 'users' table
print("\n[ Table Schema: 'users' ]")
cur.execute("""
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'users';
""")
columns = [row[0] for row in cur.fetchall()]
print(f"Columns found: {columns}")
if not columns:
print("⚠️ WARNING: No columns found! Does the table 'users' exist?")
# 2. Dump all users
print("\n[ Table Content: 'users' ]")
# We try to select 'id' specifically if it exists, otherwise *
try:
cur.execute("SELECT * FROM users;")
rows = cur.fetchall()
if not rows:
print("⚠️ Table is EMPTY.")
else:
for row in rows:
# Print raw row data
print(f"Row: {row}")
except Exception as e:
print(f"Could not select data: {e}")
conn.close()
except Exception as e:
print(f"\n❌ CONNECTION FAILED: {e}")