Obtener ultimo ID insertado en la db¶
INSERT INTO persons (lastname,firstname) VALUES ('Smith', 'John') RETURNING id
Ejeplo con Python
with psycopg2.connect(conn_string) as conn:
with conn.cursor() as cur:
cur.execute(
"INSERT INTO persons(lastname, firstname) \
VALUES(%s, %s) RETURNING id", (firstname, lastname))
conn.commit()
print(cur.fetchone()[0])