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 the parameter := value format.

    Available parameters:

    • all_varchar: Specifies whether to skip automatic detection of column data types and return all columns as VARCHAR.

      Possible values:

      • true

      • false

      Default value: false.

    • allow_quoted_nulls: Specifies whether to convert quoted values to NULL values.

      Possible values:

      • true

      • false

      Default value: true.

    • auto_detect: Specifies whether to automatically detect CSV parameters, such as column data types.

      Possible values:

      • true

      • false

      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, the VARCHAR type 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_detect

      • none

      • gzip

      • zstd

      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-8

      • utf-16

      • latin-1

      Default value: utf-8.

    • filename: Specifies whether to return the filename column with file paths. Since DuckDB v1.3.0, this column is added automatically.

      Possible values:

      • true

      • false

      Default value: false.

    • force_not_null: The columns for which NULL matching is disabled, so their NULL values are returned as strings.

      Default value: [] — NULL matching is enabled for all columns.

    • header: Specifies whether the first line of each CSV file contains column names.

      Possible values:

      • true

      • false

      Default value: false.

    • hive_partitioning: Specifies whether the directory is associated with a partitioned analytical table and whether partition columns are returned.

      Possible values:

      • true

      • false

      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:

      • true

      • false

      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:

      • true

      • false

      Default value: false.

    • null_padding: Specifies whether to fill the missing columns with NULL values when a line has fewer columns than expected.

      Possible values:

      • true

      • false

      Default value: false.

    • nullstr / null: The strings to return as NULL values, specified as an array.

      Default value: ''.

    • parallel: Specifies whether to use the parallel CSV reader.

      Possible values:

      • true

      • false

      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 the rejects_scan and rejects_table parameters.

      Possible values:

      • true

      • false

      Default value: false.

    • strict_mode: Specifies whether to enforce the strictness level of the CSV reader.

      Possible values:

      • true

      • false

      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 the decimal_separator value.

      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 STRUCT value 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:

      • true

      • false

      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['column_name'] syntax:

  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
  

FAQ