Skip to content

Commit faad5af

Browse files
committed
Recreate roles on all existing databases
1 parent a179e9d commit faad5af

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

lib/charms/postgresql_k8s/v1/postgresql.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ def create_user(
307307
f"WITH LOGIN{' SUPERUSER' if admin else ''} ENCRYPTED PASSWORD '{password}'"
308308
)
309309
if in_role:
310-
user_definition += f" IN ROLE \"{in_role}\""
310+
user_definition += f' IN ROLE "{in_role}"'
311311
if can_create_database:
312312
user_definition += " CREATEDB"
313313
if privileges:
@@ -1035,8 +1035,18 @@ def set_up_predefined_catalog_roles_function(self) -> None:
10351035
END LOOP;
10361036
END;
10371037
$$ LANGUAGE plpgsql security definer;"""
1038+
connection = None
10381039
try:
1039-
for database in ["postgres", "template1"]:
1040+
databases = []
1041+
with self._connect_to_database() as connection, connection.cursor() as cursor:
1042+
cursor.execute()
1043+
cursor.execute("SELECT datname FROM pg_database where datname <> 'template0';")
1044+
db = cursor.fetchone()
1045+
while db:
1046+
databases.append(db[0])
1047+
db = cursor.fetchone()
1048+
1049+
for database in databases:
10401050
with self._connect_to_database(
10411051
database=database
10421052
) as connection, connection.cursor() as cursor:
@@ -1052,6 +1062,9 @@ def set_up_predefined_catalog_roles_function(self) -> None:
10521062
except psycopg2.Error as e:
10531063
logger.error(f"Failed to set up predefined catalog roles function: {e}")
10541064
raise PostgreSQLCreatePredefinedRolesError() from e
1065+
finally:
1066+
if connection:
1067+
connection.close()
10551068

10561069
def update_user_password(
10571070
self, username: str, password: str, database_host: Optional[str] = None

0 commit comments

Comments
 (0)