Skip to content

Commit 5110adb

Browse files
committed
Also set up the login hook on all existing dbs
1 parent 4b098cd commit 5110adb

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

lib/charms/postgresql_k8s/v1/postgresql.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,8 +952,17 @@ def set_up_login_hook_function(self) -> None:
952952
END;
953953
END;
954954
$$ LANGUAGE plpgsql;"""
955+
connection = None
955956
try:
956-
for database in ["postgres", "template1"]:
957+
databases = []
958+
with self._connect_to_database() as connection, connection.cursor() as cursor:
959+
cursor.execute("SELECT datname FROM pg_database where datname <> 'template0';")
960+
db = cursor.fetchone()
961+
while db:
962+
databases.append(db[0])
963+
db = cursor.fetchone()
964+
965+
for database in databases:
957966
with self._connect_to_database(
958967
database=database,
959968
) as connection, connection.cursor() as cursor:
@@ -964,6 +973,8 @@ def set_up_login_hook_function(self) -> None:
964973
except psycopg2.Error as e:
965974
logger.error(f"Failed to create login hook function: {e}")
966975
raise e
976+
finally:
977+
connection.close()
967978

968979
def set_up_predefined_catalog_roles_function(self) -> None:
969980
"""Create predefined catalog roles function."""

0 commit comments

Comments
 (0)