Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CREATE DATABASE cannot run inside a transaction block

I am working on AWS server + PostgreSQL. When I execute a query for creating the database I get an error:

CREATE DATABASE cannot run inside a transaction block 

I am working on Linux Ubuntu 12.04 LTS.

How can I resolve this issue?

like image 566
Nikunj K. Avatar asked Oct 21 '14 09:10

Nikunj K.


People also ask

What is transaction block in database?

A group of statements surrounded by BEGIN and COMMIT is sometimes called a transaction block.

How do I list databases in PostgreSQL?

Use \l or \l+ in psql to show all databases in the current PostgreSQL server. Use the SELECT statement to query data from the pg_database to get all databases.

How do I drop a database in PostgreSQL?

The first method to remove a PostgreSQL database is to use the following SQL statement: DROP DATABASE <database name>; The command removes the directory containing the database information and the catalog entries. Only the database owner can execute the DROP DATABASE command.


2 Answers

I have used turn on autocommit in PostgreSQL and it's working for me.

Here is the query to turn on the autocommit

SET AUTOCOMMIT = ON 

Note that this only works for PostgreSQL 9.4 and below

like image 133
Nikunj K. Avatar answered Sep 17 '22 12:09

Nikunj K.


Note, for postgres 9.5+ you have to use:

psql -c '\set AUTOCOMMIT on' 

But I'm going to guess, that what you really wanted to do is destroy the database and recreate it in a single command. Here you go:

printf '\set AUTOCOMMIT on\ndrop database <your_db_here>; create database <your_db_here>; ' |  psql postgres 
like image 39
Javier Buzzi Avatar answered Sep 16 '22 12:09

Javier Buzzi