Yêu cầu thg 9 29, 2023 8:02 SA 77 0 1
  • 77 0 1
0

Import dữ liệu có new line từ file CSV ở PostgreSQL

Chia sẻ
  • 77 0 1

Mình có file CSV với dữ liệu như sau:

id,name,lyric,genre_id,created_at,updated_at,link,tempo
1,test,"this is the test
this is the test new line

this is the test new new line",2,2023-09-22 14:33:34.095762,2023-09-22 14:33:43.356895,https://open.spotify.com/embed/track/01IZAuQsBO00iLKFO9LsFf?utm_source=generator,100
2,test,"this is the test
this is the test new line

this is the test new new line",2,2023-09-22 14:33:34.095762,2023-09-22 14:33:43.356895,https://open.spotify.com/embed/track/01IZAuQsBO00iLKFO9LsFf?utm_source=generator,100

Và mình chạy lệnh import như này

psql -U postgres -d db -c "\\copy songs FROM 'data.csv' WITH CSV HEADER DELIMITER ','"

Nhưng vấn đề mình gặp là ở CSV, mỗi lần newline là 1 dữ liệu mới. Làm thế nào để import dữ liệu có new line trong trường hợp này nhỉ

1 CÂU TRẢ LỜI


Đã trả lời thg 10 18, 2023 3:54 SA
Đã được chấp nhận
+1

thêm vào lệnh đã có để psql hiểu là dùng " để wrapper string và escape các ký tự đặc biệt như new line

\COPY songs FROM 'data.csv' WITH CSV HEADER DELIMITER ',' QUOTE '"' ESCAPE '\' ;
Chia sẻ
Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí