DuckDB provides first-class support for CSV (Comma-Separated Values) files with automatic dialect detection, type inference, and flexible configuration options.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/duckdb/duckdb/llms.txt
Use this file to discover all available pages before exploring further.
Reading CSV Files
The simplest way to read a CSV file is to reference it directly in a query:- Detects the delimiter (comma, tab, pipe, etc.)
- Identifies header rows
- Infers column types
- Handles quoted values and escape characters
Using read_csv_auto
For more control, use theread_csv_auto function:
CSV Options
Delimiter Options
Header Options
Null Handling
Quote and Escape Characters
Writing CSV Files
Use theCOPY statement to write data to CSV files:
Loading into Tables
Import CSV data into existing tables:Partial Column Import/Export
Work with specific columns only:Multiple Files
Read multiple CSV files at once:Hive Partitioning
DuckDB supports Hive-style partitioning for efficient file filtering:- Extracts partition values from directory names
- Adds them as columns to the result
- Skips reading files that don’t match filter conditions
Type Detection
Override automatic type detection:Common Patterns
Skip Rows
Limit Rows During Import
Handle Different Line Endings
DuckDB automatically handles Windows (CRLF), Unix (LF), and Mac (CR) line endings:Error Handling
Control how DuckDB handles malformed rows:Performance Tips
- DuckDB automatically parallelizes CSV reading across multiple threads
- Use
COPYinstead ofINSERT INTO ... SELECTfor bulk loading - For very large files, consider converting to Parquet format for faster subsequent queries
- Use column projection to read only needed columns:
SELECT col1, col2 FROM 'file.csv'