import os
from urllib.parse import urlparse
def main():
    url=os.getenv("PAYMENT_DATABASE_URL_SYNC","")
    if not url: return
    import psycopg
    p=urlparse(url.replace("+psycopg","")); db=(p.path or "/payment_db").lstrip("/")
    with psycopg.connect(host=p.hostname or "localhost",port=p.port or 5432,user=p.username,password=p.password,dbname="postgres",autocommit=True) as conn:
        with conn.cursor() as cur:
            cur.execute("SELECT 1 FROM pg_database WHERE datname=%s",(db,))
            if not cur.fetchone(): cur.execute(f'CREATE DATABASE "{db}"')
if __name__=="__main__": main()
