From 51b0e42477d94b3f4f8320c69f14d7da05eab4b3 Mon Sep 17 00:00:00 2001 From: Rishi Kumar <96657880+k3yss@users.noreply.github.com> Date: Thu, 29 Aug 2024 12:22:23 +0530 Subject: [PATCH] fix: postgres perms script (#212) --- docs/pg-migration-guide/README.md | 17 ++++++----------- .../postgresql/gcp/bin/postgres-perms-update.sh | 5 +++-- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/docs/pg-migration-guide/README.md b/docs/pg-migration-guide/README.md index 7b77ad48..f611369d 100644 --- a/docs/pg-migration-guide/README.md +++ b/docs/pg-migration-guide/README.md @@ -5,11 +5,8 @@ Before proceeding, please review the [known limitations](https://cloud.google.co # Step 1: Configure Source Instance and create connection profile - Decide upon a instance to upgrade: - - We are choosing the `rishi-pg14-volcano-staging-pg-a34e9984` instance, a PostgreSQL 14 instance managed via the `galoy-infra/modules/postgresql/gcp` Terraform module. - ![decide-source](./assets/decide-source-instance.png) - - On the terraform file of the decided instance, enable the `prep_upgrade_as_source_db` flag ```hcl @@ -150,17 +147,17 @@ terraform output --json migration_destination_database_creds ```sh $ psql postgres=> ALTER ROLE cloudsqlexternalsync RENAME TO ''; -postgres=> ALTER ROLE "volcano-staging-pg-admin" PASSWORD '' +postgres=> ALTER ROLE "" PASSWORD ''; ``` #### Step 3.5.2 Manipulate the old state to reflect the new state by running the two scripts located at `galoy-infra/examples/gcp/bin` ```sh -$ ./terraform-db-swap.sh +$ ./terraform-db-swap.sh # This will ask for your terraform module name # And swap the state between the newer and old instance -$ ./terraform-state-rm.sh +$ ./terraform-state-rm.sh # This will ask for your terraform module name, give it the same name as you gave before # This will remove all the conflicting state which terraform will try to remove manually ``` @@ -193,8 +190,6 @@ module "postgresql" { } ``` - - #### Step 3.5.4 Finally, do a @@ -210,8 +205,8 @@ The destination instance should be exactly as with the source PostgreSQL instanc Change the owners of the tables and schemas to the correct owner using the psql command: ```sh -#TODO after the dry run - +#TODO | Need to do a dry run +./postgres-perms-update.sh ``` # Step 4: Promote the instance @@ -253,7 +248,7 @@ Do a `terraform apply` ### Delete the Database Migration Service that we used for migration. ```sh -$ gcloud database-migration migration-jobs describe "test-job" --region=us-east1 +$ gcloud database-migration migration-jobs delete "test-job" --region=us-east1 ``` ### Delete the source and external replica instance diff --git a/modules/postgresql/gcp/bin/postgres-perms-update.sh b/modules/postgresql/gcp/bin/postgres-perms-update.sh index 4f26a4c4..e4b1ed9f 100755 --- a/modules/postgresql/gcp/bin/postgres-perms-update.sh +++ b/modules/postgresql/gcp/bin/postgres-perms-update.sh @@ -7,6 +7,9 @@ read -p "Enter PostgreSQL connection string: " PG_CON PSQL_CMD="psql $PG_CON -d $DB_NAME -At -c" +$PSQL_CMD "ALTER DATABASE postgres OWNER TO cloudsqlsuperuser;" +$PSQL_CMD "ALTER SCHEMA public OWNER TO cloudsqlsuperuser;" + $PSQL_CMD "GRANT \"$NEW_OWNER\" TO \"postgres\";" # Get list of all tables in the database tables=$($PSQL_CMD "SELECT tablename FROM pg_tables WHERE schemaname = 'public';") @@ -17,5 +20,3 @@ for table in $tables; do done echo "Ownership of all tables in $DB_NAME has been granted to $NEW_OWNER." - -#$PSQL_CMD "ALTER SCHEMA public OWNER TO \"$NEW_OWNER\";"