To load tabular data into an existing database table, you can import a CSV file
stored in a Cloud Storage bucket into an AlloyDB for PostgreSQL cluster.

> [!NOTE]
> **Note:** If you're migrating an entire database from a supported database server (on-premises, in AWS, or AlloyDB) to a new AlloyDB cluster, you can use [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 cluster from the exported file, consider [restoring from a stored backup](https://docs.cloud.google.com/alloydb/docs/backup/restore.md.txt).

You can cancel the import of data into AlloyDB clusters. For more
information, see [Cancel an import operation](https://docs.cloud.google.com/alloydb/docs/cancel-import.md.txt).

## Before you begin

Before you begin an import operation:

- Make sure that your database has adequate free space.
- Import operations use database resources, but they don't interfere with normal database operations unless the cluster is under-provisioned.

> [!IMPORTANT]
> **Important:** Before starting a large import operation, make sure that at least 25 percent of the disk is free on the cluster. Doing so helps to prevent issues with aggressive autogrowth, which can adversely affect the availability of the cluster. Verify that the CSV file has the expected data and that it's a valid CSV file.

## Required roles and permissions for importing to AlloyDB clusters

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

- The [AlloyDB Admin](https://docs.cloud.google.com/alloydb/docs/reference/iam-roles-permissions.md.txt) role
- A [custom role](https://docs.cloud.google.com/iam/docs/understanding-custom-roles), including the following permissions:
  - `alloydb.clusters.get`
  - `alloydb.clusters.import`

Additionally, the service account for the AlloyDB cluster must
have one of the following roles:

- The `storage.objectViewer` IAM role
- A custom role, including the following permissions:
  - `storage.objects.get`

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).

## Import data from a CSV file to AlloyDB clusters

- The database and table you are importing into must exist on your AlloyDB cluster. For help with creating a database, see [Create a database](https://docs.cloud.google.com/alloydb/docs/database-create.md.txt).
- CSV files must have one line for each row of data and use comma-separated fields.
- CSV files must not contain a header row. If your CSV file has a header row, remove it before importing.

> [!NOTE]
> **Note:** The default behavior for AlloyDB 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 [CSV Import options](https://docs.cloud.google.com/sdk/gcloud/reference/alloydb/clusters/import.md.txt) to import from the CSV file to required columns.

To import data to an AlloyDB cluster using a CSV file, follow
these steps:

### Console

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

   [Go to Clusters](https://console.cloud.google.com/alloydb/clusters)
2. Click a cluster name to open the **Overview** page for that cluster.

3. Click **Import**.

4. In **File format** , select **CSV**.

5. In **Select source file**, choose whether to upload files from your computer or select a file from a Cloud Storage location.

   1. To upload a file from your computer, do the following:

      1. Select **Upload files from your computer**.
      2. Click **Browse** next to **Select local file** to select a CSV file from your local machine.
      3. Click **Browse** next to **Select Cloud Storage location** to select a Cloud Storage location to store the uploaded local file.
      4. Click **Upload** to upload the file.
   2. To select a CSV file that exists in a Cloud Storage bucket, do the following:

      1. Select **Select file from Google Cloud Storage**.
      2. Click **Browse** next to **bucket-name/file-name** to select a CSV file from a Cloud Storage bucket.
6. Select the database you want the data to be imported into from the **Database** drop-down menu.

7. Select the table within the database from the **Table** drop-down menu.

8. Optional: To specify a user for the import operation, click **Show user options** and enter the username in the **User** field.

9. To start the import operation, click **Import**.

> [!NOTE]
> **Note:** You can import a compressed `.gz` or uncompressed `.csv` file.

### gcloud

1. [Create a Cloud Storage bucket](https://docs.cloud.google.com/storage/docs/creating-buckets.md.txt).
2. Upload the CSV 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. Use
   [`gcloud storage buckets add-iam-policy-binding`](https://docs.cloud.google.com/sdk/gcloud/reference/storage/buckets/add-iam-policy-binding)
   to grant the `storage.objectViewer`
   [IAM role](https://docs.cloud.google.com/storage/docs/access-control/iam-roles.md.txt)
   to the AlloyDB service account for the bucket:

   ```bash
    service-PROJECT_NUMBER@gcp-sa-alloydb.iam.gserviceaccount.com
   ```

   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:

   ```bash
   gcloud alloydb clusters import CLUSTER_NAME \
     --region=REGION \
     --gcs-uri=gs://BUCKET_NAME/FILE_NAME \
     --database=DATABASE_NAME \
     --user=USER \
     --csv \
     --table=TABLE_NAME
   ```

   > [!NOTE]
   > **Note:** You can import a compressed (`.gz`) or an uncompressed (`.csv`) file.

   Replace the following:
   - <var translate="no">CLUSTER_NAME</var>: the name of the cluster.
   - <var translate="no">REGION</var>: the region in which the AlloyDB cluster is deployed.
   - <var translate="no">BUCKET_NAME</var>: the Cloud Storage bucket name.
   - <var translate="no">FILE_NAME</var>: the name of the CSV file.
   - <var translate="no">DATABASE_NAME</var>: the name of a database inside the cluster.
   - <var translate="no">USER</var>: the user for the import operation.
   - <var translate="no">TABLE_NAME</var>: the table inside the database.

   For more information about using the `import` command, see the
   [`gcloud alloydb clusters import`](https://docs.cloud.google.com/sdk/gcloud/reference/alloydb/clusters/import.md.txt) command reference page.
5. If you don't need to keep the IAM permissions that 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 CSV 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. Use [`gcloud storage buckets add-iam-policy-binding`](https://docs.cloud.google.com/sdk/gcloud/reference/storage/buckets/add-iam-policy-binding)
   to grant the `storage.objectViewer`
   [IAM role](https://docs.cloud.google.com/storage/docs/access-control/iam-roles.md.txt)
   to the AlloyDB cluster service account for the bucket. The
   format for the service account is as follows:

   ```bash
   service-PROJECT_NUMBER@gcp-sa-alloydb.iam.gserviceaccount.com
   ```

   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.

   > [!NOTE]
   > **Note:** You can import a compressed (`.gz`) or an uncompressed (`.csv`) file.

   Use the following HTTP method and URL:

   ```bash
   POST https://alloydb.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/clusters/CLUSTER_ID:import
   ```

   Before you use any of the request data, make the following replacements:
   - <var translate="no">PROJECT_ID</var>: the project ID.
   - <var translate="no">LOCATION_ID</var>: the region in which the AlloyDB cluster is deployed.
   - <var translate="no">CLUSTER_ID</var>: the cluster 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">USER</var>: the user for the import operation.
   - <var translate="no">DATABASE_NAME</var>: the name of a database inside the AlloyDB cluster.
   - <var translate="no">TABLE_NAME</var>: the table inside the database.
   - <var translate="no">COLUMNS</var> (Optional): the columns to import.
   - <var translate="no">ESCAPE_CHARACTER</var> (Optional): the character that must appear before a data character that needs to be escaped. The value of this argument must be a character in Hex ASCII Code. For example, `22` represents a double quotation mark.
   - <var translate="no">QUOTE_CHARACTER</var> (Optional): the character that encloses values from columns that have a string data type. The value of this argument must be a character in Hex ASCII Code. For example, `22` represents a double quotation mark.
   - <var translate="no">FIELD_DELIMITER</var> (Optional): the character that splits column values. The value of this argument must be a character in Hex ASCII Code. For example, `2C` represents a comma.

   Request JSON body:

   ```json
   {
     "gcsUri": "gs://BUCKET_NAME/PATH_TO_CSV_FILE",
     "database": "DATABASE_NAME",
     "user": "USER",
     "csvImportOptions": {
       "table": "TABLE_NAME",
       "columns": ["COLUMN1", "COLUMN2"],
       "fieldDelimiter": "FIELD_DELIMITER",
       "quoteCharacter": "QUOTE_CHARACTER",
       "escapeCharacter": "ESCAPE_CHARACTER"
     }
   }
   ```

   To send your request, expand one of these options:

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

   The following command assumes that you signed into the Google Cloud 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 signs you into the gcloud CLI. You can check the 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://alloydb.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/clusters/CLUSTER_ID:import"
   ```

   #### PowerShell (Windows)

   The following command assumes that you signed into the Google Cloud 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 signs you into the gcloud CLI. You can check the 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://alloydb.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/clusters/CLUSTER_ID:import"| Select-Object -Expand Content
   ```

   You receive a JSON response similar to the following:

   #### Response

   ```
   {
    "name": "projects/PROJECT_ID/locations/LOCATION_ID/operations/OPERATION_ID",
    "metadata": {
     "@type": "type.googleapis.com/google.cloud.alloydb.v1.OperationMetadata",
     "createTime": "2024-09-17T06:05:31.244428646Z",
     "target": "projects/PROJECT_ID/locations/LOCATION_ID/clusters/CLUSTER_ID",
     "verb": "import",
     "requestedCancellation": false,
     "apiVersion": "v1"
    },
    "done": false
   }
   ```
5. If you don't need to retain the IAM permissions that you set
   previously, remove them.

   For the complete list of parameters for the request, see [`clusters.import`](https://docs.cloud.google.com/alloydb/docs/reference/rest/v1/projects.locations.clusters/import.md.txt).

## Customize CSV format and import the file

You can customize the CSV format and import the file when the CSV file
contains non-default field delimiter, or quote or escape characters. To import
a CSV file with a non-default format, you must provide the same formatting
options in the request.

Use the following gcloud CLI and REST API examples to help you
customize your CSV file format.

### gcloud

```bash
gcloud alloydb clusters import CLUSTER_NAME \
  --region=REGION \
  --database=DATABASE_NAME \
  --gcs-uri='gs://BUCKET_NAME/PATH_TO_CSV_FILE' \
  --user=USERNAME \
  --table=TABLE_NAME \
  --columns=COLUMNS \
  --field-delimiter='2C' \
  --quote-character='22' \
  --escape-character='5C' \
  --csv
```

### REST v1

The equivalent REST API request body looks similar to the following:

```json
{
 "gcsUri": "gs://BUCKET_NAME/PATH_TO_CSV_FILE",
 "database": "DATABASE_NAME",
 "user": "USER",
 "csvImportOptions": {
     "table": "TABLE_NAME",
     "columns": ["COLUMN1", "COLUMN2"],
     "escapeCharacter": "5C",
     "quoteCharacter": "22",
     "fieldDelimiter": "2C"
 }
}
```

To see how the [underlying REST API request](https://docs.cloud.google.com/alloydb/docs/reference/rest/v1/projects.locations.clusters/import.md.txt#body.HTTP_TEMPLATE) is constructed for this task, see the [APIs Explorer on the `clusters.import` page](https://docs.cloud.google.com/alloydb/docs/reference/rest/v1/projects.locations.clusters/import.md.txt).

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

If you get an error like `INVALID_ARGUMENT`, make sure that 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).

## Check the status of an import operation

To check the status of an import operation, follow these steps:

### gcloud

Run the following using the [gcloud alloydb operations describe](https://docs.cloud.google.com/sdk/gcloud/reference/alloydb/operations/describe.md.txt)
command:

```bash
gcloud alloydb operations describe OPERATION_ID --region=REGION
```

The status of these operations is in the `STATUS` field.

You can also list details of a specific operation or cancel a specific
operation. For more information about this command, see the
[`gcloud alloydb operations`](https://docs.cloud.google.com/sdk/gcloud/reference/alloydb/operations.md.txt)
command reference page.

### REST v1

Use the `GET` method and the following URL:

```
GET https://alloydb.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/operations/OPERATION_ID
```

For more information, see [`get`](https://docs.cloud.google.com/alloydb/docs/reference/rest/v1/projects.locations.operations/get.md.txt).

Before you use any of the request data, make the following replacements:

- <var translate="no">REGION</var>: the region in which the AlloyDB cluster is deployed.
- <var translate="no">PROJECT_ID</var>: the project ID.
- <var translate="no">OPERATION_ID</var>: the ID of the import operation. For more information, see [Before you begin](https://docs.cloud.google.com/alloydb/docs/import-csv-file.md.txt#before-you-begin).

To send your request, expand one of these options:

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

The following command assumes that you signed into 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 signs you into the gcloud CLI. You can check the active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).

Execute the following command:

```
  curl -X GET \
       -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  https://alloydb.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/operations/OPERATION_ID
 
```

#### *PowerShell (Windows)*

The following command assumes that you signed into 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 active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).

Execute the following command:

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

Invoke-WebRequest `
  -Method GET `
  -Headers $headers `
  -Uri "https://alloydb.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/operations/OPERATION_ID" | Select-Object -Expand Content
```

<br />


If successful, the response body contains an instance of
[`Operation`](https://docs.cloud.google.com/alloydb/docs/reference/rest/Shared.Types/ListOperationsResponse.md.txt#Operation).

<br />

## What's next

- Learn how to [import a SQL file](https://docs.cloud.google.com/alloydb/docs/import-sql-file.md.txt).
- Learn how to [cancel an import operation](https://docs.cloud.google.com/alloydb/docs/cancel-import.md.txt).