title: Getting the id of the last record inserted for Postgresql SERIAL KEY with Python
body: <p>I am using SQLAlchemy without the ORM, i.e. using hand-crafted SQL statements to directly interact with the backend database. I am using PG as my backend database (psycopg2 as DB driver) in this instance - I don't know if that affects the answer.</p>
  <p>I have statements like this,for brevity, assume that conn is a valid connection to the database:</p>
  <pre class="lang-py prettyprint-override"><code>conn.execute(&quot;INSERT INTO user (name, country_id) VALUES ('Homer', 123)&quot;)
  </code></pre>
  <p>Assume also that the user table consists of the columns <code>(id [SERIAL PRIMARY KEY], name, country_id)</code></p>
  <p>How may I obtain the id of the new user, ideally, without hitting the database again?</p>