{"id":15750827,"url":"https://github.com/rgl/terraform-gcp-cloud-sql-postgres","last_synced_at":"2025-03-31T07:21:26.301Z","repository":{"id":139753331,"uuid":"523858213","full_name":"rgl/terraform-gcp-cloud-sql-postgres","owner":"rgl","description":"Example GCP Cloud SQL PostgreSQL instance using terraform","archived":false,"fork":false,"pushed_at":"2022-08-12T07:48:04.000Z","size":7,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-06T11:56:51.243Z","etag":null,"topics":["cloud-sql","gcp","postgres","terraform"],"latest_commit_sha":null,"homepage":"","language":"HCL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rgl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-08-11T20:21:53.000Z","updated_at":"2022-08-11T20:25:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"33e2f37d-6918-4db3-b217-69584292b63f","html_url":"https://github.com/rgl/terraform-gcp-cloud-sql-postgres","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rgl%2Fterraform-gcp-cloud-sql-postgres","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rgl%2Fterraform-gcp-cloud-sql-postgres/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rgl%2Fterraform-gcp-cloud-sql-postgres/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rgl%2Fterraform-gcp-cloud-sql-postgres/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rgl","download_url":"https://codeload.github.com/rgl/terraform-gcp-cloud-sql-postgres/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246429440,"owners_count":20775809,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cloud-sql","gcp","postgres","terraform"],"created_at":"2024-10-04T07:00:20.265Z","updated_at":"2025-03-31T07:21:26.270Z","avatar_url":"https://github.com/rgl.png","language":"HCL","readme":"# About\n\nThis creates an example GCP Cloud SQL PostgreSQL instance using terraform.\n\nThis will:\n\n* Create a public PostgreSQL instance.\n* Configure the PostgresSQL instance to require mTLS.\n* Enable automated backups.\n* Set a random `postgres` account password.\n* Show how to connect to the created PostgreSQL instance using `psql`.\n\nFor further managing the PostgreSQL instance, you could use:\n\n* The [community.postgresql Ansible Collection](https://galaxy.ansible.com/community/postgresql) as in [rgl/ansible-init-postgres](https://github.com/rgl/ansible-init-postgres).\n\n## Usage\n\nInstall `terraform`, `gcloud`, and `docker`.\n\nLogin into your GCP account:\n\n```bash\n# see https://cloud.google.com/sdk/docs/authorizing\ngcloud auth login --no-launch-browser\ngcloud config set project PROJECT_ID # gcloud projects list\ngcloud config set compute/region REGION_ID # gcloud compute regions list\ngcloud auth application-default login --no-launch-browser\n```\n\nVerify your GCP account settings:\n\n```bash\ngcloud config get account\ngcloud config get project\ngcloud config get compute/region\n```\n\nCreate the example:\n\n```bash\nexport CHECKPOINT_DISABLE=1\nexport TF_LOG=TRACE\nexport TF_LOG_PATH=terraform.log\nexport TF_VAR_project=\"$(gcloud config get project)\"\nexport TF_VAR_region=\"$(gcloud config get compute/region)\"\nterraform init\nterraform plan -out=tfplan\n# NB it takes about 20m to create a simple google_sql_database_instance. YMMV.\nterraform apply tfplan\n```\n\nConnect to it:\n\n```bash\n# see https://www.postgresql.org/docs/14/libpq-envars.html\n# see https://cloud.google.com/sql/docs/postgres/connect-admin-ip?authuser=2#connect-ssl\nterraform output -raw ca \u003epgcacerts.pem\nterraform output -raw crt \u003epostgres-crt.pem\ninstall -m 600 /dev/null postgres-key.pem\nterraform output -raw key \u003epostgres-key.pem\ninstall -m 600 /dev/null pgpass.conf\necho \"$(terraform output -raw ip_address):5432:postgres:postgres:$(terraform output -raw password)\" \u003epgpass.conf\ndocker run \\\n    --rm \\\n    -it \\\n    -v \"$PWD:/host:ro\" \\\n    -e \"PGSSLROOTCERT=/host/pgcacerts.pem\" \\\n    -e \"PGSSLCERT=/host/postgres-crt.pem\" \\\n    -e \"PGSSLKEY=/host/postgres-key.pem\" \\\n    -e \"PGPASSFILE=/host/pgpass.conf\" \\\n    -e \"PGHOSTADDR=$(terraform output -raw ip_address)\" \\\n    -e \"PGSSLMODE=verify-ca\" \\\n    -e \"PGDATABASE=postgres\" \\\n    -e \"PGUSER=postgres\" \\\n    postgres:14 \\\n    psql\n```\n\nExecute example queries:\n\n```sql\nselect version();\nselect current_user;\nselect case when ssl then concat('YES (', version, ')') else 'NO' end as ssl from pg_stat_ssl where pid=pg_backend_pid();\n```\n\nExit the `psql` session:\n\n```sql\nexit\n```\n\nDestroy everything:\n\n```bash\n# disable the delete protection.\nsed -i -E 's,(deletion_protection).*?=.*,\\1 = false,g' main.tf\nterraform plan -out=tfplan\nterraform apply tfplan\n# destroy everything, including all the data.\nterraform destroy\n# enable the delete protection (only in the source code, as the instance is already gone).\nsed -i -E 's,(deletion_protection).*?=.*,\\1 = true,g' main.tf\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frgl%2Fterraform-gcp-cloud-sql-postgres","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frgl%2Fterraform-gcp-cloud-sql-postgres","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frgl%2Fterraform-gcp-cloud-sql-postgres/lists"}