71

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?

4
  • 1
    The query itself would make a be very helpful addition to question. From the message alone I can only assume that you try to create a database in a Transaction. If so your database creation logically belongs before the transaction. Commented Oct 21, 2014 at 9:04
  • 1
    Hi, I am very new to postgraSQL. I am first time start with postgraSQL. I have searched for same error, but the last two days I am facing this issue. Your comment is quite difficult for me to understand. Commented Oct 21, 2014 at 9:18
  • 3
    "How can I resolve this issue?" - turn on autocommit. How you do that depends on the SQL client you are using. Commented Oct 21, 2014 at 9:23
  • Hi @a_horse_with_no_name Thanks for your valuable comment. I have to turn on autocommit and that create command is working. Commented Oct 21, 2014 at 9:34

7 Answers 7

24

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

Sign up to request clarification or add additional context in comments.

7 Comments

In DBeaver go to "Edit connection" (F4) and tick the autocommit-on checkbox
That parameter hasn't done anything for a long time: stackoverflow.com/a/40546968/2850543
@MillieSmith that is not correct. The link you referred to is talking about a client library deprecating a parameter in its own API. Postgres supports setting autocommit behavior. Here is the current documentation: postgresql.org/docs/current/ecpg-sql-set-autocommit.html
This is not relevant from PostgreSQL 9.5 onwards
Referring to other answer - such option deprecated since Postgres 7.4. stackoverflow.com/questions/40517942/…
|
15

With psql you can also run drop and and create your database in one command by using multiple --command arguments:

psql postgres -c "drop database if exists <your_db_here>;" -c "create database <your_db_here>;"

1 Comment

This is the correct answer for new Postgres versions (2024). Thanks
11

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

Comments

4

In Postgres SQL 14.1, pgAdmin query tool, I see this same error when running the create database query with other queries. Running the create database query by itself completes successfully.

Comments

1

I had this issue when trying to execute PostgreSQL scripts in a python program like this:

with psycopg2.connect(
   dbname=database, host=server, port=port, user=user, password=password
) as connection:  
    connection.autocommit = True

what solved the issue for me it that I changed it to:

connection = psycopg2.connect(
    dbname=database, host=server, port=port, user=user, password=password
) 
connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)

Comments

1

The actual reason for the problem has not been described yet. It can be found in the PostgreSQL documentation.

"[...] the server executes [the command] as a single transaction even if the string contains multiple SQL commands, unless there are explicit BEGIN/COMMIT commands included in the string to divide it into multiple transactions."

It makes sense that some statements can not be executed in the same transaction. As of now the original sql command is not provided by @Nikunj K. I happened to run into this issue by creating a default database with a default user: sudo -u postgres psql -c \"CREATE DATABASE my_database; create user my_user with encrypted password 'JgiF?K430$cvi';

@mpoqq has already given a good non-version specific solution. His solution is in my opinion better than changing AUTOCOMMIT on setting that might change over time. It also separates the commands logically from each other which is easier to read.

So in my case it was:

sudo -u postgres psql -c "CREATE DATABASE my_database;" -c "CREATE USER my_user WITH ENCRYPTED PASSWORD 'JgiF?K430$cvi';"

Comments

1

For me, moving the CREATE DATABASE step to a different transaction worked.
This was throwing the error:

"CREATE USER user WITH PASSWORD 'somepassword'; CREATE DATABASE db WITH OWNER user;"

while this doesn't (as two separate transactions):

CREATE USER user WITH PASSWORD 'somepassword';
CREATE DATABASE db WITH OWNER user;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.