simple Python app that conencts to db

This commit is contained in:
luptmoor
2026-02-25 18:19:22 +01:00
parent 8473d3d12c
commit 0e242801e7
4 changed files with 68 additions and 0 deletions

35
app/main.py Normal file
View File

@@ -0,0 +1,35 @@
import os
import psycopg2
import time
DB_USER = os.getenv("POSTGRES_USER");
DB_PASSWWORD = os.getenv("POSTGRES_PASSWORD");
DB_NAME = os.getenv("POSTGRES_DB");
DB_HOST = os.getenv("POSTGRES_HOST");
def connect_db():
while True:
try:
conn = psycopg2.connect(
host=DB_HOST,
database=DB_NAME,
user=DB_USER,
password=DB_PASSWWORD
)
print("Connected to the database successfully!")
return conn
except psycopg2.OperationalError as e:
print(f"Database connection failed: {e}")
time.sleep(3)
if __name__ == "__main__":
print("starting app.")
conn = connect_db();
cursor = conn.cursor()
cursor.execute("SELECT postgis_full_version();")
print("obst", cursor.fetchone());