When your data is already on the server, you can use the COPY command to load it into PostgreSQL tables. This is especially handy if you’re working with big data sets or your PostgreSQL server is running on a separate machine.
The server-side COPY command is super convenient in real-world projects. First off, it works way faster since the files are already on the server and you don’t need to transfer them over the network. It’s also safer: you don’t have to copy data from your local computer, which lowers the risk of leaks. Plus, you can easily plug the loading process into automated server scripts or background services—like when you’re regularly updating analytics tables.
Syntax of the COPY Command
The COPY command is simple, but there are a few key things to keep in mind:
COPY table_name
FROM '/path/to/file.csv'
WITH (FORMAT CSV, HEADER TRUE);
table_name— the name of the table you want to load data into./path/to/file.csv— the full path to the file on the server.- The
WITHoptions let you specify the file format, whether there’s a header, delimiters, and a bunch of other stuff.
Example of Using the COPY Command
Let’s walk through a quick example. Say you need to load a CSV file with student data into a PostgreSQL database. The file is on the server at /var/lib/postgresql/data/students.csv.
Step 1. Table for the Data
First, make sure there’s a table in the database where you can load the data:
CREATE TABLE students (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
registration_date DATE
);
This table will store info about students: their name, email, and registration date.
Step 2. The CSV File
Here’s what the /var/lib/postgresql/data/students.csv file might look like:
id,name,email,registration_date
1,Alex Lin,alex.lin@example.com,2023-09-01
2,Maria Chi,maria.chi@example.com,2023-09-02
3,Peter Ming,peter.ming@example.com,2023-09-02
Step 3. The COPY Command
Now we can use COPY to load the data from the file into the table:
COPY students
FROM '/var/lib/postgresql/data/students.csv'
WITH (FORMAT CSV, HEADER TRUE);
Here:
FORMAT CSVtells PostgreSQL the file is in CSV format.HEADER TRUEmeans the first row of the file contains column headers.
After running the command, the data from the file will fill up the students table.
Checking the Result
After loading the data, make sure everything went smoothly:
SELECT * FROM students;
You’ll see the rows from the file loaded into your table:
| id | name | registration_date | |
|---|---|---|---|
| 1 | Alex Lin | alex.lin@example.com | 2023-09-01 |
| 2 | Maria Chi | maria.chi@example.com | 2023-09-02 |
| 3 | Peter Ming | peter.ming@example.com | 2023-09-02 |
Setting Up Access Rights
PostgreSQL needs to have access to the file to use the COPY command. If the permissions aren’t set up right, you’ll run into errors. For example:
ERROR: could not open file "/var/lib/postgresql/data/students.csv" for reading: Permission denied
To avoid these problems, make sure of the following:
- The file is readable by the PostgreSQL user. Usually, that’s the
postgresuser. - Check the permissions on the file and the directory it’s in:
ls -l /var/lib/postgresql/data/students.csv
If the permissions aren’t enough, you can change them:
chmod 644 /var/lib/postgresql/data/students.csv
chown postgres:postgres /var/lib/postgresql/data/students.csv
This is for Linux. Setting up permissions in Windows is usually done by a Windows admin and is outside the scope of our course.
Limitations of the COPY Command
Working with COPY has a few quirks you should know about:
File path: use the absolute path, since PostgreSQL might not recognize relative paths.
File encoding: if your CSV file uses a different encoding than the database (like Windows-1251 instead of UTF-8), you need to specify the encoding explicitly:
COPY students
FROM '/var/lib/postgresql/data/students.csv'
WITH (FORMAT CSV, ENCODING 'WIN1251', HEADER TRUE);
File structure errors: if the file structure doesn’t match the table structure or the data is invalid (like text in a numeric field), the load will fail with an error. Check your data before loading!
Practical Uses
Using COPY to load data from the server has tons of use cases:
- Data migration: you can move large amounts of data between servers or databases.
- Integration with external systems: lots of systems export data as CSV files.
COPYlets you quickly load that data into PostgreSQL. - Preparing analytics reports: automated server-side data loading speeds up analyzing big chunks of info.
Common Errors and How to Fix Them
When working with COPY, you might run into a few errors:
Problem: File Not Accessible
Error message: could not open file for reading: Permission denied.
Solution: make sure the file is accessible to the PostgreSQL user (postgres) and check the permissions.
Problem: Wrong File Format
Error message: malformed CSV line.
Solution: check the file for empty lines, data errors, or weird characters. Make sure the delimiter is set correctly.
Problem: Data Structure Mismatch
Error message: ERROR: invalid input syntax for type integer.
Solution: make sure the columns in the table match the data structure in the file. For example, numbers should go into numeric columns, and dates into DATE columns.
Now you’ve got all the knowledge you need to use COPY effectively for loading data from the server. Use these skills in your projects to save time and boost your database productivity!
GO TO FULL VERSION