Import a SQL file

This page describes how to import a SQL file into an AlloyDB database.

The procedure to perform the import involves these tasks:

  1. Upload the SQL file to a Cloud Storage bucket.

  2. Prepare a client host to perform the import operation.

  3. Import the SQL file into the database.

  4. Clean up the resources created to perform the procedure.

Before you begin

  • You must have the Owner (roles/owner) or Editor (roles/editor) basic IAM role in the Google Cloud project you are using, or you must have these predefined IAM roles:
    • AlloyDB Admin (roles/alloydb.admin) or AlloyDB Viewer (roles/alloydb.viewer)
    • Storage Admin (roles/storage.admin)
    • Compute Instance Admin (v1) (roles/compute.instanceAdmin.v1)

Upload the SQL file

To upload the SQL file, you create a Cloud Storage bucket and then upload the SQL file to that bucket.

  1. Create a standard storage, regional storage bucket in the project and region where your AlloyDB database is located. When following these instructions, make sure to allocate enough local storage to the Compute Engine VM to accommodate the SQL file you are importing.

  2. Upload the SQL file to the storage bucket you created.

Prepare a client host

To prepare a client host to perform the import operation, you create a Compute Engine VM that can connect to the AlloyDB primary instance where your database is located, and install the psql tool and the Google Cloud CLI on that VM.

  1. Follow the instructions Connect a psql client to an instance to create a Compute Engine VM with the proper connectivity and the psql tool installed. When following these instructions, make sure to allocate enough local storage to the Compute Engine VM to accommodate the SQL file you are importing.

  2. Install the gcloud CLI to provide command-line access to the SQL file in the Cloud Storage bucket.

Import the SQL file

To import the SQL file, you get the IP address of the AlloyDB primary instance where your database is located and then use the psql tool to import the file into the database.

  1. Get the IP address of the AlloyDB primary instance where your database is located by viewing its details.
  2. SSH into the Compute Engine VM.

    Console

    1. In the Google Cloud console, go to the VM instances page.

      Go to VM instances

    2. In the list of virtual machine instances, click SSH in the row of the instance you created.

      SSH button next to instance name.

    gcloud

    Use the gcloud compute ssh command to connect to the instance you created.

    gcloud compute ssh --project=PROJECT_ID --zone=ZONE VM_NAME

    Replace the following:

    • PROJECT_ID: The ID of the project that contains the instance.
    • ZONE: The name of the zone in which the instance is located.
    • VM_NAME: The name of the instance.
  3. Copy the SQL file to the client host's local file system:
    gcloud storage cp gs://BUCKET_NAME/SQL_FILE_NAME .
    
  4. Import the SQL file:
    psql -h IP_ADDRESS -U postgres DB_NAME < SQL_FILE_NAME
    
    • IP_ADDRESS: The IP address of the primary instance.
    • DB_NAME: The name of the database to import into.
    • SQL_FILE_NAME: The SQL file you uploaded.

Clean up resources

After successfully importing the SQL file, you can delete the Cloud Storage bucket and delete the Compute Engine VM you used during the import procedure.