Export and import using SQL dump files

This page describes exporting and importing data into Cloud SQL instances using SQL dump files.

Before you begin

Exports use database resources, but they do not interfere with normal database operations unless the instance is under-provisioned.

For best practices, see Best Practices for Importing and Exporting Data.

After completing an import operation, verify the results.

Export data from Cloud SQL for PostgreSQL

Required roles and permissions for exporting from Cloud SQL for PostgreSQL

To export data from Cloud SQL into Cloud Storage, the user initiating the export must have one of the following roles:

Additionally, the service account for the Cloud SQL instance must have one of the following roles:

  • The storage.objectAdmin Identity and Access Management (IAM) role
  • A custom role, including the following permissions:
    • storage.objects.create
    • storage.objects.list (for exporting files in parallel only)
    • storage.objects.delete (for exporting files in parallel only)

For help with IAM roles, see Identity and Access Management.

Export to a SQL dump file from Cloud SQL for PostgreSQL

When you use Cloud SQL to perform an export, whether from the Google Cloud console, the gcloud CLI, or the API, you are using the pg_dump utility, with the options required to ensure that the resulting export file is valid for import back into Cloud SQL.

If you plan to import your data into Cloud SQL, you must follow the instructions provided in Exporting data from an external database server so that your SQL dump file is formatted correctly for Cloud SQL.

To export data from a database on a Cloud SQL instance to a SQL dump file in a Cloud Storage bucket:

Console

  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. To open the Overview page of an instance, click the instance name.
  3. Click Export.
  4. In the File format section, click SQL to create a SQL dump file.
  5. In the Data to export section, use the drop-down menu to select the database you want to export from.
  6. In the Destination section, select Browse to search for a Cloud Storage bucket or folder for your export.
  7. Click Export to begin the export.

gcloud

  1. Create a Cloud Storage bucket.
  2. Find the service account for the Cloud SQL instance you're exporting from. You can do this running the gcloud sql instances describe command. Look for the serviceAccountEmailAddress field in the output.
    gcloud sql instances describe INSTANCE_NAME
      
  3. Use gcloud storage buckets add-iam-policy-binding to grant the storage.objectAdmin IAM role to the service account. For help with setting IAM permissions, see Using IAM permissions.
  4. Export the database to your Cloud Storage bucket: