I'm trying to execute the below postgres query through python psycopg2
SELET * FROM users WHERE user_id IN (1,2,3,4,5);
Below is the python code for the same:
def get_usernames(userids):
connection = psycopg2.connect("dbname=userdb user=guest host=example.com")
connection.autocommit = True
cursor = connection.cursor()
cursor.execute("SELECT username FROM users WHERE userid IN {}".format(userids))
return cursor
userids=[1,2,3,4,5]
get_usernames(userids)
I'm getting the below error:
psycopg2.ProgrammingError: syntax error at or near "["
LINE 1: ...from users WHERE userid IN {1,2...
I'm used to pymysql. This is new to me. Let me know what is my mistake.