Creating table from csv using sql query

247 Views Asked by At

I was creating table name "tripsdata" in SQL query tool, which is done successfully. Now I have to insert values into the table and I used the below syntax :

LOAD data INFILE 'tripsdata.csv' 
INTO TABLE tripsdata
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

It keeps giving me error - "ERROR: syntax error at or near "data" LINE 2: LOAD data INFILE 'tripsdata.csv' "

Can you share the error and rectification?

tried what I mentioned above. I want to create this table using SQL query:

3

There are 3 best solutions below

0
Panagiotis Kanavos On

LOAD DATA is a MySQL command. PostgresSQL uses COPY. I suspect what you're trying to do is import a CSV file while skipping the header row. You can do that with the FORMAT CSV and HEADER ON options:

COPY country 
FROM '/path/to/tripsdata.csv' 
(FORMAT CSV,HEADER ON);
0
Nnaemeka Daniel John On

You can as well try this query:

COPY tripsdata FROM 'tripsdata.csv' WITH CSV HEADER;

0
Hanisha Dua On

Thank you!

I tried this and the error pertains - "ERROR: could not open file "/path/to/tripsdata.csv" for reading: No such file or directory HINT: COPY FROM instructs the PostgreSQL server process to read a file. You may want a client-side facility such as psql's \copy."