From cd11d7677e80826d15d4a79935eb127e5129946c Mon Sep 17 00:00:00 2001 From: "wu.chunyang" Date: Fri, 16 Jun 2023 11:35:22 +0800 Subject: [PATCH] Fix "create database" failed for postgress MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR stops using the psycopg2’s connections context manager because "CREATE DATABASE" command must be run outside any transaction[1]. [1]: https://www.psycopg.org/docs/usage.html#transactions-control Co-Authored-By: hungnt1 Co-Authored-By: Hirotaka Wakabayashi Story: 2010761 Task: 48059 Change-Id: I73ec6c659d8ad7216460055077737429c878df33 --- .../fix-postgress-create-database-75fbe03e3b4e296d.yaml | 8 ++++++++ trove/guestagent/datastore/postgres/service.py | 7 +++++-- 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 releasenotes/notes/fix-postgress-create-database-75fbe03e3b4e296d.yaml 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: