I am a newby in terraform and working with GCP project. I have following terraform code that creates a postgres database for an existing database instance
resource "google_sql_database" "db" {
name = "test_db1"
instance = "already_existing_instance"
project = var.project_id
}
And I would like to understand how (if possible) to run some SQL initial script for that newly created database with terraform script. SQL script should create a table and insert few records.
CodePudding user response:
Terraform is not really designed for schema management, so in the long run you might be better to seek out a tool more specialized for that task (for example, so that it can potentially modify the schema later as your needs change).
If you do still want to take these actions with Terraform, you'd need to seek out a provider which supports the protocol for whichever database engine you've selected. For example, if you're using the Postgres database engine then you could potentially use the cyrilgdn/postgresql
provider. (I've not used this provider myself, so I'm just giving this as an example and I can't vouch for it's suitability.)
However, providers like these typically go only as far as creating the object which contains tables (in Postgres terms, the "schema" object). To go more granular than that, creating tables and possibly data, you may not be able to achieve your goals with Terraform alone (unless you write your own provider), and may need to resort to using external software for that final setup step.