Exporting from Cloud SQL to CSV with column headers
Asked Answered
D

1

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

Dimension answered 10/7, 2018 at 18:27 Comment(1)
Check out this thread about MySQL exports with column headers #5942309.Asben
B
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.

Banns answered 11/7, 2018 at 14:34 Comment(3)
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.Dimension
This would only work if columnname1, columnname2, columnname3 are of type string/text/varchar/etc. no?Proficiency
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.Ohare

© 2022 - 2024 — McMap. All rights reserved.