File size: 1,072 Bytes
feeaf83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

from sqlalchemy import create_engine, text
import os
from dotenv import load_dotenv

load_dotenv()

DATABASE_URL = os.getenv("DATABASE_URL")
if not DATABASE_URL:
    print("DATABASE_URL not found in .env")
    exit(1)

print(f"Connecting to: {DATABASE_URL.split('@')[1]}") # Print host only for safety

try:
    engine = create_engine(DATABASE_URL)
    with engine.connect() as conn:
        # Check connection
        res = conn.execute(text("SELECT version();"))
        print(f"Connected! Postgres version: {res.fetchone()[0]}")
        
        # Check tables
        res = conn.execute(text("SELECT count(*) FROM verified_news;"))
        count = res.fetchone()[0]
        print(f"VerifiedNews count: {count}")
        
        # Check if we can fetch a few articles
        res = conn.execute(text("SELECT title FROM verified_news LIMIT 5;"))
        articles = res.fetchall()
        print("Latest 5 articles:")
        for a in articles:
            print(f"- {a[0]}")
            
except Exception as e:
    print(f"Database connection failed: {e}")
    exit(1)