Sub topic 1
What COPY Does and Why It Beats Row-by-Row INSERTs
Prerequisites
This chapter assumes that you know basic SQL, including CREATE TABLE, INSERT, and SELECT. You should also know what a CSV file is and have used the psql command-line client. PostgreSQL data types such as integer, text, and date are also assumed.
The purpose of COPY
COPY is a PostgreSQL command for moving many rows between a table and a file or data stream. It can import data with COPY ... FROM and export data with COPY ... TO.
A row-by-row INSERT sends one SQL statement for each row:
INSERT INTO products (id, name, price)
VALUES (1, 'Keyboard', 49.99);
INSERT INTO products (id, name, price)
VALUES (2, 'Mouse', 19.99);This approach is easy to understand, but it creates repeated parsing, planning, network, and transaction work. A large import can require thousands or millions of statements.
COPY uses a bulk data path. PostgreSQL reads or writes a stream of rows using one command:
COPY products
FROM '/var/lib/postgresql/import/products.csv'
WITH (FORMAT csv, HEADER true);The FORMAT csv option tells PostgreSQL to interpret the file as comma-separated values. The HEADER true option tells it to skip the first line, which usually contains column names.
| Method | Typical use | Main cost |
|---|---|---|
| `INSERT` per row | A few rows or application transactions | Repeated statement overhead |
| `COPY` | Bulk imports and exports | Requires careful file-format handling |
COPY does not replace INSERT in every situation. Use INSERT when an application needs to add a small number of rows and apply business logic to each one. Use COPY when the data already exists in a structured stream and you need to transfer many rows efficiently.
A target table and the basic data flow
The following table gives the examples a destination:
CREATE TABLE products (
id integer PRIMARY KEY,
name text NOT NULL,
price numeric(10,2),
released date,
notes text
);An import reads fields from the input, converts them to the target column types, and inserts rows into products. An export does the reverse: PostgreSQL converts table values into a delimited text stream.
The table must already exist before COPY FROM inserts rows into it. PostgreSQL checks each field against the matching column type. For example, a value in the id field must be valid as an integer, and a value in the released field must be valid as a date.
COPY syntax
The general form is:
COPY table_name
FROM 'file_name'
WITH (FORMAT csv, HEADER true);For an export, use TO:
COPY products
TO '/var/lib/postgresql/export/products.csv'
WITH (FORMAT csv, HEADER true);These examples use a path on the database server. That detail matters because the PostgreSQL server, rather than your local terminal, opens the file. Server-side file access and the client-side psql \copy command are compared in the next chapter.
COPY can also export the result of a query, which makes it useful for producing filtered reports:
COPY (
SELECT id, name, price
FROM products
WHERE price > 10
ORDER BY id
) TO '/var/lib/postgresql/export/expensive.csv'
WITH (FORMAT csv, HEADER true);Choosing COPY
Choose COPY when most of these conditions apply:
- The input contains many rows.
- The file has a predictable structure.
- The target table and data types are known.
- You want one bulk operation instead of repeated statements.
- You can verify the result after the transfer.
The basic command is only the beginning. Correct imports depend on matching the file's delimiter, header, quoting, and missing-value rules to the COPY options.
Exercise
When is COPY usually a better choice than one INSERT statement per row?
Show answer
COPY is usually better when transferring many already-structured rows because it uses a bulk data path and avoids repeated statement and network overhead.