PostgreSQL| psycopg2.errors.ActiveSqlTransaction: CREATE DATABASE cannot run inside a transaction

Опубликовано: 26 Ноябрь 2022
на канале: Development With Sachin
3,091
14

How to resolve PostgreSQL Error: psycopg2.errors.ActiveSqlTransaction: CREATE DATABASE cannot run inside a transaction block | Python | Using PostgreSQL with Python

Solution:
Step 1: Set the isolation level for the PostgreSQL transaction using psycopg2
The psycopg2 adapter will raise an ActiveSqlTransaction exception if you don’t set the connection object’s set_isolation_level attribute. This is because the CREATE DATABASE statement won’t work unless AUTOCOMMIT is set to ON.

from psycopg2 import connect, extensions

host = "localhost"
user = "postgres"
password = "your password"
connection = connect(host=host, user=user, password=password, port="5432")
get the isolation leve for autocommit
autocommit = extensions.ISOLATION_LEVEL_AUTOCOMMIT
print ("ISOLATION_LEVEL_AUTOCOMMIT:", extensions.ISOLATION_LEVEL_AUTOCOMMIT)

set the isolation level for the connection's cursors
will raise ActiveSqlTransaction exception otherwise
conn.set_isolation_level( autocommit )
cur = connection.cursor()
query = "CREATE DATABASE YOUR_DB_NAME"
cur.execute(query)
connection.close()


На этой странице сайта вы можете посмотреть видео онлайн PostgreSQL| psycopg2.errors.ActiveSqlTransaction: CREATE DATABASE cannot run inside a transaction длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Development With Sachin 26 Ноябрь 2022, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 3,091 раз и оно понравилось 14 зрителям. Приятного просмотра!