0

I have created postgresql instance (with db and users) and successfully deployed on Google cloud platform. Can someone please suggest how can I create tables in this db through Terraform?

Vivek
  • 11
  • 1
  • 2
  • 1
    My Opinion: Terraform is designed for the infrastructure, not for the soft part. I recommend you to look at something else, like Ansible more suitable for that. – guillaume blaquiere Mar 20 '21 at 19:31
  • 1
    I agree. Use the correct tool for the task. Using Terraform for non-infrastructure tasks via hooks and hacks will one day result in Terraform recreating something and then you will have data loss. – John Hanley Mar 20 '21 at 19:35

2 Answers2

1

You can use a provisioner, something along the lines of:

resource "google_sql_database_instance" "default" {
  [...]
  provisioner "local-exec" {
    command = "PGPASSWORD=<password> psql -f schema.sql -p <port> -U <username> <databasename>"
  }
}

schema.sql would hold your table definitions.

Caveats: psql must be installed on the database server and you need to provide the password to the command, see also this SO question.

yvesonline
  • 4,609
  • 2
  • 21
  • 32
  • 1
    On GCP i have already created a postgresql instance and a database. I also installed postgresql on my local and set path varibales as well but while running the above script in terraform throws error. 'psql' is not recognized as an internal or external command, – Vivek Mar 23 '21 at 06:42
0

follow the different steps in Github , here i terraform module for Postgresql https://github.com/cyrilgdn/terraform-provider-postgresql

itIsNaz
  • 621
  • 5
  • 11