How to import tables with missing values?

前端 未结 2 961
清歌不尽
清歌不尽 2021-01-04 10:31

I use basketball data tables to get some understanding of Postgres 9.2 & phppgadmin. Therefore I would like to import csv tables into that database. However, I get:

相关标签:
2条回答
  • 2021-01-04 11:11

    ERROR: missing data for column "year" CONTEXT: COPY coaches, line 1: ""coachid";"year";"yr_order";"firstname";"lastname";"season_win";"season_loss";"playoff_win";"playoff..."

    This type of error is also the result of a Table-mismatch. The table you are importing the text file into either has more columns or less columns than the text file has.

    0 讨论(0)
  • 2021-01-04 11:21

    You can have columns missing for the whole table. Tell COPY (or the psql wrapper \copy) to only fill those columns appending a column list to the table, for instance:

    \copy coaches (coachid, yr_order, firstname)
    FROM '/Users/.../coaches_data.csv' (FORMAT csv, HEADER, DELIMITER ',');

    Missing values are filled in with column defaults. Per documentation:

    If there are any columns in the table that are not in the column list, COPY FROM will insert the default values for those columns.

    But you cannot have values missing for just some rows. That's not possible. The text representation of NULL can be used (overruling respective column defaults).

    It's all in the manual, really:

    • SQL-COPY
    • psql \copy
    0 讨论(0)
提交回复
热议问题