Create Idempotent inserts in Postgres

Details in the comments of the code