6

I'm trying to export a table from Google Cloud SQL into a CSV file using the gcloud sql export csv command from Gcloud SDK but I don't have the option to export on top of the file also the names of the columns. Is there any workaround for this?

Thanks

billiout
  • 695
  • 1
  • 8
  • 22
  • 1
    Check out this thread about MySQL exports with column headers https://stackoverflow.com/questions/5941809/include-headers-when-using-select-into-outfile. – jrhode2 Aug 27 '21 at 16:28

1 Answers1

4

I believe that this command should produce the necessary functionality:

gcloud sql export csv instance_name gs://bucket_name/ --query="SELECT 'columnname1' , 'columnname2', 'columnname3' UNION SELECT columnname1, columnname2, columnname3 FROM table_name" --database=database_name

One downside of doing it this way is you have to specify all columns. If there is a large number of those, it might be better to write some script to write the SQL query part.

A feature request has been created on your behalf. Please star it so that you could receive updates about this feature request and do not hesitate to add additional comments to provide details of the desired implementation. You can track the feature request by following this link.

Philipp Sh
  • 967
  • 5
  • 11
  • 1
    Thank you! I'll use the above command until this implemented. It would be a nice feature as well to allow you to split the resulted CSV from a big query into smaller parts if you specify a max-size per file option with the gcloud command. – billiout Jul 11 '18 at 17:52
  • 2
    This would only work if `columnname1`, `columnname2`, `columnname3` are of type string/text/varchar/etc. no? – KinaneD Oct 02 '19 at 14:04
  • Yes you need to manually cast the columns themselves to a textual SQL datatype. Shouldn't be a problem if you're going to be converting to .csv anyway. – hkh Apr 14 '22 at 15:32