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?

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. Commented Mar 20, 2021 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. Commented Mar 20, 2021 at 19:35

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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,
0

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

Comments

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.