Exporting Data from Cloud SQL

This page describes how to export data from Google Cloud SQL to Google Cloud Storage. You can then download your data from Cloud Storage to your local environment if you want to access it locally.

To learn more about exporting data, read the Overview of Importing and Exporting Data.

Before you begin

Decide what file type you are creating: a SQL dump file or a CSV file. To export your data to an external MySQL database, such as an external replica for Cloud SQL, create a SQL dump file. To export your data for use with other databases or tools, export to a CSV file.

Exporting data to a SQL dump file

Console

  1. Go to the Cloud SQL Instances page in the Google Cloud Platform Console.

    Go to the Cloud SQL Instances page

  2. Click the instance you want to export data from to open its Instance details page.
  3. Click Export in the button bar.
  4. Under Cloud Storage file, choose where to save your export in Google Cloud Storage.
  5. Set the Format to SQL.
  6. Click Show advanced options and enter a comma-separated list of all of the databases that you have created.
  7. Click Export to start the export.

gcloud (2nd Gen)

To export a dump file from an instance to a Google Cloud Storage bucket:

  1. Create a bucket for the export:
    gsutil mb -p [PROJECT_NAME] -l [LOCATION_NAME] gs://[BUCKET_NAME]
    

    This step is not required, but strongly recommended, so you do not open up access to any other data.

  2. Describe the instance you are exporting from:
    gcloud sql instances describe [INSTANCE_NAME]
    
  3. Copy the serviceAccountEmailAddress field.
  4. Add the service account to the bucket ACL as a writer:
    gsutil acl ch -u [SERVICE_ACCOUNT_ADDRESS]:W gs://[BUCKET_NAME]
    
  5. Export the database:
    gcloud sql instances export [INSTANCE_NAME] gs://[BUCKET_NAME]/sqldumpfile.gz --database [DATABASE_NAME]
    

    For information about using the export command, see the sql instances export command reference page.

  6. Remove the service account from the bucket ACL:
    gsutil acl ch -d [SERVICE_ACCOUNT_ADDRESS] gs://[BUCKET_NAME]
    

    This step is strongly recommended for security.

gcloud (1st Gen)

gcloud sql instances export [INSTANCE_NAME] gs://[BUCKET_NAME]/sqldumpfile.gz --database [DATABASE_NAMES]

If you get an error like `ERROR_RDBMS`, you might not have the proper permissions to export to that bucket. Check that you have at least `WRITER` permissions on the bucket. For more information on configuring access control in Google Cloud Storage, see Create and Manage Access Control Lists.

For information about using the export command, see the sql instances export command reference page.

cURL

  1. Create a bucket for the export:
    gsutil mb -p [PROJECT_NAME] -l [LOCATION_NAME] gs://[BUCKET_NAME]
    

    This step is not required, but strongly recommended, so you do not open up access to any other data.

  2. Provide your instance with write permissions to your bucket.

    For help with setting ACLs, see Setting ACLs.

  3. Export your database:
    ACCESS_TOKEN="$(gcloud auth application-default print-access-token)"
    curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
         --header 'Content-Type: application/json' \
         --data '{"exportContext":
                    {"fileType": "SQL",
                     "uri": "gs://<BUCKET_NAME>/<PATH_TO_DUMP_FILE>",
                     "databases": ["<DATABASE_NAME1>", "<DATABASE_NAME2>"] }}' \
       -X POST \
       https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[INSTANCE_NAME]/export
    
  4. Remove the ACLs you set in the previous step.

    This step is strongly recommended for security.

For the complete list of parameters for this request, see the instances:export page.

Exporting data to a CSV file

You can export your data in CSV format if you need to access it with a tool other than MySQL.

To export data to a CSV file:

  1. Go to the Cloud SQL instances page in the Google Cloud Platform Console.
    Go to the Cloud SQL instances page
  2. Click the instance to open its Instance details page.
  3. Click Export.
  4. Under Cloud Storage file, choose where to save your export in Google Cloud Storage.

    For Second Generation instances, you can use a file extension of ".gz" to compress your export file.

  5. Set the Format to CSV.
  6. Enter a SQL query to specify the data to export.

    For example, to export the entire contents of the `entries` table in the `guestbook` database, you would enter `SELECT * FROM guestbook.entries;`. You must specify a table; you cannot export an entire database in CSV format.

  7. Click Export to start the export.

What's next

Send feedback about...

Cloud SQL for MySQL