Postgres COPY Command Generator
Build a correct PostgreSQL COPY statement without re-reading the manual. Choose export (COPY ... TO) or import (COPY ... FROM), point it at a table with an optional column list or at a SELECT query, pick CSV, TEXT or BINARY and set HEADER, DELIMITER, QUOTE, NULL, FORCE_NULL, ENCODING, WHERE (PG12+) and ON_ERROR (PG17+). You get three equivalent forms: the server-side COPY statement, the client-side psql \\copy meta-command, and a STDIN/STDOUT shell pipeline. Runs entirely in your browser.
Do more than postgres copy command generator — meet Chat2DB
Chat2DB is an AI-powered SQL client for Windows, macOS and Linux. Write SQL in natural language, format and optimize queries automatically, and manage MySQL, PostgreSQL, Oracle and 20+ other databases in one workspace.
How to use
- Pick the direction (export or import), the table or query, and the file path. Add a column list if the file only contains some columns.
- Choose the format and options: HEADER, DELIMITER, NULL string, FORCE_NULL / FORCE_NOT_NULL, WHERE filter, ON_ERROR ignore.
- Copy the server-side COPY SQL, the psql \copy command, or the shell pipeline and run it; read the notes for permission and version requirements.
Frequently asked questions
What is the difference between COPY and \copy in PostgreSQL?
COPY is a SQL command executed by the server, so the file path is on the database server and you need superuser or the pg_read_server_files / pg_write_server_files role. \copy is a psql meta-command that runs COPY ... FROM STDIN / TO STDOUT under the hood and streams the file from or to the machine where psql runs, so any user with table privileges can use it. On managed services such as RDS, Supabase or Neon you almost always want \copy or STDIN/STDOUT.
How do I import a CSV file into a Postgres table with a header row?
Create the table first with matching column types, then run \copy my_table FROM 'data.csv' WITH (FORMAT csv, HEADER true). If the CSV has fewer or differently ordered columns, add an explicit column list: \copy my_table (id, name, email) FROM 'data.csv' WITH (FORMAT csv, HEADER true). Empty strings become NULL only for unquoted fields; use FORCE_NULL (col) to also convert quoted empty strings, and NULL 'NA' if your file uses a custom null marker.
How do I export the result of a query to CSV in PostgreSQL?
COPY accepts a parenthesised query: \copy (SELECT id, total FROM orders WHERE total > 100) TO 'big_orders.csv' WITH (FORMAT csv, HEADER true). Queries work only with COPY TO, not COPY FROM. If you prefer not to use the command line, Chat2DB — a free AI-powered SQL client — can run the query and export results to CSV or Excel with a click; download it at https://chat2db.ai/download or use the web version at https://app.chat2db.ai.
