Error executing: CREATE DATABASE aNewDb . Cause: org.postgresql.util.PSQLException: ERROR: CREATE DATABASE cannot run inside a transaction block
NickName:Kuzma Ask DateTime:2020-02-01T06:36:15

Error executing: CREATE DATABASE aNewDb . Cause: org.postgresql.util.PSQLException: ERROR: CREATE DATABASE cannot run inside a transaction block

Main class where the code is running :

static final String JDBC_DRIVER = "org.postgresql.Driver";
static final String DB_URL = "jdbc:postgresql://localhost:5432/postgres";

//  Database credentials
static final String USER = "postgres";
static final String PASS = "postgres";

public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;
    try {
        // STEP 1: Register JDBC driver
        Class.forName(JDBC_DRIVER);

        //STEP 2: Open a connection
        System.out.println("Connecting to database...");
        conn = DriverManager.getConnection(DB_URL, USER, PASS);

        //STEP 3: Execute a query
        ScriptRunner sr = new ScriptRunner(conn);
        //Creating a reader object

        Reader reader = new BufferedReader(new FileReader("C:\\PROJECTS\\SQL\\src\\main\\resources\\createDatabaseAndUser.sql"));

        //Running the script
        sr.runScript(reader);

And this is the file createDatabaseAndUser.sql :

CREATE DATABASE aNewDb;

I am trying to run my program in IDEA and I receive the ERROR :

Connecting to database...
CREATE DATABASE aNewDb
Error executing: CREATE DATABASE aNewDb.
Cause: org.postgresql.util.PSQLException: ERROR: CREATE DATABASE cannot run inside a transaction block

When I run this script without program ( without Main.class ) everything is OK and the database created without any problems.

Copyright Notice:Content Author:「Kuzma」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60012171/error-executing-create-database-anewdb-cause-org-postgresql-util-psqlexcepti

More about “Error executing: CREATE DATABASE aNewDb . Cause: org.postgresql.util.PSQLException: ERROR: CREATE DATABASE cannot run inside a transaction block” related questions

Error executing: CREATE DATABASE aNewDb . Cause: org.postgresql.util.PSQLException: ERROR: CREATE DATABASE cannot run inside a transaction block

Main class where the code is running : static final String JDBC_DRIVER = "org.postgresql.Driver"; static final String DB_URL = "jdbc:postgresql://localhost:5432/postgres"; // Database credentials

Show Detail

Error CREATE DATABASE cannot run inside a transaction block

I am trying to create a database from a Java application running in Micronaut framework using jOOQ. The code contextSupplier.get().createDatabaseIfNotExists(this.databaseName).execute(); with a

Show Detail

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 ...

Show Detail

ansible cannot execute SQL CREATE DATABASE CREATE DATABASE cannot run inside a transaction block

There are several entries that CREATE DATABASE cannot run inside a transaction block which give the answer autocommit needs to be on. However, they do not reference ansible which is what I was look...

Show Detail

CREATE DATABASE cannot run inside a transaction block in pgadmin 4

I am triing to run a script in pgAdmin 4, but I get this error: CREATE DATABASE cannot run inside a transaction block And this is the script: CREATE USER ky_auth WITH PASSWORD 'ky_auth'; COMMENT...

Show Detail

Liquibase: ERROR: CREATE DATABASE cannot run inside a transaction block

I am trying to create a db on PostgreSQL using liquibase. This is my dbchangelog file <?xml version="1.0" encoding="UTF-8"?> <databaseChangeLog xmlns="http://www.

Show Detail

ERROR: CREATE DATABASE cannot run inside a transaction block SQL state: 25001 - Using PostgresSQL and pgAdmin 4

Hi guys I'm stuck on a problem for my first database on Postgres and I cannot find a solution, I tried to see if it was a matter of setting autocommit on but apparently from postgres 9.x it's an

Show Detail

postgresql: cannot run inside a transaction block

I am trying to run these commands together in SQLbox of phppgadmin CREATE DATABASE "ir-staging"; CREATE USER django WITH ENCRYPTED PASSWORD 'django'; GRANT ALL PRIVILEGES ON DATABASE &quo...

Show Detail

How would I create a PostgreSQL database using "CREATE DATABASE" over an ODBC connection using PyODBC?

I am attempting to create a PostgreSQL 12.1 database (not a schema) using the CREATE DATABASE statement from a Python 3.7.5 program connecting to a PostgreSQL 12.1 instance as user postgres over Un...

Show Detail

CREATE DATABASE inside transaction

According to postgresql docs; CREATE DATABASE cannot be executed inside a transaction block. Is there a technical reason for this?

Show Detail