SQLSTATE 25001 ERROR Class 25: Invalid Transaction State

active_sql_transaction Active Sql Transaction — SQLSTATE 25001

A command was run that is not allowed inside a transaction block.

PG 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed May 2025 Grounded in source

Symptoms

A command was run that is not allowed inside a transaction block.

  • The error is written to the server log and returned to the client carrying SQLSTATE 25001.
  • Any driver (libpq, JDBC, psycopg, npgsql, pgx) surfaces this code in its error object so you can branch on it programmatically.
  • PL/pgSQL can trap it by name: EXCEPTION WHEN active_sql_transaction THEN.

Environment

Severity: ERROR  |  PostgreSQL versions: 12, 13, 14, 15, 16, 17

Reproduce with the exact statement and read the full message in the server log (raise log_min_messages / set log_min_error_statement for more context).

Root Cause

Commands such as CREATE DATABASE, DROP DATABASE, VACUUM, CREATE INDEX CONCURRENTLY, and ALTER SYSTEM cannot run inside an explicit transaction.

Diagnostic Queries

Recovery

Steps to resolve 25001:

  1. Run the command outside BEGIN ... COMMIT (in autocommit mode).
  2. In migration tools, mark the step as non-transactional.

Reference: PostgreSQL error codes — Class 25 (Invalid Transaction State).

Was this helpful?