1

Is there an official way of installing an extension on a GCP Postgress Cloud SQL instance via Terraform?

Closest I've found is this unofficial Postgres resource but it's not immediately clear how to link the two. This issue on their tracker sort of shows how, but far from a step by step guide.

if it makes any difference, I'm trying to provision a Postgres Cloud SQL instance with PostGIS.

Thanks.

2 Answers 2

1

You can use the postgresql_extension resource of the PostgreSQL Provider to manage extensions.

When you used Terraform to set up the DB instance, you can reuse the resource to "link" the two providers.

resource "google_sql_database_instance" "instance" {
  name             = random_id.instance_name.hex
  region           = var.region
  project          = var.project
  database_version = var.database_version
}

resource "google_sql_user" "root" {
  name     = local.root_user
  password = random_password.generated_password.result
  project  = var.project
  instance = google_sql_database_instance.instance.name

  depends_on = [google_sql_database_instance.instance]
}

provider "postgresql" {
  scheme    = "gcppostgres"
  host      = google_sql_database_instance.instance.connection_name
  username  = google_sql_user.root.name
  password  = google_sql_user.root.password
  superuser = false
}

resource "postgresql_extension" "pgcrypto_extension" {
  name = "pgcrypto"
  depends_on = [google_sql_database_instance.instance, google_sql_user.root]
}
Sign up to request clarification or add additional context in comments.

Comments

-1

Terraform is a deployment tool, to create all your infrastructure. To install an extension on Postgres, you need a installation tool, because you need to connect to the database and to run a command.

It's the same thing if you want to create a user in the database and you want to grant some privileges on it.

In summary, you can't achieve that with Terraform. I recommend you to have an installation tool, such as Ansible to perform this action.

An alternative is to create, with Terraform, a micro VM with a startup script that connect the database, run the command and destroy itself at the end.

2 Comments

I want to deploy a Postgres DB with PostGIS : D Jokes aside, I understand what you mean but it feels like a technicallity. It would be incredibly helpful if the official Terraform Cloud SQL module would take an array of extensions and get them installed for you transparently. If that is not the case, then we enter workaround territory.
I think that is hard to document something when it is obvious for the google developers: There is the runtime environment configuration, and the db engine configuration. With terraform your configure the runtime environment, to configure the db engine, you need to log into, therefore it's not part of the terraform module, no need to document because it's obvious. Sadly, as often in the cloud providers (Google and others) documentation, it's not obvious for everyone and the documentation lack on these points...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.