Take a look at this post, especially the accepted answer of using the INSERT...RETURNING syntax:
How to get a value from the last inserted row?
This the best way to get this value in PostgreSQL as you only make one network round trip and it's done as a single atomic operation on the server.