Importing Data into Cloud SQL

This page describes how to import data into Google Cloud SQL using a SQL dump file.

Importing SQL dump files to Cloud SQL

Before you begin

  • The database you are importing into must already exist on your Cloud SQL instance.

    For help with creating a database, see Creating a database.

Importing a SQL dump file

To import a SQL dump file to a Cloud SQL instance:

Console

  1. Create your SQL dump file.
  2. Create a Cloud Storage bucket, if you haven't already.

    For help with creating a bucket, see Creating Storage Buckets.

  3. Upload the dump file to your bucket.

    For help with uploading files to buckets, see Uploading an Object.

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

    Go to the Cloud SQL Instances page

  5. Select the instance to open its Instance details page.
  6. Click Import in the button bar.
  7. Enter the path to the bucket and SQL dump file you uploaded, or browse to it by clicking Browse and doubleclicking the name of the bucket and then selecting the file and clicking Select.

    You can use a compressed (.gz) or an uncompressed (.sql) file.

  8. Provide the name of the database you want to import the data into.
  9. Click Import to start the import.

gcloud

  1. Create your SQL dump file.
  2. Create a Cloud Storage bucket, if you haven't already.

    For help with creating a bucket, see Creating Storage Buckets.

  3. Upload the dump file to your bucket.

    For help with uploading files to buckets, see Uploading an Object.

  4. Describe the instance you are importing to:
    gcloud sql instances describe [INSTANCE_NAME]
    
  5. Copy the serviceAccountEmailAddress field.
  6. Add the service account to the bucket ACL as a writer:
    gsutil acl ch -u [SERVICE_ACCOUNT_ADDRESS]:W gs://[BUCKET_NAME]
    
  7. Add the service account to the import file as a reader:
    gsutil acl ch -u [SERVICE_ACCOUNT_ADDRESS]:R gs://[BUCKET_NAME]/[IMPORT_FILE_NAME]
    
  8. Import the database:
    gcloud sql instances import [INSTANCE_NAME] gs://[BUCKET_NAME]/[IMPORT_FILE_NAME] \
                                --database [DATABASE_NAME]
    

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

    If the command returns an error like `ERROR_RDBMS`, review the permissions; this error is often due to permissions issues.

  9. If you do not need to retain the permissions provided by the ACLs you set previously, remove them now:
    gsutil acl ch -d [SERVICE_ACCOUNT_ADDRESS] gs://[BUCKET_NAME]/[IMPORT_FILE_NAME]
    gsutil acl ch -d [SERVICE_ACCOUNT_ADDRESS] gs://[BUCKET_NAME]
    

cURL

  1. Create your SQL dump file.
  2. Create a Cloud Storage bucket, if you haven't already.

    For help with creating a bucket, see Creating Storage Buckets.

  3. Upload the dump file to a Cloud Storage bucket.

    For help with uploading files to buckets, see Uploading an Object.

  4. Provide your instance with write permissions to your bucket, and read access to the file.

    For help with setting ACLs, see Setting ACLs.

  5. Import your dump file:
    ACCESS_TOKEN="$(gcloud auth application-default print-access-token)"
    curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
         --header 'Content-Type: application/json' \
         --data '{"importContext":
                    {"fileType": "SQL",
                     "uri": "gs://[BUCKET_NAME]/[PATH_TO_DUMP_FILE]",
                     "database": "[DATABASE_NAME]" }}' \
       -X POST \
       https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[INSTANCE_NAME]/import
    
    For the complete list of parameters for this request, see the instances:import page.
  6. If you do not need to retain the permissions provided by the ACLs you set previously, remove them now.

What's next

Send feedback about...

Cloud SQL for PostgreSQL