Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change owner of postgres database to created user #45

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions controllers/postgres/postgresqlconsumer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down