G.3. Reading CSV Files (read_csv) #
Required privileges: Superuser or the Postgres Pro AXE administrator.
For more information about access management, refer to Chapter 17.
Execute the following command on the Postgres Pro AXE server:
SELECT * FROM read_csv('path', optional_parameters);
Where:
path: The path to the CSV files that must be read.You can specify a value in one of the following formats:
a path to a single CSV file, such as
'file.csv'a glob pattern, such as
'data/*.csv'an array of paths, such as
['file1.csv', 'file2.csv']
optional_parameters: The optional parameters, specified in theparameter := valueformat.Available parameters:
all_varchar: Specifies whether to skip automatic detection of column data types and return all columns asVARCHAR.Possible values:
truefalse
Default value:
false.allow_quoted_nulls: Specifies whether to convert quoted values toNULLvalues.Possible values:
truefalse
Default value:
true.auto_detect: Specifies whether to automatically detect CSV parameters, such as column data types.Possible values:
truefalse
Default value:
true.auto_type_candidates: The data types to match against when automatically detecting column data types, specified as an array. If no type matches, theVARCHARtype is used.buffer_size: The size of read buffers, in bytes. It must be large enough to store four lines.Default value: 16 ×
max_line_size.comment: The character used to add comments. Lines starting with this character are ignored.Default value:
''.compression: The file compression method.Possible values:
auto_detectnonegzipzstd
Default value:
auto_detect— the compression method is detected automatically using file extensions.dateformat: The format used when parsing dates.Default value:
''.decimal_separator: The decimal separator character for numeric values.Default value:
..delim/sep: The separator character for columns. It can be up to four bytes.Default value:
,.escape: The character used to escape quotes within quoted values.Default value:
".encoding: The file encoding method.Possible values:
utf-8utf-16latin-1
Default value:
utf-8.filename: Specifies whether to return thefilenamecolumn with file paths. Since DuckDB v1.3.0, this column is added automatically.Possible values:
truefalse
Default value:
false.force_not_null: The columns for whichNULLmatching is disabled, so theirNULLvalues are returned as strings.Default value:
[]—NULLmatching is enabled for all columns.header: Specifies whether the first line of each CSV file contains column names.Possible values:
truefalse
Default value:
false.hive_partitioning: Specifies whether the directory is associated with a partitioned analytical table and whether partition columns are returned.Possible values:
truefalse
Default value: Detected automatically based on the directory structure.
For more information about Hive partitioning, refer to Section 15.2.
ignore_errors: Specifies whether to ignore parsing errors.Possible values:
truefalse
Default value:
false.max_line_size/maximum_line_size: The maximum line size, in bytes.Default value:
2000000.names/column_names: The column names used when CSV files do not contain headers, specified as an array.Default value:
''— column names are detected automatically.new_line: The character for new lines.Possible values:
\r\n\r\n
Default value:
''.normalize_names: Specifies whether to normalize column names by removing non-alphanumeric characters. Column names that match SQL reserved keywords are prefixed with an underscore (_).Possible values:
truefalse
Default value:
false.null_padding: Specifies whether to fill the missing columns withNULLvalues when a line has fewer columns than expected.Possible values:
truefalse
Default value:
false.nullstr/null: The strings to return asNULLvalues, specified as an array.Default value:
''.parallel: Specifies whether to use the parallel CSV reader.Possible values:
truefalse
Default value:
true.quote: The character used to quote values.Default value:
".rejects_scan: The name of the temporary table where information about file-level parsing errors is stored.Default value:
reject_scans.rejects_table: The name of the temporary table where information about line-level parsing errors is stored.Default value:
reject_errors.rejects_limit: The maximum number of line-level parsing errors per file to store.Default value:
0— all line-level parsing errors are stored.sample_size: The number of lines sampled within each CSV file for automatic detection of CSV parameters.Default value:
20480.skip: The number of lines to skip at the start of each CSV file.Default value:
0.store_rejects: Specifies whether to skip lines with parsing errors and store information about them in temporary tables specified by therejects_scanandrejects_tableparameters.Possible values:
truefalse
Default value:
false.strict_mode: Specifies whether to enforce the strictness level of the CSV reader.Possible values:
truefalse
If this parameter is set to
false, the CSV reader attempts to process structurally incorrect files, which may produce unexpected results.Default value:
true.thousands: The character used as the thousands separator in numeric values. It must differ from thedecimal_separatorvalue.Default value:
''.timestampformat: The format used when parsing timestamps.Default value:
''.types/dtypes/column_types: The column data types to use instead of the automatically detected ones.You can specify a value in one of the following formats:
an array of type names matching the column order, such as
['INTEGER', 'VARCHAR']a
STRUCTvalue with column names as keys, such as{'col1': 'INTEGER'}
Default value:
''— column types are detected automatically.union_by_name: Specifies whether columns from different files are aligned by name instead of by position.Possible values:
truefalse
If this parameter is set to
true, memory consumption increases.Default value:
false.
Postgres Pro AXE will return rows of CSV files.
Example G.5. Reading CSV Files
Contents of file.csv:
1;Alice;30;2020-01-15 2;Bob;25;2021-03-22 3;Charlie;35;2019-07-01
SELECT * FROM read_csv('file.csv',
delim := ';',
header := false,
names := ARRAY['id', 'name', 'age', 'hired'],
types := ARRAY['INTEGER', 'VARCHAR', 'INTEGER', 'DATE']);
id | name | age | hired
----+---------+-----+------------
1 | Alice | 30 | 2020-01-15
2 | Bob | 25 | 2021-03-22
3 | Charlie | 35 | 2019-07-01
You can select specific columns by assigning an alias to a procedure, such as r, and using the r[' syntax: column_name']
SELECT r['id'], r['name'] FROM read_csv('file.csv',
delim := ';',
header := false,
names := ARRAY['id', 'name', 'age', 'hired'],
types := ARRAY['INTEGER', 'VARCHAR', 'INTEGER', 'DATE']) r
WHERE r['age'] = 25;
id | name ----+------ 2 | Bob