Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: allow postgres user name with hyphen (#4236)

gandergof0d437364f0d
diff --git a/plugin/db/pg/dump.go b/plugin/db/pg/dump.go
index 19d3b20473..245d46c37b 100644
--- a/plugin/db/pg/dump.go
+++ b/plugin/db/pg/dump.go
@@ -176,7 +176,7 @@ func (driver *Driver) Restore(ctx context.Context, sc io.Reader) error {
return errors.Wrapf(err, "failed to get the OWNER of the current database")
}
- if _, err := txn.ExecContext(ctx, fmt.Sprintf("SET LOCAL ROLE %s;", owner)); err != nil {
+ if _, err := txn.ExecContext(ctx, fmt.Sprintf("SET LOCAL ROLE \"%s\";", owner)); err != nil {
return errors.Wrapf(err, "failed to set role to %q", owner)
}
@@ -189,7 +189,7 @@ func (driver *Driver) Restore(ctx context.Context, sc io.Reader) error {
stmt = strings.ReplaceAll(stmt, "EXECUTE FUNCTION", "EXECUTE PROCEDURE")
}
if isSuperuserStatement(stmt) {
- stmt = fmt.Sprintf("SET LOCAL ROLE NONE;%sSET LOCAL ROLE %s;", stmt, owner)
+ stmt = fmt.Sprintf("SET LOCAL ROLE NONE;%sSET LOCAL ROLE \"%s\";", stmt, owner)
}
if isIgnoredStatement(stmt) {
return nil
diff --git a/plugin/db/pg/pg.go b/plugin/db/pg/pg.go
index ba190e23a5..5a41779050 100644
--- a/plugin/db/pg/pg.go
+++ b/plugin/db/pg/pg.go
@@ -308,7 +308,7 @@ func (driver *Driver) Execute(ctx context.Context, statement string, createDatab
stmt = strings.ReplaceAll(stmt, "EXECUTE FUNCTION", "EXECUTE PROCEDURE")
}
// Use superuser privilege to run privileged statements.
- stmt = fmt.Sprintf("SET LOCAL ROLE NONE;%sSET LOCAL ROLE %s;", stmt, owner)
+ stmt = fmt.Sprintf("SET LOCAL ROLE NONE;%sSET LOCAL ROLE \"%s\";", stmt, owner)
remainingStmts = append(remainingStmts, stmt)
} else if !isIgnoredStatement(stmt) {
remainingStmts = append(remainingStmts, stmt)
@@ -332,7 +332,7 @@ func (driver *Driver) Execute(ctx context.Context, statement string, createDatab
defer tx.Rollback()
// Set the current transaction role to the database owner so that the owner of created database will be the same as the database owner.
- if _, err := tx.ExecContext(ctx, fmt.Sprintf("SET LOCAL ROLE %s", owner)); err != nil {
+ if _, err := tx.ExecContext(ctx, fmt.Sprintf("SET LOCAL ROLE \"%s\"", owner)); err != nil {
return 0, err
}
diff --git a/server/issue.go b/server/issue.go
index d2fdc56a8a..81b0006720 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -1234,7 +1234,7 @@ func getCreateDatabaseStatement(dbType db.Type, createDatabaseContext api.Create
//
// For tenant project, the schema for the newly created database will belong to the same owner.
// TODO(d): alter schema "public" owner to the database owner.
- return fmt.Sprintf("%s\nALTER DATABASE \"%s\" OWNER TO %s;", stmt, databaseName, createDatabaseContext.Owner), nil
+ return fmt.Sprintf("%s\nALTER DATABASE \"%s\" OWNER TO \"%s\";", stmt, databaseName, createDatabaseContext.Owner), nil
case db.ClickHouse:
clusterPart := ""
if createDatabaseContext.Cluster != "" {