File size: 1,315 Bytes
e43f92e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import psycopg2

def query_postgresql(
    query: str, 
    database: str, 
    user: str, 
    password: str, 
    host: str,
    port: str,
    named_columns: bool=True
    ):
    
    conn = psycopg2.connect(
            database=database,
            user=user,
            password=password,
            host=host,
            port=port
        )
    
    cur = conn.cursor()
    cur.execute(query)
    rows = cur.fetchall()

    if named_columns:
        column_names = [desc[0] for desc in cur.description]
        return [ dict(zip(column_names, r)) for r in rows ]

    return rows

def query_postgresql_realvest(query: str, named_columns: bool=True):
    import streamlit as st
    POSTGRESQL_REALVEST_USER = st.secrets["POSTGRESQL_REALVEST_USER"]
    POSTGRESQL_REALVEST_PSWD = st.secrets["POSTGRESQL_REALVEST_PSWD"]

    return query_postgresql(
        query, 
        database="realvest", 
        user=POSTGRESQL_REALVEST_USER, 
        password=POSTGRESQL_REALVEST_PSWD, 
        host="realvest.cdb5lmqrlgu5.us-east-2.rds.amazonaws.com",
        port="5432",
        named_columns=named_columns
        )

if __name__ == "__main__":
    query = """
    SELECT *
    FROM main_products
    WHERE productid in ('2093075');
    """
    
    results = query_postgresql_realvest(query)
    print(results)