What's the fastest way to do a bulk insert into Postgres?

Ash picture Ash · Apr 17, 2009 · Viewed 306.4k times · Source

I need to programmatically insert 10's of millions of records into a postgres database. Presently I am executing 1000's of insert statements in a single "query".

Is there a better way to do this, some bulk insert statement I dont know about?

Answer

Dan Lew picture Dan Lew · Apr 17, 2009

PostgreSQL has a guide on how to best populate a database initially, and they suggest using the COPY command for bulk loading rows. The guide has some other good tips on how to speed up the process, like removing indexes and foreign keys before loading the data (and adding them back afterwards).