The use of string concatenation while building dynamic SQL queries can expose your application to SQL Injection Attacks, even if you're using psycopg2.
Although, psycopg2 provides some safety, but concatenating user input into SQL strings in risky.
Consider the following example:
query = "SELECT * FROM users WHERE username = '" + user_input + "';"
Here, the attacker could manipulate user_input to execute arbitrary SQL commands.
So, instead of concatenating strings, use parameterized queries, which are safer:
query = "SELECT * FROM users WHERE username = %s;"
cursor.execute(query, (user_input,))
This will ensure that user input is treated as data, not as part of the SQL command.