Search code examples
pythonpostgresqlpsycopg2ui-automation

python psycopg2 passes db connection and nothing happens


I need your help. I have a problem connecting to PostgreSQL via Pycharm. First I connect to vpn:

    def run_openvpn(config_path):
        try:
            subprocess.run(['openvpn', '--config', config_path])
            print('OpenVPN connection established.')
        except FileNotFoundError:
            print("OpenVPN executable not found. Please ensure it is installed and in your system's PATH.")

        except subprocess.CalledProcessError as e:
            return f"OpenVPN connection failed with error: {e}"

It is followed by a function that sends a request:

    def execute_sql_request(host, port, database, user, password, sql_query):
        try:
            # Connect to the PostgreSQL database
            connection = psycopg2.connect(
                host=host,
                port=port,
                database=database,
                user=user,
                password=password
            )
        
            # Create a cursor to execute SQL queries
            cursor = connection.cursor()

            formatted_query = cursor.mogrify(sql_query)
            print("Formatted SQL Query:", formatted_query.decode('utf-8'))

            # Execute the SQL query
            cursor.execute(sql_query)

            # Fetch all the results
            results = cursor.fetchall()

            # Close the cursor and the database connection
            connection.commit()
            cursor.close()
            connection.close()

            return results  # Return the result of the SQL request as a list of tuples

        except psycopg2.Error as e:
            error_msg = str(e)
            return error_msg  # Return the error message if there was a problem

I run the code and get messages about successful vpn initialization, but nothing else happens. I don't get any error messages, I don't get values from the database. Please help me understand why the execute_sql_request function is not executing

I tried to add a display of some information to the execute_sql_request function in order to understand whether the function is being executed or it is falling. But I didn’t receive anything, so I can conclude that there is not even a function call

this code runs my functions

def test_execute_sql_query(browser)
    sql.run_openvpn(config_path=**)
    sql.execute_sql_request(host=**, port=**, database=**, user=**, password=**, sql_query=**)
    sql.stop_openvpn()

'sql' is a page that stores functions that work with SQL ' stop_openvpn' function to close connection with vpn. She works right


Solution

  • If someone faced the same problem. I found a solution to the problem: The problem turned out to be very simple, it was that the function that opened the connection to openvpn was blocking the call of other functions. Therefore, I combined the run_openvpn and execute_sql_request functions into one function. Now python, using the threading module, simultaneously establishes a vpn connection and sends an sql query to the database