Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need to programmatically insert tens of millions of records into a Postgres database. Presently I am executing thousands of insert statements in a single query.

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

like image 932
Ash Avatar asked Apr 17 '09 03:04

Ash


People also ask

How do I run multiple inserts in PostgreSQL?

PostgreSQL INSERT Multiple Rows First, specify the name of the table that you want to insert data after the INSERT INTO keywords. Second, list the required columns or all columns of the table in parentheses that follow the table name. Third, supply a comma-separated list of rows after the VALUES keyword.

What is the best way to transfer the data in a PostgreSQL?

If you really have two distinct PostgreSQL databases, the common way of transferring data from one to another would be to export your tables (with pg_dump -t ) to a file, and import them into the other database (with psql ).

Which of the following commands is the most efficient way to bulk load data into a Postgres table from a CSV file?

Goto solution for bulk loading into PostgreSQL is the native copy command.

How many inserts per second can Postgres handle?

At 200 million rows the insert rate in PostgreSQL is an average of 30K rows per second and only gets worse; at 1 billion rows, it's averaging 5K rows per second. On the other hand, TimescaleDB sustains an average insert rate of 111K rows per second through 1 billion rows of data–a 20x improvement.


2 Answers

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).

like image 104
Dan Lew Avatar answered Oct 02 '22 20:10

Dan Lew


There is an alternative to using COPY, which is the multirow values syntax that Postgres supports. From the documentation:

INSERT INTO films (code, title, did, date_prod, kind) VALUES     ('B6717', 'Tampopo', 110, '1985-02-10', 'Comedy'),     ('HG120', 'The Dinner Game', 140, DEFAULT, 'Comedy'); 

The above code inserts two rows, but you can extend it arbitrarily, until you hit the maximum number of prepared statement tokens (it might be $999, but I'm not 100% sure about that). Sometimes one cannot use COPY, and this is a worthy replacement for those situations.

like image 33
Ben Harper Avatar answered Oct 02 '22 19:10

Ben Harper