[Python] Unable to export dataframe in python to POSTGRESQL [duplicate]

Discussão em 'Python' iniciado por Stack, Março 26, 2025.

  1. Stack

    Stack Membro Participativo

    Hi I am trying to export my dataframe that was cleaned in python to postgresql database, I have multiple times tried to edit the to_sql data in python, which is really annoying

    def marcommdb_connection():
    load_dotenv()

    # Get credentials from environment variables
    username = os.getenv("PG_USERNAME")
    password = os.getenv("PG_PASSWORD")
    host = os.getenv("PG_HOST")
    port = os.getenv("PG_PORT")
    database = os.getenv("PG_DATABASE_DBEXPORT")

    if not all([username, password, host, port, database]):
    raise ValueError("Missing one or more PostgreSQL environment variables!")

    encoded_password = quote(password, safe="") if password else ""


    DATABASE_URL = f"postgresql+psycopg2://{username}:{encoded_password}@{host}:{port}/{database}"

    # Create and return SQLAlchemy engine
    return create_engine(DATABASE_URL)

    engine = marcommdb_connection()
    output_table = 'sf_enreg'

    final_enreg_tbl.to_sql('sf_enreg', engine, index=False, if_exists='replace')


    But somehow I keep on getting this result:

    AttributeError: 'Engine' object has no attribute 'cursor'

    Appreciate if anyone can help

    Thank you !

    Continue reading...

Compartilhe esta Página