<br />

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

> [!NOTE]
> **Note:** If you're migrating an entire database from
> a supported database server (on-premises, in AWS, or Cloud SQL) to a new
> Cloud SQL instance, you can use the
> [Database Migration Service](https://docs.cloud.google.com/database-migration/docs.md.txt) instead of exporting
> and then importing files.
>
> If you're exporting because you want to create a new instance from the exported
> file, consider
> [restoring from a backup to a different instance](https://docs.cloud.google.com/sql/docs/postgres/backup-recovery/restoring.md.txt#restorebackups-another-instance)
> or [cloning the instance](https://docs.cloud.google.com/sql/docs/postgres/clone-instance.md.txt).
>
> You can cancel the import of data into Cloud SQL instances and
> the export of data from the instances.
> This data is contained in CSV files. For
> more information about cancelling an import or export operation, see [Cancel the import
> and export of data](https://docs.cloud.google.com/sql/docs/postgres/import-export/cancel-import-export).

## Before you begin

Before you begin an export or import operation:

- Ensure that your database has adequate free space.
- Export and import operations use database resources, but they do not interfere with normal database operations unless the instance is under-provisioned.

> [!NOTE]
> **Important**: Before starting a large operation, ensure that at least 25 percent of the disk is free on the instance. Doing so helps prevent issues with aggressive autogrowth, which can adversely affect the availability of the instance.

- [Verify](https://docs.cloud.google.com/sql/docs/postgres/import-export.md.txt#verify) that the CSV file has the expected data and that it's in the correct format. CSV files must have one line for each row of data fields.
- Follow the [best practices for exporting and importing data.](https://docs.cloud.google.com/sql/docs/postgres/import-export.md.txt)

## 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:

- The [Cloud SQL Editor](https://docs.cloud.google.com/sql/docs/postgres/iam-roles.md.txt) role
- A [custom role](https://docs.cloud.google.com/iam/docs/understanding-custom-roles), including the following permissions:
  - `cloudsql.instances.get`
  - `cloudsql.instances.export`

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](https://docs.cloud.google.com/storage/docs/access-control/iam.md.txt).

> [!NOTE]
> **Note** : The changes that you make to the IAM permissions and roles might take a few minutes to take effect. For more information, see [Access change propagation](https://docs.cloud.google.com/iam/docs/access-change-propagation.md.txt).

<br />

### Export data to a CSV file from Cloud SQL for PostgreSQL

You can export your data in CSV format, which is usable by other tools and
environments. Exports happen at the database level. During a CSV export, you can
specify the schemas to export. All schemas at the database level are
eligible for export.

> [!NOTE]
> **Note:** Cloud SQL uses double quotes (hex value "22") as the default escape character. This can be a problem for databases where values for `NULL` are entered as string literals. When importing a file that was exported using the default escape character, the file doesn't treat the value as `NULL` but as `"NULL"`. We recommend that you use `--escape="5C"` to override the default when you export the file.

> [!NOTE]
> **Note:** You cannot export to a CSV file from a read replica instance. The export operation creates an export user and grants that user select permissions on the database that the user wants to export. Because read replica instances run in read-only mode, these operations fail.

To export data from a database on a Cloud SQL instance to a CSV 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](https://console.cloud.google.com/sql)
2. To open the **Overview** page of an instance, click the instance name.
3. Click **Export**.
4. In the **File format** section, click **CSV**.
5. In the **Data to export** section, select the name of the database from the **Database** drop-down menu.
6.
   For **SQL query**, enter a SQL query to specify the table to export
   data from.


   For example, to export the entire contents of the `entries`
   table in the `guestbook` database, you enter

   ```bash
   SELECT * FROM guestbook.entries;
   ```
   Your query must specify a table in the specified database. You can't export an entire database in CSV format.

   <br />

   > [!NOTE]
   > **Note:**While in-transit, the query might be
   > processed in intermediate locations other than the location of the target
   > instance

7. Optional: Select **Offload export to a temporary instance** to allow other operations to occur while the export is ongoing.
8. In the **Destination** section, add the name of the bucket, folder, and file that you want to export to, or click **Browse** to find or create a bucket, folder, or file.

   If you click **Browse**:
   1. In the **Location** section, select a Cloud Storage bucket or folder for your export.

      > [!NOTE]
      > **Note:** If you need to create a new storage bucket, click the bucket with the **+** sign in the **Location** section. This opens **Create a bucket** , where you can add a name and other configurations for the bucket. Select a **standard** storage class for your bucket.

   2. In the **Name** box, add a name for the `CSV`
      file, or select an existing file from the list
      in the **Location** section.

      You can use a file extension of `.gz` (the complete
      extension would be `.csv.gz`) to compress your export
      file.
   3. Click **Select**.
9. Click **Export** to start the export.

### gcloud

1. [Create a Cloud Storage bucket.](https://docs.cloud.google.com/storage/docs/creating-buckets.md.txt)
2. Upload the file to your bucket.

   For help with uploading files to buckets, see
   [Uploading objects](https://docs.cloud.google.com/storage/docs/uploading-objects.md.txt).
3. Find the service account for the Cloud SQL instance you're exporting from. You can do this running the [`gcloud sql instances describe`](https://docs.cloud.google.com/sdk/gcloud/reference/sql/instances/describe) command. Look for the `serviceAccountEmailAddress` field in the output.

   ```bash
   gcloud sql instances describe INSTANCE_NAME
   ```
4. Use `
   gcloud storage buckets add-iam-policy-binding` to grant the `storage.objectAdmin` [IAM role](https://docs.cloud.google.com/storage/docs/access-control/iam-roles.md.txt) to the Cloud SQL instance service account. For help with setting IAM permissions, see [Using IAM permissions](https://docs.cloud.google.com/storage/docs/access-control/using-iam-permissions.md.txt).
5. Export the database: **Note:** Use the `--offload` flag if you want to use serverless export. Otherwise, remove it from the following command.

   ```
   gcloud sql export csv INSTANCE_NAME gs://BUCKET_NAME/FILE_NAME \
   --database=DATABASE_NAME \
   --offload \
   --query=SELECT_QUERY
   ```

   For information about using the `export csv` command, see the
   [`sql export csv`](https://docs.cloud.google.com/sdk/gcloud/reference/sql/export/csv)
   command reference page.

   > [!NOTE]
   > **Note:**While in-transit, the SELECT_QUERY might be
   > processed in intermediate locations other than the location of the target
   > instance.

6. If you do not need to retain the IAM role you set previously, [revoke](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt#revoking-console) it now.

### REST v1

1. Create a bucket for the export:

   ```
   gcloud storage buckets create gs://BUCKET_NAME --location=LOCATION_NAME --project=PROJECT_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 the `storage.objectAdmin` [IAM role](https://docs.cloud.google.com/storage/docs/access-control/iam-roles.md.txt) for your bucket. For help with setting IAM permissions, see [Using
   IAM permissions](https://docs.cloud.google.com/storage/docs/access-control/using-iam-permissions).
3.
   Export your database:


   Before using any of the request data,
   make the following replacements:
   - <var translate="no">project-id</var>: The project ID
   - <var translate="no">instance-id</var>: The instance ID
   - <var translate="no">bucket_name</var>: The Cloud Storage bucket name
   - <var translate="no">path_to_csv_file</var>: The path to the CSV file
   - <var translate="no">database_name</var>: The name of a database inside the Cloud SQL instance
   - <var translate="no">offload</var>: Enables serverless export. Set to `true` to use serverless export.

     > [!NOTE]
     > **Note:** Serverless export costs extra. See the [pricing page](https://cloud.google.com/sql/pricing#storage-networking-prices).

   - <var translate="no">select_query</var>: SQL query for export (optional)

     > [!NOTE]
     > **Note:** While in-transit, the query might be processed in intermediate locations other than the location of the target instance.

   - <var translate="no">escape_character</var>: The character that should appear before a data character that needs to be escaped. The value of this argument has to be a character in Hex ASCII Code. For example, "22" represents double quotes. (optional)
   - <var translate="no">quote_character</var>:The character that encloses values from columns that have a string data type. The value of this argument has to be a character in Hex ASCII Code. For example, "22" represents double quotes. (optional)
   - <var translate="no">fields_terminated_by</var>: The character that split column values. The value of this argument has to be a character in Hex ASCII Code. For example, "2C" represents a comma. (optional)
   - <var translate="no">lines_terminated_by</var>: The character that split line records. The value of this argument has to be a character in Hex ASCII Code. For example, "0A" represents a new line. (optional)


   HTTP method and URL:

   ```
   POST https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/export
   ```


   Request JSON body:

   ```
   {
    "exportContext":
      {
         "fileType": "CSV",
         "uri": "gs://bucket_name/path_to_csv_file",
         "databases": "database_name",
         "offload": true | false
         "csvExportOptions":
          {
              "selectQuery":"select_query",
              "escapeCharacter":"escape_character",
              "quoteCharacter":"quote_character",
              "fieldsTerminatedBy":"fields_terminated_by",
              "linesTerminatedBy":"lines_terminated_by"
          }
      }
   }
   ```

   To send your request, expand one of these options:

   #### curl (Linux, macOS, or Cloud Shell)

   > [!NOTE]
   > **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt) , or by using [Cloud Shell](https://docs.cloud.google.com/shell/docs.md.txt), which automatically logs you into the `gcloud` CLI . You can check the currently active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).


   Save the request body in a file named `request.json`,
   and execute the following command:

   ```
   curl -X POST \
        -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        -H "Content-Type: application/json; charset=utf-8" \
        -d @request.json \
        "https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/export"
   ```

   #### PowerShell (Windows)

   > [!NOTE]
   > **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt) . You can check the currently active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).


   Save the request body in a file named `request.json`,
   and execute the following command:

   ```
   $cred = gcloud auth print-access-token
   $headers = @{ "Authorization" = "Bearer $cred" }

   Invoke-WebRequest `
       -Method POST `
       -Headers $headers `
       -ContentType: "application/json; charset=utf-8" `
       -InFile request.json `
       -Uri "https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/export" | Select-Object -Expand Content
   ```

   You should receive a JSON response similar to the following:

   #### Response

   ```
   {
     "kind": "sql#operation",
     "targetLink": "https://sqladmin.googleapis.com/v1/projects/project-id/instances/target-instance-id",
     "status": "PENDING",
     "user": "user@example.com",
     "insertTime": "2020-01-21T22:43:37.981Z",
     "operationType": "UPDATE",
     "name": "operation-id",
     "targetId": "instance-id",
     "selfLink": "https://sqladmin.googleapis.com/v1/projects/project-id/operations/operation-id",
     "targetProject": "project-id"
   }
   ```


   You must specify exactly one database with the `databases`
   property, and if the select query specifies a database, it must be the
   same.
4. If you do not need to retain the IAM permissions you set previously, remove them now.

For the complete list of parameters for the request, see the [instances:export](https://docs.cloud.google.com/sql/docs/postgres/admin-api/rest/v1beta4/instances/export.md.txt) page.

### REST v1beta4

1. Create a bucket for the export:

   ```bash
   gcloud storage buckets create gs://BUCKET_NAME --location=LOCATION_NAME --project=PROJECT_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 the `storage.objectAdmin` [IAM role](https://docs.cloud.google.com/storage/docs/access-control/iam-roles.md.txt) for your bucket. For help with setting IAM permissions, see [Using
   IAM permissions](https://docs.cloud.google.com/storage/docs/access-control/using-iam-permissions).
3.
   Export your database:


   Before using any of the request data,
   make the following replacements:
   - <var translate="no">project-id</var>: The project ID
   - <var translate="no">instance-id</var>: The instance ID
   - <var translate="no">bucket_name</var>: The Cloud Storage bucket name
   - <var translate="no">path_to_csv_file</var>: The path to the CSV file
   - <var translate="no">database_name</var>: The name of a database inside the Cloud SQL instance
   - <var translate="no">offload</var>: Enables serverless export. Set to `true` to use serverless export.

     > [!NOTE]
     > **Note:** Serverless export costs extra. See the [pricing page](https://cloud.google.com/sql/pricing#storage-networking-prices).

   - <var translate="no">select_query</var>: SQL query for export (optional)

     > [!NOTE]
     > **Note:** While in-transit, the query might be processed in intermediate locations other than the location of the target instance.

   - <var translate="no">escape_character</var>: The character that should appear before a data character that needs to be escaped. The value of this argument must be in [ASCII hex format](https://ascii.cl/). For example, "22" represents double quotes. (optional)
   - <var translate="no">quote_character</var>: The character that encloses values from columns that have a string data type. The value of this argument must be in [ASCII hex format](https://ascii.cl/). For example, "22" represents double quotes. (optional)
   - <var translate="no">fields_terminated_by</var>: The character that splits column values. The value of this argument must be in [ASCII hex format](https://ascii.cl/). For example, "2C" represents a comma. (optional)
   - <var translate="no">lines_terminated_by</var>: The character that split line records. The value of this argument must be in [ASCII hex format](https://ascii.cl/). For example, "0A" represents a new line. (optional)


   HTTP method and URL:

   ```
   POST https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/export
   ```


   Request JSON body:

   ```
   {
    "exportContext":
      {
         "fileType": "CSV",
         "uri": "gs://bucket_name/path_to_csv_file",
         "databases": "database_name",
         "offload": true | false
         "csvExportOptions":
          {
              "selectQuery": "select_query",
              "escapeCharacter":  "escape_character",
              "quoteCharacter": "quote_character",
              "fieldsTerminatedBy": "fields_terminated_by",
              "linesTerminatedBy": "lines_terminated_by"
          }
      }
   }
   ```

   To send your request, expand one of these options:

   #### curl (Linux, macOS, or Cloud Shell)

   > [!NOTE]
   > **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt) , or by using [Cloud Shell](https://docs.cloud.google.com/shell/docs.md.txt), which automatically logs you into the `gcloud` CLI . You can check the currently active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).


   Save the request body in a file named `request.json`,
   and execute the following command:

   ```
   curl -X POST \
        -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        -H "Content-Type: application/json; charset=utf-8" \
        -d @request.json \
        "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/export"
   ```

   #### PowerShell (Windows)

   > [!NOTE]
   > **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt) . You can check the currently active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).


   Save the request body in a file named `request.json`,
   and execute the following command:

   ```
   $cred = gcloud auth print-access-token
   $headers = @{ "Authorization" = "Bearer $cred" }

   Invoke-WebRequest `
       -Method POST `
       -Headers $headers `
       -ContentType: "application/json; charset=utf-8" `
       -InFile request.json `
       -Uri "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/export" | Select-Object -Expand Content
   ```

   You should receive a JSON response similar to the following:

   #### Response

   ```
   {
     "kind": "sql#operation",
     "targetLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/target-instance-id",
     "status": "PENDING",
     "user": "user@example.com",
     "insertTime": "2020-01-21T22:43:37.981Z",
     "operationType": "UPDATE",
     "name": "operation-id",
     "targetId": "instance-id",
     "selfLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/operations/operation-id",
     "targetProject": "project-id"
   }
   ```


   You must specify exactly one database with the `databases`
   property, and if the select query specifies a database, it must be the
   same.
4. If you do not need to retain the IAM role you set previously, [revoke](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt#revoking-console) it now.

For the complete list of parameters for the request, see the [instances:export](https://docs.cloud.google.com/sql/docs/postgres/admin-api/rest/v1beta4/instances/export.md.txt) page.

### Customize the format of a CSV export file

You can use `gcloud` or the REST API to customize your CSV file format. When you
perform an export, you can specify the following formatting options:

| CSV option | Default value | gcloud flag | REST API property | Description |
|---|---|---|---|---|
| Escape | `"5C"` ASCII hex code for file separator. | `--escape` | `escapeCharacter` | Character that appears before a data character that needs to be escaped. Available only for MySQL and PostgreSQL. |
| Quote | `"22"` ASCII hex code for double quotes. | `--quote` | `quoteCharacter` | Character that encloses values from columns that have a string data type. Available only for MySQL and PostgreSQL. |
| Field delimiter | `"2C"` ASCII hex code for comma. | `--fields-terminated-by` | `fieldsTerminatedBy` | Character that splits column values. Available only for MySQL and PostgreSQL. |
| Newline character | `"0A"` ASCII hex code for newline. | `--lines-terminated-by` | `linesTerminatedBy` | Character that splits line records. Available only for MySQL. |

For example, a `gcloud` command using all of these arguments could be
like the following:

```bash
gcloud sql export csv INSTANCE_NAME gs://BUCKET_NAME/FILE_NAME \
--database=DATABASE_NAME \
--offload \
--query=SELECT_QUERY \
--quote="22" \
--escape="5C" \
--fields-terminated-by="2C" \
--lines-terminated-by="0A"
```

The equivalent REST API request body would look like this:

```bash
{
 "exportContext":
   {
      "fileType": "CSV",
      "uri": "gs://bucket_name/path_to_csv_file",
      "databases": ["DATABASE_NAME"],
      "offload": true,
      "csvExportOptions":
       {
           "selectQuery": "SELECT_QUERY",
           "escapeCharacter":  "5C",
           "quoteCharacter": "22",
           "fieldsTerminatedBy": "2C",
           "linesTerminatedBy": "0A"
       }
   }
}
```

CSV export creates standard CSV output by default. If you need even more options
than Cloud SQL provides, you can use the following statement in a psql
client:

```bash
      \copy [table_name] TO '[csv_file_name].csv' WITH
          (FORMAT csv, ESCAPE '[escape_character]', QUOTE '[quote_character]',
          DELIMITER '[delimiter_character]', ENCODING 'UTF8', NULL '[null_marker_string]');
```

## Import data to Cloud SQL for PostgreSQL

### Required roles and permissions for importing to Cloud SQL for PostgreSQL


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

- The [Cloud SQL Admin](https://docs.cloud.google.com/sql/docs/postgres/iam-roles.md.txt) role
- A [custom role](https://docs.cloud.google.com/iam/docs/understanding-custom-roles), including the following permissions:
  - `cloudsql.instances.get`
  - `cloudsql.instances.import`

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


- The `storage.objectAdmin` IAM role
- A custom role, including the following permissions:
  - `storage.objects.get`
  - `storage.objects.list` (for importing files in parallel only)

For help with
IAM roles, see
[Identity and Access Management](https://docs.cloud.google.com/storage/docs/access-control/iam.md.txt).

> [!NOTE]
> **Note** : The changes that you make to the IAM permissions and roles might take a few minutes to take effect. For more information, see [Access change propagation](https://docs.cloud.google.com/iam/docs/access-change-propagation.md.txt).

<br />

### Import data from a CSV file to Cloud SQL for PostgreSQL

- The database and table you are importing into must exist on your Cloud SQL instance. For help with creating a database, see [Creating a database](https://docs.cloud.google.com/sql/docs/postgres/create-manage-databases.md.txt#create).
- Your CSV file must conform to the [CSV
  file format requirements](https://docs.cloud.google.com/sql/docs/postgres/import-export/import-export-csv#csv-reqs).

> [!NOTE]
> **Note:** The default behavior for Cloud SQL is to import columns in your CSV file in the same order as the table schema. If the order is different in your CSV file or if some columns are skipped, use [importContext.csvImportOptions.columns\[\]](https://docs.cloud.google.com/sql/docs/postgres/admin-api/rest/v1/operations.md.txt#importcontext) to import data from the CSV file.

> [!NOTE]
> **Note:** You cannot import a CSV file that was created using one database engine into an instance created using another database engine.

[](https://docs.cloud.google.com/sql/docs/postgres/import-export/import-export-csv.md.txt#)

### CSV file format requirements

<br />

CSV files must have one line for each row of data and use comma-separated
fields.

To import data to a Cloud SQL instance using a CSV file:

### Console

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

   [Go to Cloud SQL Instances](https://console.cloud.google.com/sql)
2. To open the **Overview** page of an instance, click the instance name.
3. Click **Import**.
4. In the **File format** section, select **CSV**.
5. In the **Select source file** section, enter the path to the bucket and CSV file or upload a local CSV file to use for the import. To browse to the file:
   1. Click **Browse**.
   2. In the **Location** section, double-click the name of the bucket in the list.
   3. Select the file in the list.
   4. Click **Select**.

   You can import a compressed (`.gz`) or an uncompressed
   (`.csv`) file.
6. In the **Destination** section, specify the **Database** and **Table** in your Cloud SQL instance where you want to import the CSV file.
7. You can optionally specify a user for the import operation.
8. Click **Import** to start the import.

### gcloud

1. [Create a Cloud Storage bucket.](https://docs.cloud.google.com/storage/docs/creating-buckets.md.txt)
2. Upload the file to your bucket.

   For help with uploading files to buckets, see
   [Uploading objects](https://docs.cloud.google.com/storage/docs/uploading-objects.md.txt).
3. Upload data from the CSV file to the bucket.
4. Identify the service account for the Cloud SQL instance you're exporting from. You can do this running the [`gcloud sql instances describe`](https://docs.cloud.google.com/sdk/gcloud/reference/sql/instances/describe) command with the instance name. Look for the `serviceAccountEmailAddress` field in the output.

   ```bash
   gcloud sql instances describe INSTANCE_NAME
   ```
5. Copy the serviceAccountEmailAddress field.
6. Use `
   gcloud storage buckets add-iam-policy-binding` to grant the `storage.objectAdmin` [IAM role](https://docs.cloud.google.com/storage/docs/access-control/iam-roles.md.txt) to the Cloud SQL instance service account for the bucket. For help with setting IAM permissions, see [Using IAM permissions](https://docs.cloud.google.com/storage/docs/access-control/using-iam-permissions.md.txt).
7. Import the file:

   ```bash
   gcloud sql import csv INSTANCE_NAME gs://BUCKET_NAME/FILE_NAME \
   --database=DATABASE_NAME \
   --table=TABLE_NAME
   ```

   For information about using the `import csv` command, see the
   [`sql import csv`](https://docs.cloud.google.com/sdk/gcloud/reference/sql/import/csv)
   command reference page.
8. If you do not need to retain the IAM permissions you set previously, remove them using [`gcloud storage buckets remove-iam-policy-binding`](https://docs.cloud.google.com/sdk/gcloud/reference/storage/buckets/remove-iam-policy-binding).

### REST v1

1. [Create a Cloud Storage bucket.](https://docs.cloud.google.com/storage/docs/creating-buckets.md.txt)
2. Upload the file to your bucket.

   For help with uploading files to buckets, see
   [Uploading objects](https://docs.cloud.google.com/storage/docs/uploading-objects.md.txt).
3. Provide your instance with the `storage.objectAdmin` and `objectViewer` [IAM roles](https://docs.cloud.google.com/storage/docs/access-control/iam-roles.md.txt) for your bucket. For help with setting IAM permissions, see [Using IAM permissions](https://docs.cloud.google.com/storage/docs/access-control/using-iam-permissions.md.txt).
4. Import the file:
   Before using any of the request data,
   make the following replacements:

   - <var translate="no">project-id</var>: The project ID
   - <var translate="no">instance-id</var>: The instance ID
   - <var translate="no">bucket_name</var>: The Cloud Storage bucket name
   - <var translate="no">path_to_csv_file</var>: The path to the CSV file
   - <var translate="no">database_name</var>: The name of a database inside the Cloud SQL instance
   - <var translate="no">table_name</var>: The name of the database table
   - <var translate="no">escape_character</var>: The character that should appear before a data character that needs to be escaped. The value of this argument has to be a character in Hex ASCII Code. For example, "22" represents double quotes. (optional)
   - <var translate="no">quote_character</var>: The character that encloses values from columns that have a string data type. The value of this argument has to be a character in Hex ASCII Code. For example, "22" represents double quotes. (optional)
   - <var translate="no">fields_terminated_by</var>: The character that split column values. The value of this argument has to be a character in Hex ASCII Code. For example, "2C" represents a comma. (optional)
   - <var translate="no">lines_terminated_by</var>: The character that split line records. The value of this argument has to be a character in Hex ASCII Code. For example, "0A" represents a new line. (optional)


   HTTP method and URL:

   ```
   POST https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/import
   ```


   Request JSON body:

   ```
   {
    "importContext":
      {
         "fileType": "CSV",
         "uri": "gs://bucket_name/path_to_csv_file",
         "database": "database_name",
         "csvImportOptions":
          {
            "table": "table_name",
            "escapeCharacter": "escape_character",
            "quoteCharacter": "quote_character",
            "fieldsTerminatedBy": "fields_terminated_by",
            "linesTerminatedBy": "lines_terminated_by"
          }
      }
   }

   ```

   To send your request, expand one of these options:

   #### curl (Linux, macOS, or Cloud Shell)

   > [!NOTE]
   > **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt) , or by using [Cloud Shell](https://docs.cloud.google.com/shell/docs.md.txt), which automatically logs you into the `gcloud` CLI . You can check the currently active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).


   Save the request body in a file named `request.json`,
   and execute the following command:

   ```
   curl -X POST \
        -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        -H "Content-Type: application/json; charset=utf-8" \
        -d @request.json \
        "https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/import"
   ```

   #### PowerShell (Windows)

   > [!NOTE]
   > **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt) . You can check the currently active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).


   Save the request body in a file named `request.json`,
   and execute the following command:

   ```
   $cred = gcloud auth print-access-token
   $headers = @{ "Authorization" = "Bearer $cred" }

   Invoke-WebRequest `
       -Method POST `
       -Headers $headers `
       -ContentType: "application/json; charset=utf-8" `
       -InFile request.json `
       -Uri "https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/import" | Select-Object -Expand Content
   ```

   You should receive a JSON response similar to the following:

   #### Response

   ```
   {
     "kind": "sql#operation",
     "targetLink": "https://sqladmin.googleapis.com/v1/projects/project-id/instances/target-instance-id",
     "status": "PENDING",
     "user": "user@example.com",
     "insertTime": "2020-01-21T22:43:37.981Z",
     "operationType": "UPDATE",
     "name": "operation-id",
     "targetId": "instance-id",
     "selfLink": "https://sqladmin.googleapis.com/v1/projects/project-id/operations/operation-id",
     "targetProject": "project-id"
   }
   ```
   For the complete list of parameters for the request, see the [instances:import](https://docs.cloud.google.com/sql/docs/postgres/admin-api/rest/v1beta4/instances/import.md.txt) page.
5. If you do not need to retain the IAM permissions you set previously, remove the permissions.

### REST v1beta4

1. [Create a Cloud Storage bucket.](https://docs.cloud.google.com/storage/docs/creating-buckets.md.txt)
2. Upload the file to your bucket.

   For help with uploading files to buckets, see
   [Uploading objects](https://docs.cloud.google.com/storage/docs/uploading-objects.md.txt).
3. Provide your instance with the `storage.objectAdmin` [IAM role](https://docs.cloud.google.com/storage/docs/access-control/iam-roles.md.txt) for your bucket. For help with setting IAM permissions, see [Using IAM permissions](https://docs.cloud.google.com/storage/docs/access-control/using-iam-permissions.md.txt).
4. Import the file:
   Before using any of the request data,
   make the following replacements:

   - <var translate="no">project-id</var>: The project ID
   - <var translate="no">instance-id</var>: The instance ID
   - <var translate="no">bucket_name</var>: The Cloud Storage bucket name
   - <var translate="no">path_to_csv_file</var>: The path to the CSV file
   - <var translate="no">database_name</var>: The name of a database inside the Cloud SQL instance
   - <var translate="no">table_name</var>: The name of the database table
   - <var translate="no">escape_character</var>: The character that should appear before a data character that needs to be escaped. The value of this argument must be in [ASCII hex format](https://ascii.cl/). For example,"22" represents double quotes. (optional)
   - <var translate="no">quote_character</var>: The character that encloses values from columns that have a string data type. The value of this argument must be in [ASCII hex format](https://ascii.cl/). For example, "22" represents double quotes. (optional)
   - <var translate="no">fields_terminated_by</var>: The character that split column values. The value of this argument must be in [ASCII hex format](https://ascii.cl/). For example, "2C" represents a comma. (optional)
   - <var translate="no">lines_terminated_by</var>: The character that split line records. The value of this argument must be in [ASCII hex format](https://ascii.cl/). For example, "0A" represents a new line. (optional)


   HTTP method and URL:

   ```
   POST https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/import
   ```


   Request JSON body:

   ```
   {
    "importContext":
      {
         "fileType": "CSV",
         "uri": "gs://bucket_name/path_to_csv_file",
         "database": "database_name",
         "csvImportOptions":
          {
            "table": "table_name",
            "escapeCharacter": "escape_character",
            "quoteCharacter": "quote_character",
            "fieldsTerminatedBy": "fields_terminated_by",
            "linesTerminatedBy": "lines_terminated_by"
          }
      }
   }

   ```

   To send your request, expand one of these options:

   #### curl (Linux, macOS, or Cloud Shell)

   > [!NOTE]
   > **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt) , or by using [Cloud Shell](https://docs.cloud.google.com/shell/docs.md.txt), which automatically logs you into the `gcloud` CLI . You can check the currently active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).


   Save the request body in a file named `request.json`,
   and execute the following command:

   ```
   curl -X POST \
        -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        -H "Content-Type: application/json; charset=utf-8" \
        -d @request.json \
        "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/import"
   ```

   #### PowerShell (Windows)

   > [!NOTE]
   > **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt) . You can check the currently active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).


   Save the request body in a file named `request.json`,
   and execute the following command:

   ```
   $cred = gcloud auth print-access-token
   $headers = @{ "Authorization" = "Bearer $cred" }

   Invoke-WebRequest `
       -Method POST `
       -Headers $headers `
       -ContentType: "application/json; charset=utf-8" `
       -InFile request.json `
       -Uri "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/import" | Select-Object -Expand Content
   ```

   You should receive a JSON response similar to the following:

   #### Response

   ```
   {
     "kind": "sql#operation",
     "targetLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/target-instance-id",
     "status": "PENDING",
     "user": "user@example.com",
     "insertTime": "2020-01-21T22:43:37.981Z",
     "operationType": "UPDATE",
     "name": "operation-id",
     "targetId": "instance-id",
     "selfLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/operations/operation-id",
     "targetProject": "project-id"
   }
   ```
   For the complete list of parameters for the request, see the [instances:import](https://docs.cloud.google.com/sql/docs/postgres/admin-api/rest/v1beta4/instances/import.md.txt) page.
5. If you do not need to retain the IAM permissions you set previously, remove the permissions.

### Customize the format of a CSV file for Cloud SQL for PostgreSQL

You can use `gcloud` or the REST API to customize your CSV file format.

A sample `gcloud` command follows:

```bash
gcloud sql import csv INSTANCE_NAME gs://BUCKET_NAME/FILE_NAME \
--database=DATABASE_NAME \
--table=TABLE_NAME \
--quote="22" \
--escape="5C" \
--fields-terminated-by="2C" \
--lines-terminated-by="0A"
```

The equivalent REST API request body would look like this:

```bash
{
 "importContext":
   {
      "fileType": "CSV",
      "uri": "gs://bucket_name/path_to_csv_file",
      "database": ["DATABASE_NAME"],
      "csvImportOptions":
       {
           "table": "TABLE_NAME",
           "escapeCharacter":  "5C",
           "quoteCharacter": "22",
           "fieldsTerminatedBy": "2C",
           "linesTerminatedBy": "0A"
       }
   }
}
```

> [!NOTE]
> **Note:**If you use custom format options in your import commands, make sure the exported file was created with the same options.

If you get an error such as `ERROR_RDBMS`, ensure the table
exists. If the table exists, confirm that you have the correct
permissions on the bucket. For help configuring access control in
Cloud Storage, see [Create and Manage Access Control Lists](https://docs.cloud.google.com/storage/docs/access-control/create-manage-lists.md.txt).
To see how the [underlying REST API request](https://docs.cloud.google.com/sql/docs/postgres/admin-api/rest/v1beta4/instances/import.md.txt) is constructed for this task, see the [APIs Explorer on the instances:import page](https://docs.cloud.google.com/sql/docs/postgres/admin-api/rest/v1beta4/instances/import.md.txt).

## What's next

- Learn how to [check the status of import and export operations](https://docs.cloud.google.com/sql/docs/postgres/import-export/checking-status-import-export.md.txt).
- Learn more about [best practices for importing and exporting data](https://docs.cloud.google.com/sql/docs/postgres/import-export.md.txt).
- [Known issues for imports and exports](https://docs.cloud.google.com/sql/docs/postgres/known-issues.md.txt#import-export).