7.7. Inserting Huge Amounts of Data

Until now you have only dealt with small amounts of data. Inserting only a few records can easily be done by using INSERT statements. With huge amounts of data, using INSERT would be far too slow due to a lot of parsing and transaction overhead.

To insert data more efficiently, PostgreSQL provides a command called COPY, which can also be used to store tables in external files. Let's look at the syntax overview of the COPY command:

phpbook=# \h COPY Command: COPY Description: copy data between files and tables Syntax: COPY [ BINARY ] table [ WITH OIDS ] FROM { 'filename' | stdin } [ [USING] DELIMITERS 'delimiter' ] [ WITH NULL AS 'null string' ] COPY [ BINARY ] table [ WITH OIDS ] TO { 'filename' | stdout } ...

Get PHP and PostgreSQL: Advanced Web Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.