From c6b2cad896ae07b2f13f05285ccea74b13c26bda Mon Sep 17 00:00:00 2001 From: Ben Jackson Date: Tue, 25 May 2021 13:28:54 +1000 Subject: [PATCH] change owner of postgres database to created user --- .../postgres/postgresqlconsumer_controller.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/controllers/postgres/postgresqlconsumer_controller.go b/controllers/postgres/postgresqlconsumer_controller.go index 51fa7b2..63eec3a 100644 --- a/controllers/postgres/postgresqlconsumer_controller.go +++ b/controllers/postgres/postgresqlconsumer_controller.go @@ -374,6 +374,21 @@ func createDatabaseIfNotExist(provider postgresv1.PostgreSQLProviderSpec, consum } return fmt.Errorf("Unable to grant user %s permissions on database %s: %v", userName[0], consumer.Spec.Consumer.Database, err) } + var changeOwner string + changeOwner = fmt.Sprintf("ALTER DATABASE \"%s\" OWNER TO \"%s\";", consumer.Spec.Consumer.Database, userName[0]) + _, err = db.Exec(changeOwner) + if err != nil { + // if change ownership fails, drop the database and user that gets created + dropErr := dropDatabase(db, consumer.Spec.Consumer.Database) + if dropErr != nil { + return fmt.Errorf("Unable drop database after failed ownership change: %v", dropErr) + } + dropErr = dropUser(db, consumer, provider) + if dropErr != nil { + return fmt.Errorf("Unable drop user after failed ownership change: %v", dropErr) + } + return fmt.Errorf("Unable to change owner of database %s to %s: %v", consumer.Spec.Consumer.Database, userName[0], err) + } return nil }