Files
AutoMarket/backend/db.py
2026-05-16 02:14:57 +03:00

26 lines
762 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# backend/db.py
import os
from psycopg2 import pool
from dotenv import load_dotenv
load_dotenv()
DATABASE_URL = os.getenv('DATABASE_URL')
# Initialise a connection pool (min 1, max 10 connections)
postgres_pool = pool.ThreadedConnectionPool(1, 10, dsn=DATABASE_URL)
def get_db_connection():
"""Return a connection from the pool. Caller must close it after use."""
conn = postgres_pool.getconn()
# Устанавливаем схему automarket (таблицы созданы там, а не в public)
cur = conn.cursor()
cur.execute("SET search_path TO automarket, public")
cur.close()
conn.commit()
return conn
def release_db_connection(conn):
"""Return the connection to the pool."""
postgres_pool.putconn(conn)