google_sql_database_instance

Creates a new Google SQL Database Instance. For more information, see the official documentation, or the JSON API.

Example Usage

SQL Second Generation Instance

resource "google_sql_database_instance" "main" {
  name             = "main-instance"
  database_version = "POSTGRES_15"
  region           = "us-central1"

  settings {
    # Second-generation instance tiers are based on the machine
    # type. See argument reference below.
    tier = "db-f1-micro"
  }
}

Granular restriction of network access

resource "google_compute_instance" "apps" {
  count        = 8
  name         = "apps-${count.index + 1}"
  machine_type = "f1-micro"

  boot_disk {
    initialize_params {
      image = "ubuntu-os-cloud/ubuntu-1804-lts"
    }
  }

  network_interface {
    network = "default"

    access_config {
      // Ephemeral IP
    }
  }
}

resource "random_id" "db_name_suffix" {
  byte_length = 4
}

locals {
  onprem = ["192.168.1.2", "192.168.2.3"]
}

resource "google_sql_database_instance" "postgres" {
  name             = "postgres-instance-${random_id.db_name_suffix.hex}"
  database_version = "POSTGRES_15"

  settings {
    tier = "db-f1-micro"

    ip_configuration {

      dynamic "authorized_networks" {
        for_each = google_compute_instance.apps
        iterator = apps

        content {
          name  = apps.value.name
          value = apps.value.network_interface.0.access_config.0.nat_ip
        }
      }

      dynamic "authorized_networks" {
        for_each = local.onprem
        iterator = onprem

        content {
          name  = "onprem-${onprem.key}"
          value = onprem.value
        }
      }
    }
  }
}

Private IP Instance

resource "google_compute_network" "private_network" {
  provider = google-beta

  name = "private-network"
}

resource "google_compute_global_address" "private_ip_address" {
  provider = google-beta

  name          = "private-ip-address"
  purpose       = "VPC_PEERING"
  address_type  = "INTERNAL"
  prefix_length = 16
  network       = google_compute_network.private_network.id
}

resource "google_service_networking_connection" "private_vpc_connection" {
  provider = google-beta

  network                 = google_compute_network.private_network.id
  service                 = "servicenetworking.googleapis.com"
  reserved_peering_ranges = [google_compute_global_address.private_ip_address.name]
}

resource "random_id" "db_name_suffix" {
  byte_length = 4
}

resource "google_sql_database_instance" "instance" {
  provider = google-beta

  name             = "private-instance-${random_id.db_name_suffix.hex}"
  region           = "us-central1"
  database_version = "MYSQL_5_7"

  depends_on = [google_service_networking_connection.private_vpc_connection]

  settings {
    tier = "db-f1-micro"
    ip_configuration {
      ipv4_enabled                                  = false
      private_network                               = google_compute_network.private_network.id
      enable_private_path_for_google_cloud_services = true
    }
  }
}

provider "google-beta" {
  region = "us-central1"
  zone   = "us-central1-a"
}

ENTERPRISE_PLUS Instance with data_cache_config

resource "google_sql_database_instance" "main" {
  name             = "enterprise-plus-main-instance"
  database_version = "MYSQL_8_0_31"
  settings {
    tier    = "db-perf-optimized-N-2"
    edition = "ENTERPRISE_PLUS"
    data_cache_config {
        data_cache_enabled = true
    }
  }
}

Cloud SQL Instance with PSC connectivity

resource "google_sql_database_instance" "main" {
  name             = "psc-enabled-main-instance"
  database_version = "MYSQL_8_0"
  settings {
    tier    = "db-f1-micro"
    ip_configuration {
      psc_config {
        psc_enabled = true
        allowed_consumer_projects = ["allowed-consumer-project-name"]
      }
      ipv4_enabled = false
    }
    backup_configuration {
      enabled = true
      binary_log_enabled = true
    }
    availability_type = "REGIONAL"
  }
}

Argument Reference

The following arguments are supported:


The settings block supports:

The optional settings.advanced_machine_features subblock supports:

The optional settings.database_flags sublist supports:

The optional settings.active_directory_config subblock supports:

The optional settings.data_cache_config subblock supports:

The optional settings.deny_maintenance_period subblock supports:

The optional settings.sql_server_audit_config subblock supports:

The optional settings.backup_configuration subblock supports:

The optional settings.backup_configuration.backup_retention_settings subblock supports:

The optional settings.ip_configuration subblock supports:

The optional settings.ip_configuration.authorized_networks[] sublist supports:

The optional settings.ip_configuration.psc_config sublist supports:

The optional settings.location_preference subblock supports:

The optional settings.maintenance_window subblock for instances declares a one-hour maintenance window when an Instance can automatically restart to apply updates. The maintenance window is specified in UTC time. It supports:

The optional settings.insights_config subblock for instances declares Query Insights(MySQL, PostgreSQL) configuration. It contains:

The optional settings.password_validation_policy subblock for instances declares Password Validation Policy configuration. It contains:

The optional replica_configuration block must have master_instance_name set to work, cannot be updated, and supports:

The optional clone block supports:

The optional restore_backup_context block supports: NOTE: Restoring from a backup is an imperative action and not recommended via Terraform. Adding or modifying this block during resource creation/update will trigger the restore action after the resource is created/updated.

Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:

Timeouts

google_sql_database_instance provides the following Timeouts configuration options:

Import

Database instances can be imported using one of any of these accepted formats:

In Terraform v1.5.0 and later, use an import block to import Database instances using one of the formats above. For example:

import {
  id = "projects/{{project}}/instances/{{name}}"
  to = google_sql_database_instance.default
}

When using the terraform import command, Database instances can be imported using one of the formats above. For example:

$ terraform import google_sql_database_instance.default projects/{{project}}/instances/{{name}}
$ terraform import google_sql_database_instance.default {{project}}/{{name}}
$ terraform import google_sql_database_instance.default {{name}}