diff --git a/releasenotes/notes/fix-postgress-create-database-75fbe03e3b4e296d.yaml b/releasenotes/notes/fix-postgress-create-database-75fbe03e3b4e296d.yaml new file mode 100644 index 0000000000..96a227cbe7 --- /dev/null +++ b/releasenotes/notes/fix-postgress-create-database-75fbe03e3b4e296d.yaml @@ -0,0 +1,8 @@ +--- +fixes: + - | + Fix guest-agent failed to start postgres container due to execution + of the "CREATE DATABASE" statement within the context manager of + psycopg library. See the following for details + `Stroy 2010761 `__ + diff --git a/trove/guestagent/datastore/postgres/service.py b/trove/guestagent/datastore/postgres/service.py index 0712081c7a..4b000b8602 100644 --- a/trove/guestagent/datastore/postgres/service.py +++ b/trove/guestagent/datastore/postgres/service.py @@ -770,12 +770,15 @@ class PostgresConnection(object): def _execute_stmt(self, statement, identifiers, data_values, fetch, autocommit=False): cmd = self._bind(statement, identifiers) - with psycopg2.connect(self.connect_str) as connection: - connection.autocommit = autocommit + connection = psycopg2.connect(self.connect_str) + connection.autocommit = autocommit + try: with connection.cursor() as cursor: cursor.execute(cmd, data_values) if fetch: return cursor.fetchall() + finally: + connection.close() def _bind(self, statement, identifiers): if identifiers: