How to export database table data to a CSV file with platform-cli

Goal

You want to export a database table (or a part of it) to a CSV file.

Assumptions

You will need:

  • Platform CLI

Platform SQL

The platform CLI tool allows you to connect to your database using the command
platform sql

You can also specify which query you want to run. This will show you all the tables in your database.
platform sql "SHOW TABLES"

Exporting data

With this, piping it to a file then becomes trivial. Just add --raw to your command to make the output machine readable. If there is more than one column, it will be separated by a TAB character.
platform sql "SHOW TABLES" --raw > all_tables.csv

To export data from the table items, in the database main simply do:

platform sql "SELECT id, title FROM items LIMIT 10" --raw --schema=main > tab_separated_output.csv

The resulting file can be opened with any spreadsheet software (libreoffice, openoffice, MS Office). Simply select TAB as a column separator.

Warning

Note that these queries will run on your database server. If the tables are large, exporting them can take a while.

More information

For more information on what is possible with platform sql try
platform sql --help

or checkout the platform cli git repository https://github.com/platformsh/platformsh-cli