handeling database from inside voice agent container

parent 78f5ff19
......@@ -73,6 +73,11 @@ services:
N8N_WEBHOOK_URL: "${N8N_WEBHOOK_URL}"
OPENAI_API_KEY: "${OPENAI_API_KEY}"
MINIO_BUCKET: "${MINIO_BUCKET}"
POSTGRES_HOST: "postgres"
POSTGRES_USER: "${POSTGRES_USER}"
POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}"
POSTGRES_DB: "${POSTGRES_DB}"
volumes:
- ./uploads:/app/uploads
depends_on:
......
......@@ -6,3 +6,4 @@ fastapi
uvicorn[standard]
python-multipart
openai
psycopg2-binary
\ No newline at end of file
import os
import psycopg2
from psycopg2.extras import RealDictCursor
# Read credentials from environment variables
POSTGRES_HOST = os.getenv("POSTGRES_HOST", "postgres")
POSTGRES_USER = os.getenv("POSTGRES_USER")
POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD")
POSTGRES_DB = os.getenv("POSTGRES_DB")
def get_db_connection():
conn = psycopg2.connect(
host=POSTGRES_HOST,
user=POSTGRES_USER,
password=POSTGRES_PASSWORD,
dbname=POSTGRES_DB
)
return conn
# Example usage
if __name__ == "__main__":
conn = get_db_connection()
cur = conn.cursor(cursor_factory=RealDictCursor)
# Get all table names in the public schema
cur.execute("""
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_type = 'BASE TABLE';
""")
tables = cur.fetchall()
for table in tables:
table_name = table['table_name']
print(f"Contents of table: {table_name}")
# Fetch all rows in the table
cur.execute(f"SELECT * FROM {table_name};")
rows = cur.fetchall()
for row in rows:
print(row)
print("\n")
cur.close()
conn.close()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment