0
0
PostgreSQLquery~10 mins

COPY command for bulk data loading in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - COPY command for bulk data loading
Start COPY command
Read source file
Parse each line
Convert data to table format
Insert rows into table
Finish and report rows loaded
The COPY command reads data from a file, parses it line by line, converts it into table rows, inserts them, and finishes by reporting how many rows were loaded.
Execution Sample
PostgreSQL
COPY employees FROM '/tmp/employees.csv' DELIMITER ',' CSV HEADER;
This command loads data from a CSV file into the employees table, using comma as separator and skipping the header line.
Execution Table
StepActionInput/StateOutput/Result
1Start COPY commandCommand issuedReady to read file
2Open file '/tmp/employees.csv'File openedFile stream ready
3Read first lineLine: 'id,name,salary'Header detected, skipped
4Read second lineLine: '1,John Doe,50000'Parsed row: (1, 'John Doe', 50000)
5Insert row into employeesRow dataRow inserted
6Read third lineLine: '2,Jane Smith,60000'Parsed row: (2, 'Jane Smith', 60000)
7Insert row into employeesRow dataRow inserted
8End of file reachedNo more linesCOPY finished, 2 rows loaded
💡 End of file reached, all rows processed
Variable Tracker
VariableStartAfter Step 4After Step 6Final
Current LineNone'1,John Doe,50000''2,Jane Smith,60000'EOF
Rows Inserted0122
Key Moments - 3 Insights
Why does COPY skip the first line in this example?
Because the command uses CSV HEADER option, the first line is treated as column names and skipped, as shown in execution_table row 3.
What happens if the file has more rows than shown?
COPY will continue reading and inserting rows until it reaches the end of the file, as indicated by the loop in execution_table rows 4-7.
Why is the delimiter important in COPY?
The delimiter tells COPY how to split each line into columns. Here, ',' is used to separate fields, matching the CSV format.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after reading the first data line?
AHeader detected, skipped
BParsed row: (1, 'John Doe', 50000)
CRow inserted
DFile stream ready
💡 Hint
Check row 4 in the execution_table where the first data line is parsed.
At which step does the COPY command finish loading data?
AStep 6
BStep 7
CStep 8
DStep 3
💡 Hint
Look at the exit note and the last row in execution_table.
If the CSV file did not have a header, what would change in the execution?
AThe first line would be inserted as data
BCOPY would fail immediately
CRows inserted would be zero
DDelimiter would change automatically
💡 Hint
Refer to key_moments about the HEADER option and execution_table row 3.
Concept Snapshot
COPY command syntax:
COPY table_name FROM 'file_path' DELIMITER ',' CSV HEADER;

- Reads data from a file
- Parses lines using delimiter
- Skips header if CSV HEADER used
- Inserts rows into table
- Efficient for bulk loading
Full Transcript
The COPY command in PostgreSQL loads bulk data from a file into a table. It starts by opening the file, then reads each line. If the CSV HEADER option is used, the first line is skipped as it contains column names. Each subsequent line is parsed using the specified delimiter, converting text into table columns. Each parsed row is inserted into the table. This continues until the end of the file is reached, and the command reports how many rows were loaded. This process is efficient for loading large amounts of data quickly.