skip to Main Content

I am trying to send the outputs I got in python to the database. Like I am reading valves from my plc using the OPC UA server via python. I can read my variables every second, Now I want to send those values to my PostgreSQL database. Any help would be appreciated. Thank you very much again.

I created a connection between python and PostgreSQL by using the psycopg module. And I am able to create the tables using my python script.

2

Answers


  1. Chosen as BEST ANSWER

    I created a connection between python and postgreSQL database by using

    psycopg2.connect(dbname="dbname", port= 6543, user="postgres", password="password", host="host.docker.internal") 
    

    I give host details for creating docker image and use it.


  2. Rather than having to script directly with psycopg, I usually reccomend using SqlAlchemy, which is a nice wrapper for psycopg that makes editing data in the database much easier

    Eg, by using the same psycopg engine you have already set up, you can do:

    from sqlalchemy.orm import Session
    
    with Session(engine) as session:
    
         spongebob = User(
             name="spongebob",
             fullname="Spongebob Squarepants",
             addresses=[Address(email_address="[email protected]")],
         )
         sandy = User(
             name="sandy",
             fullname="Sandy Cheeks",
             addresses=[
                 Address(email_address="[email protected]"),
                 Address(email_address="[email protected]"),
             ],
         )
         patrick = User(name="patrick", fullname="Patrick Star")
    
         session.add_all([spongebob, sandy, patrick])
    
         session.commit()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search