How to insert million rows to database table

Disabling Index

1
ALTER TABLE example DISABLE TRIGGER ALL;

Inserting data

You could insert data via insert- or coppy clauses.

Enabling indexes after the data is stored

1
ALTER TABLE example ENABLE   TRIGGER ALL;

How to rebuild indexes

1
2

REINDEX TABLE example;

If you don’t want to lock the table while rebuilding the index

1
REINDEX TABLE CONCURRENTLY example;

This CONCURRENTLY allows application to write rows to table. This is suitable when the database reindexing takes longer periods. Beware There’s a duplicate index during the indexing process which consumes space on system.