The \COPY command is made for loading data into PostgreSQL straight from your local computer. Unlike COPY, which works with files on the PostgreSQL server, \COPY works with files available on the client side, like your laptop or desktop.
You use \COPY when you’re working with files on your own computer, like your laptop. Unlike COPY, which reads files from the server, \COPY lets you load data that’s right at your fingertips.
This is super handy when you’re developing or testing a project on your own machine, or when you don’t have access to the PostgreSQL server’s file system. This approach is especially popular among devs and analysts who need to quickly load a CSV file to check something without any extra hassle.
Difference Between COPY and \COPY
At first glance, COPY and \COPY look similar, but they work differently:
COPYruns on the server side and needs the file to be in a directory PostgreSQL can access. PostgreSQL also needs permission to read that file.\COPYruns on the client side, so the file has to be available to the client app, likepsql. With\COPY, you can load data from files on your own computer, even if you don’t have access to the server.
Basics of Using \COPY
Command syntax:
\COPY table FROM 'file_path' [WITH] (options)
Where:
table: the name of the table you’re loading data into.file_path: the path to the file on your local computer.options: optional parameters that let you customize how the function works.
Example:
\COPY students FROM 'C:/data/students.csv' DELIMITER ',' CSV HEADER;
students: the name of the table you’re loading data into.'C:/data/students.csv': the path to the file on your local computer.DELIMITER ',': sets the data separator in the file (comma in this case).CSV HEADER: tells PostgreSQL that the first row of the file contains column headers.
Example:
Let’s say you have a file called students.csv with student data, sitting in your local folder, like C:/data/students.csv. Here’s what’s inside:
id,name,age,major
1,John Doe,20,Computer Science
2,Jane Smith,22,Mathematics
3,Emily White,21,Physics
We want to load this data into the students table. I already created the table in PostgreSQL with this structure:
CREATE TABLE students (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER NOT NULL,
major TEXT
);
Now let’s use the \COPY command to load the data:
\COPY students FROM 'C:/data/students.csv' DELIMITER ',' CSV HEADER;
After running this command, the students table will have three records from the students.csv file. You can check it like this:
SELECT * FROM students;
Expected result:
| id | name | age | major |
|---|---|---|---|
| 1 | John Doe | 20 | Computer Science |
| 2 | Jane Smith | 22 | Mathematics |
| 3 | Emily White | 21 | Physics |
\COPY Command Options
When you use \COPY, especially with CSV format, it’s important to know the key options that help you control the data format and make processing easier. Here are the most popular ones.
FORMAT Option
Sets the data format. The most common values are: text, csv.
Example:
\COPY users FROM 'users.csv' WITH (FORMAT csv)
Why you need it: Without this option, PostgreSQL will assume you’re using text format with tabs. csv is more universal and readable.
HEADER Option
Tells PostgreSQL that the first row of the CSV file contains column headers, not actual data.
Example:
\COPY users FROM 'users.csv' WITH (FORMAT csv, HEADER)
Why you need it: Lets you skip the header row during import — super useful when exporting from Excel or other systems.
DELIMITER Option
Sets the field separator character. By default, it’s a comma for CSV, tab for TEXT.
Example:
\COPY products FROM 'products.csv' WITH (FORMAT csv, DELIMITER ';')
Why you need it: Lets you adapt import/export for non-standard CSV formats, like using a semicolon (which is common in Europe).
ENCODING Option
Specifies the file encoding.
Example:
\COPY clients FROM 'clients.csv' WITH (FORMAT csv, HEADER, ENCODING 'WIN1251')
Why you need it: Lets you properly load files in Windows or other encodings without having to convert them manually.
NULL Option
Defines which string value in the file should be treated as NULL.
Example:
\COPY orders FROM 'orders.csv' WITH (FORMAT csv, NULL 'NULL')
Why you need it: When reading files where missing values are shown as 'NULL', this helps PostgreSQL interpret empty fields correctly.
Limitations and Features of \COPY
1. PostgreSQL Client Requirements
psql is the PostgreSQL client that supports the \COPY command. Make sure you’re using it to work with your database. Keep in mind that other clients, like pgAdmin, might not support \COPY.
2. File Encoding
Pay attention to the encoding of the file you’re loading. PostgreSQL expects the file to be in UTF-8. If your file uses a different encoding (like Windows-1251), you might get errors. You can convert the file encoding using tools like iconv or text editors (like VS Code).
3. File Path Localization
On Windows, the file path might have backslashes (\). In that case, replace them with forward slashes (/). For example, instead of C:\data\students.csv use C:/data/students.csv.
Common Errors and How to Fix Them
1. File access error.
If you see something like:
could not open file "C:/data/students.csv" for reading: No such file or directory
Make sure that:
- The file exists at the specified path.
- You specified the correct absolute path.
- You have permission to access the file.
2. Data structure mismatch error. If the structure of the file you’re loading is different from the table structure, you’ll get an error. For example, if the file has extra columns or wrong data types, you’ll see an error message. Check the column headers and types before loading.
3. Encoding problems. If the file isn’t in UTF-8, you’ll see “weird” characters instead of text. The fix is to convert the file to UTF-8 before loading.
Advantages of Using \COPY
1. Super easy to use. You can load data from a file with just one command. No need to move files to the server.
2. Universal. psql is available on all major operating systems, so you can use \COPY pretty much anywhere.
3. No server access headaches. Since the command works on the client side, you don’t have to worry about server file permissions.
Limitations and Tips
Even though it’s super convenient, the \COPY command has some limitations:
- It’s not great for really big files, since it has to transfer data over the network between your client and the server. For those cases, it’s better to use
COPYon the server. - Make sure your file is fully prepped for loading: fix errors, remove extra lines, and make sure the file structure matches the table.
For big projects, it’s a good idea to test loading with a small data set first, then process the full file.
GO TO FULL VERSION