简体   繁体   中英

PostgreSQL, import a csv file

I'm trying to copy a CSV file into an empty table, after trying to match the columns in the CSV which failed with the exact same error.

COPY books 
FROM '/path/to/file/books.csv' CSV HEADER; 

error:

ERROR:  extra data after last expected column
CONTEXT:  COPY books, line 2: "1,Harry Potter and the Half-Blood Prince (Harry Potter  #6),J.K. Rowling/Mary GrandPré,4.57,0439785..."
SQL state: 22P04

Also, I would like that the publication_date will be of date type, so it can be queried, How can that be applied during copying?

a piece of the CSV file:

bookID| title        | authors  |   average_rating  | isbn|isbn13 |num_pages  | ratings_count| text_reviews_count| publication_date|
----------------------------------------------------------------------------------------------------------------------------------
1     | harry potter |          |                   |             |
      |(harry Potter |          |                   |             |
      |     #6)      | author   | 4                 |"num" | "num"| 600       | 3243         | 534                | 01/01/2000    |
SELECT * FROM books

output:

bookID| title    | authors  |   average_rating |    isbn   |isbn13| language_code    
---------------------------------------------------------------------------------
text  | character| text     | integer        | text   | text  |  character |      
      |  varying |          |                |        |       |  varying               

| num_pages  | ratings_count| text_reviews_count| publication_date| publisher
-------------------------------------------------------------------------------
| integer    | bigint       | bigint            | date            |  character
                                                                     varying


First of all, the columns number mismatch from CSV file and TABLE , but you can specify via COPY command the columns for the table that you need:

  COPY books (bookID,title,authors,average_rating,isbn,isbn13,num_pages,ratings_count , text_reviews_count , publication_date) FROM '/path/to/file/books.csv' CSV header delimiter ','; 

You can specify you delimiter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM