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:
- Run the command outside
BEGIN ... COMMIT(in autocommit mode). - In migration tools, mark the step as non-transactional.
Reference: PostgreSQL error codes — Class 25 (Invalid Transaction State).
Thanks — noted. This helps keep the database accurate.