3

I am doing an INSERT into a postgres DB, which has an auto increment field called id.

How do I get the value of id after doing the insert in Python? There are good references on this site to this for MySQL databases (i.e. using cursor.lastrowid, or connection.insert_id()) but these don't seem to work with my postgres DB.

The id field is using a sequence to auto increment. So the id field is not null, default, nextval('table_id_seq')

Thanks

Simon
  • 97
  • 3
  • 13

1 Answers1

3

Pls try that:

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