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

MethodSpeedPrivilegesFile Location
COPYFastestSuperuserServer
\copyFastNormal userClient
pgAdminMediumNormal userClient
INSERTSlowerInsert permN/A (SQL text)

Frequently Asked Questions

What is the fastest way to import CSV into PostgreSQL?
The COPY command is the fastest. For client-side files, use \copy in psql which streams the file through the client connection.
What is the difference between COPY and \copy?
COPY runs on the server and reads files from the server's filesystem (requires superuser). \copy reads files from the client machine and streams to the server — no superuser needed.
How do I handle NULL values in CSV import?
Use the NULL option: COPY ... WITH (FORMAT CSV, NULL 'NA'). By default, an unquoted empty string is treated as NULL in COPY with CSV format.
Can I import CSV using pgAdmin?
Yes. Right-click the target table, select Import/Export Data, choose Import, select your CSV file, configure options, and run the import.
What encoding should my CSV file use?
UTF-8 is recommended and is the PostgreSQL default. For other encodings, specify with the ENCODING option.