[Solved]-Returning a value with psycopg2


Sure it will, it’ll know the ID as soon as the command finishes, that’s how RETURNING is implemented. You need to actually fetch it though, so something like:

cursor.execute("INSERT INTO .... RETURNING id")
id_of_new_row = cursor.fetchone()[0]

should work in your scenario.


RETURNING works on Postgresql >= 8.2


Leave a comment