Skip to main content

CSV Import

To read data from a CSV file, use the read_csv function in the FROM clause of a query:

Query
SELECT * FROM read_csv('input.csv');
Result
 id | name    | col1 | col2 | some column name | i | a  | b  | c  | number | number1 | number2----+---------+------+------+------------------+---+----+----+----+--------+---------+---------  8 | fixture |  1.5 |  2.5 | delta            | 4 | 13 | 23 | 33 |     30 |     400 |    4000  9 | sample  |  3.5 |  4.5 | epsilon          | 5 | 14 | 24 | 34 |     40 |     500 |    5000

Alternatively, you can omit the read_csv function and let SereneDB infer the format from the file name:

Query
SELECT * FROM 'input.csv';
Result
 id | name    | col1 | col2 | some column name | i | a  | b  | c  | number | number1 | number2----+---------+------+------+------------------+---+----+----+----+--------+---------+---------  8 | fixture |  1.5 |  2.5 | delta            | 4 | 13 | 23 | 33 |     30 |     400 |    4000  9 | sample  |  3.5 |  4.5 | epsilon          | 5 | 14 | 24 | 34 |     40 |     500 |    5000

To create a new table using the result from a query, use CREATE TABLE ... AS SELECT statement:

Query
CREATE TABLE new_tbl AS    SELECT * FROM read_csv('input.csv');

We can use SereneDB's optional FROM-first syntax to omit SELECT *:

Query
CREATE TABLE new_tbl AS    FROM read_csv('input.csv');

To load data into an existing table from a query, use INSERT INTO from a SELECT statement:

Query
INSERT INTO tbl    SELECT * FROM read_csv('input.csv');

Alternatively, the COPY statement can also be used to load data from a CSV file into an existing table:

Query
COPY tbl FROM 'input.csv';

For additional options, see the CSV import reference and the COPY statement documentation.