How to Import CSV into PostgreSQL
PostgreSQL provides several methods for importing CSV data: the server-side COPY command, the client-side \copy meta-command in psql, the pgAdmin Import/Export wizard, and plain INSERT statements. This guide covers each approach with working examples.
💡 Quick path: Need INSERT statements from CSV? Use the CSV to SQL INSERT Generator — paste your CSV and download a PostgreSQL-ready SQL script with correct quoting, TEXT types, and BOOLEAN literals.
Method 1: COPY (Server-Side)
The COPY command reads files from the PostgreSQL server's filesystem. It requires superuser privileges or the pg_read_server_files role.
COPY customers (id, name, email, amount)
FROM '/var/data/customers.csv'
WITH (
FORMAT CSV,
HEADER true,
DELIMITER ',',
NULL 'NA',
ENCODING 'UTF8'
);
Method 2: \copy (Client-Side, psql)
The \copy meta-command in psql reads the file from your local machine and streams it to the server. No superuser privileges required — ideal for remote databases and cloud-hosted PostgreSQL.
\copy customers (id, name, email, amount)
FROM '/home/user/customers.csv'
WITH (FORMAT CSV, HEADER true, DELIMITER ',');
Note that \copy must be on a single line — it does not support multi-line syntax.
Method 3: pgAdmin Import/Export
In pgAdmin 4, right-click the target table, select Import/Export Data, choose Import, select your CSV file, configure the delimiter and header settings, and click OK. This is the easiest method for one-off imports without writing any SQL.
Method 4: INSERT Statements
INSERT statements are the most portable method — they work in any PostgreSQL client and can be version-controlled. They are slower than COPY for large datasets but give you full control over data transformation and NULL handling.
INSERT INTO "customers" ("id", "name", "email", "amount") VALUES
(1, 'Alice Johnson', '[email protected]', 250.00),
(2, 'Bob Smith', '[email protected]', NULL),
(3, 'Carol Williams', '[email protected]', 175.50);
The CSV to SQL INSERT Generator automates this process with correct PostgreSQL escaping, TEXT types, BOOLEAN literals (TRUE/FALSE), and configurable batch sizes.
Choosing the Right Method
| Method | Speed | Privileges | File Location |
|---|---|---|---|
| COPY | Fastest | Superuser | Server |
| \copy | Fast | Normal user | Client |
| pgAdmin | Medium | Normal user | Client |
| INSERT | Slower | Insert perm | N/A (SQL text) |
