Basic questions about Cloud SQL

I'm trying to populate a cloud sql database using a cloud storage bucket, but I'm getting some errors. The csv has the headers (or column names) as first row and does not have all the columns (some columns in the database can be null, so I'm loading the data I need for now).

The database is in postgresql and this is the first database in GCP I'm trying to configure and I'm a little bit confused.

  1. Does it matters if the csv file has the column names?
  2. Does the order of the columns matter in the csv file? (I guess they do if there are not present in the csv)
  3. The PK of the table is a serial number, which I'm not including in the csv file. Do I need to include also the PK? I mean, because its a serial number it should be "auto assigned", right?

Sorry for the noob questions and thanks in advance :)

Upvotes: 0

Views: 66

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 247320

This is all covered by the COPY documentation.

  1. It matters in that you will have to specify the HEADER option so that the first line is skipped:

    [...] on input, the first line is ignored.

  2. The order matters, and if the CSV file does not contain all the columns in the same order as the table, you have to specify them with COPY:

    COPY mytable (col12, col2, col4, ...) FROM '/dir/afile' OPTIONS (...);
    
  3. Same as above: if you omit a table column in the column list, it will be filled with the default value, in that case that is the autogenerated number.

Upvotes: 1

Related Questions