Why aren't you using the PostgreSQL built-in COPY funktion? I would expect it to be an order of magnitude faster than what you are doing.