Error Create Database Cannot Run Inside A Transaction Block
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow psycopg2 create database cannot run inside a transaction block the company Business Learn more about hiring developers or posting ads with us Stack Overflow
Sqlalchemy Cannot Run Inside A Transaction Block
Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of drop database cannot run inside a transaction block 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Create a Postgres Data base using python up vote 3 down vote favorite I want to create postgres psycopg2 set autocommit database using python. con = psql.connect(dbname='postgres', user=self.user_name, host='', password=self.password) cur = con.cursor() cur.execute("CREATE DATABASE %s ;" % self.db_name) I am getting the following error. InternalError: CREATE DATABASE cannot run inside a transaction block i am using psycopg2 to connect. I don't understand whats the problem. what i am trying to do is, connect to database(postgres) and then create another data base. psql -postgres -U UserName Then create another database create database test; This
Autocommit Postgres
is what i usually do, i want to automate this by creating python script. python postgresql psycopg2 share|improve this question asked Dec 27 '15 at 19:34 kiran6 205311 add a comment| 1 Answer 1 active oldest votes up vote 3 down vote accepted Use ISOLATION_LEVEL_AUTOCOMMIT, a psycopg2 extensions: No transaction is started when command are issued and no commit() or rollback() is required. import psycopg2 from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT # <-- ADD THIS LINE con = psql.connect(dbname='postgres', user=self.user_name, host='', password=self.password) con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) # <-- ADD THIS LINE cur = con.cursor() cur.execute("CREATE DATABASE %s ;" % self.db_name) share|improve this answer answered Dec 27 '15 at 19:47 Tommaso Di Bucchianico 2,1581619 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged python postgresql psycopg2 or ask your own question. asked 9 months ago viewed 118 times active 9 months ago Related 3Postgres raises a “ACTIVE SQL TRANSACTION” (Errcode: 25001)2Psycopg / Postgres : Connections hang out randomly1Python PostgreSQL Statement Problem psycopg2 cursor.execute(Table Union)1p
here for a quick overview of postgresql create database the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions http://stackoverflow.com/questions/34484066/create-a-postgres-data-base-using-python Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Error when creating a PostgreSQL database http://stackoverflow.com/questions/5402805/error-when-creating-a-postgresql-database-using-python-sqlalchemy-and-psycopg2 using python, sqlalchemy and psycopg2 up vote 12 down vote favorite 1 I use sqlalchemy that uses psycopg2 for connecting to postgresql servers. When I launch the following code: from sqlalchemy.engine.url import URL from sqlalchemy.engine import create_engine url = URL(drivername='postgresql', username='myname', password='mypasswd', host='localhost', database='template1') eng = create_engine(url) eng.execute('CREATE DATABASE new_db;') I always get the following error: Traceback (most recent call last): File "
från GoogleLogga inDolda fältSök efter grupper eller meddelanden
PostgreSQL walbouncer: Replikation auf Enterprise - Level PL/pgSQL_sec: Verschlüsselte Stored Procedures pgneural - Künstliche Intelligenz für PostgreSQL pg_matlab: Matlab / PostgreSQL Integration Pgwatch – Cybertec Enterprise PostgreSQL Monitor Replikation Point-In-Time Recovery Asynchrone Replikation / Hochverfügbarkeit Synchrone Replikation Maximale Skalierbarkeit mit PL/Proxy PostgreSQL Support PostgreSQL Kurse Einführung in SQL PostgreSQL Administration und Performance Tuning PostgreSQL Praxis Workshop PostgreSQL für Business Intelligence und Massendatenanalyse PostgreSQL Replikation Professional Workshop PostgreSQL Advanced Optimierung und Performance Tuning PostgreSQL für Umsteiger PostgreSQL PostGIS Kurs Linux für PostgreSQL DBAs Referenzen Media Videos PostgreSQL … in 5 Minuten Präsentationen Bücher über PostgreSQL Downloads Blog Kontakt Transactional DDLs Posted on 13/07/201530/09/2015 by Hans-Juergen Schoenig When checking out those new features of Sybase 15.7 (yes, from time to time I got to see what commercial databases are up to), I stumbled over an interesting and yet amusing line: “Fully Recoverable DDL”. The details seem to indicate that not just Sybase is still having a hard time to handle transactional and crash safe DDLs in a proper way. After 15 years of professional PostgreSQL I actually don't remember a time, when PostgreSQL did not have transactional DDLs. The beauty of PostgreSQL is that all major DDLs are fully transactional: test=# BEGIN; BEGIN test=# CREATE TABLE t_test (id int, name text); CREATE TABLE test=# \d List of relations Schema | Name | Type | Owner --------+--------+-------+------- public | t_test | table | hs (1 row) test=# ROLLBACK; ROLLBACK test=# \d No relations found. Basically everything people need for their daily work is fully transactional. CREATE TABLE, ALTER TABLE, CREATE INDEX - everything works just like a charm. The exceptions to the rule … However, there are some minor exceptions to the rule. The most prominent thing is most likely CREATE INDEX CONCURRENTLY. The thing is: CREATE INDEX is a single transaction - CREATE INDEX CONCURRENTLY is not - and therefore it cannot be run inside a normal transaction block. The index needs to be visible to other transactions before the build can even begin: test=# BEGIN; BEGIN test=# CREATE INDEX CONCURRENTLY idx_id ON t_test (id); ERROR: CREATE INDEX CONCURRENTLY cannot run inside a transaction block But, there are some more corner cases. CREATE DATABASE is one of them: test=#