I suggest upgrading to at least PostgreSQL 9.5 if you haven't already and look into the INSERT...ON CONFLICT feature (also called UPSERT). With it you can define what happens if an insert would violate a defined constraint and instead update the given row, do nothing or a wide variety of other operations.
If this doesn't fit the model that is causing your duplicate key errors, you may want to look into using a function to do your writes instead and catch the given error in an exception block and then just ignore it.