title: "Commands ignored until end of transaction block", with autocommit mode with psycopg2
body: <p>I'm writing a setup script for my Postgres database. As part of this, I want to create a user if it doesn't exist, and create the database if it doesn't exist, using the &quot;POSTGRES&quot; user and database to do this from. <a href="https://www.psycopg.org/docs/usage.html#transactions-control" rel="nofollow noreferrer">According to the Psycopg docs</a>, I need to have autocommit <em>on</em>, in order for this to work, so I do that here.</p>
  <pre class="lang-py prettyprint-override"><code>def postgres_create_user(username, conn, cur, password=None):
      &quot;&quot;&quot;
      Create a postgres user, including a password if provided.
      &quot;&quot;&quot;
      from psycopg2.sql import Identifier, Placeholder, SQL
      from psycopg2.errors import DuplicateObject, ProgrammingError
  
      # Run the CREATE USER
      try:
          if password:
              cur.execute(
                  SQL(&quot;CREATE USER {} WITH ENCRYPTED PASSWORD {}&quot;).format(Identifier(username), Placeholder()),
                  [password],
              )
          else:
              cur.execute(SQL(&quot;CREATE USER {}&quot;).format(Identifier(username)))
  
          print(f&quot;User {username} created successfully&quot;)
      except DuplicateObject as e:
          # it's okay if user already exists
          print(f&quot;User {username} already exists&quot;)
  
  
  def postgres_create_database(conn, cur, database, user):
      &quot;&quot;&quot;
      Create a Postgres database, with the given owner.
      &quot;&quot;&quot;
      from psycopg2.sql import Identifier, SQL
  
      # create database
      cur.execute(SQL(&quot;CREATE DATABASE {} OWNER {}&quot;).format(Identifier(database), Identifier(user)))
      print(&quot;Database created successfully&quot;)
  
  conn = default_engine.raw_connection()
  conn.autocommit = True
  
  with conn.cursor() as cur:
      print(&quot;Initializing the postgres user&quot;)
      postgres_create_user(
          config_url.username, conn=conn, cur=cur, password=config_url.password
      )   
  with conn.cursor() as cur:
      print(&quot;Initializing the postgres database&quot;)
      postgres_create_database(
          conn=conn, cur=cur, database=config_url.database, user=config_url.username
      )          
  </code></pre>
  <p>The trouble is that either of these functions (<code>postgres_create_user</code> and <code>postgres_create_database</code>) could fail. If the first fails (because the user already exists), and I try to run the second (CREATE DATABASE), I get this error:</p>
  <pre><code>megaqc_1  | psycopg2.errors.InFailedSqlTransaction: current transaction is aborted, commands ignored until end of transaction block
  </code></pre>
  <p>So it thinks I'm trying to use a failed transaction. But I enabled <code>autocommit</code> mode, so why is this happening? How can I successfully run both of these commands, ignoring failures?</p>