Skip to content

Commit 003a7b4

Browse files
authored
Fix AWS RDS Schema Creation (#112)
Fixes #91 This fixes an issue where the postgres-operator is not able to create schemas on AWS RDS.
1 parent d5f48aa commit 003a7b4

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

pkg/postgres/database.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import (
99

1010
const (
1111
CREATE_DB = `CREATE DATABASE "%s"`
12-
CREATE_SCHEMA = `CREATE SCHEMA IF NOT EXISTS "%s" AUTHORIZATION "%s"`
12+
CREATE_SCHEMA = `CREATE SCHEMA IF NOT EXISTS "%s"`
1313
CREATE_EXTENSION = `CREATE EXTENSION IF NOT EXISTS "%s"`
1414
ALTER_DB_OWNER = `ALTER DATABASE "%s" OWNER TO "%s"`
15+
ALTER_SCHEMA_OWNER = `ALTER SCHEMA "%s" OWNER TO "%s"`
1516
DROP_DATABASE = `DROP DATABASE "%s"`
1617
GRANT_USAGE_SCHEMA = `GRANT USAGE ON SCHEMA "%s" TO "%s"`
1718
GRANT_ALL_TABLES = `GRANT %s ON ALL TABLES IN SCHEMA "%s" TO "%s"`
@@ -50,10 +51,19 @@ func (c *pg) CreateSchema(db, role, schema string, logger logr.Logger) error {
5051
}
5152
defer tmpDb.Close()
5253

53-
_, err = tmpDb.Exec(fmt.Sprintf(CREATE_SCHEMA, schema, role))
54+
_, err = tmpDb.Exec(fmt.Sprintf(CREATE_SCHEMA, schema))
5455
if err != nil {
5556
return err
5657
}
58+
59+
// Set the schema owner in a separate step, because AWS RDS breaks if
60+
// you try to create a schema and set the owner in a single command.
61+
// See: https://github.com/movetokube/postgres-operator/issues/91
62+
_, err = c.db.Exec(fmt.Sprintf(ALTER_SCHEMA_OWNER, schema, role))
63+
if err != nil {
64+
return err
65+
}
66+
5767
return nil
5868
}
5969

0 commit comments

Comments
 (0)