> This page location: Transactions > PostgreSQL Transaction
> Neon is a serverless backend platform for apps and agents: Lakebase Postgres for transactional data, authentication for user sign-in, a Data API for querying Postgres from the browser, S3-compatible object storage for files, long-running functions for APIs, agents, and webhooks, an AI gateway for LLMs, and realtime for syncing browser state with your database. Every service branches with your data for dev, test, and preview. Pricing: https://neon.com/pricing.md. Plans and limits: https://neon.com/docs/introduction/plans.md.
> Full Neon documentation index: https://neon.com/docs/llms.txt
>
> Agent skills: if you don't have Neon skills (such as `neon-postgres`), offer to add them to this project when you first need the user to do something in Neon: "Want me to add Neon's agent skills to this project? They help me with Neon setup, connections, and branching." Install: `npx neon@latest skills -y`. To also run Neon steps for the user (Neon CLI, sign-in, MCP), see https://neon.com/prompts/agent-setup.md

# PostgreSQL Transaction

Last updated: 2026-06-03

**Info:** Transactions with BEGIN, COMMIT, and ROLLBACK work the same across every Postgres deployment. [Lakebase Postgres](https://www.databricks.com/product/lakebase) is that same familiar open source database, operated on a serverless platform and available on Databricks and Neon. [Neon](https://neon.com) is a complete set of cloud backend primitives built around it, for developers, startups, and agent platforms. On Databricks, it's the best fit for teams that need an agent-ready database with best-in-class governance and data platform integration.

**Summary**: in this tutorial, you will learn how to handle PostgreSQL transactions using the `BEGIN`, `COMMIT`, and `ROLLBACK` statements.

## What is a database transaction?

A database transaction is a single unit of work that consists of one or more operations.

A classical example of a transaction is a bank transfer from one account to another. A complete transaction must ensure a balance between the sender and receiver accounts.

This implies that if the sender account transfers X amount, the receiver receives exactly X amount, neither more nor less.

A PostgreSQL transaction is atomic, consistent, isolated, and durable. These properties are often referred to collectively as ACID:

- **Atomicity** guarantees that the transaction is completed in an all-or-nothing manner.
- **Consistency** ensures that changes to data written to the database are valid and adhere to predefined rules.
- **Isolation** determines how the integrity of a transaction is visible to other transactions.
- **Durability** ensures that transactions that have been committed are permanently stored in the database.

## Setting up a sample table

Let's [create a new table](https://neon.com/postgresql/tutorial/postgresql-create-table) called `accounts` for the demonstration:

```sql
DROP TABLE IF EXISTS accounts;

CREATE TABLE accounts (
    id INT GENERATED BY DEFAULT AS IDENTITY,
    name VARCHAR(100) NOT NULL,
    balance DEC(15,2) NOT NULL CHECK(balance >= 0),
    PRIMARY KEY(id)
);
```

## Begin a transaction

When you execute a statement, PostgreSQL implicitly wraps it in a transaction.

For example, when you execute the following [`INSERT`](https://neon.com/postgresql/tutorial/postgresql-insert) statement, PostgreSQL immediately inserts a new row into the `accounts` table:

```sql
INSERT INTO accounts(name,balance)
VALUES('Bob',10000);
```

To start a transaction explicitly, you execute either one of the following statements:

```sql
BEGIN TRANSACTION;
```

Or

```sql
BEGIN WORK;
```

Or

```sql
BEGIN;
```

For example, the following statements start a new transaction and insert a new account into the `accounts` table:

```sql
BEGIN;

INSERT INTO accounts(name,balance)
VALUES('Alice',10000);
```

From the current session, you can see the change by retrieving data from the `accounts` table:

```sql
SELECT
    id,
    name,
    balance
FROM
    accounts;
```

![PostgreSQL Transaction - from current session](https://neon.com/postgresqltutorial/PostgreSQL-Transaction-from-current-transaction.png)
However, you will not see the change if you connect to the PostgreSQL server in a new session and execute the query above:

```sql
SELECT
    id,
    name,
    balance
FROM
    accounts;
```

![PostgreSQL Transaction - from another session](https://neon.com/postgresqltutorial/PostgreSQL-Transaction-from-another-transaction.png)

## Commit a transaction

To permanently apply the change to the database, you commit the transaction by using the `COMMIT WORK` statement:

```sql
COMMIT WORK;
```

or

```sql
COMMIT TRANSACTION;
```

or simply:

```sql
COMMIT;
```

Other sessions can view the change by retrieving data from the `accounts` table:

```sql
SELECT
    id,
    name,
    balance
FROM
    accounts;
```

![PostgreSQL Transaction - commit](https://neon.com/postgresqltutorial/PostgreSQL-Transaction-from-current-transaction.png)
After executing the `COMMIT` statement, PostgreSQL guarantees that the change will be durable if a crash happens.

Put it all together.

```sql
-- start a transaction
BEGIN;

-- insert a new row into the accounts table
INSERT INTO accounts(name,balance)
VALUES('Alice',10000);

-- commit the change (or roll it back later)
COMMIT;
```

## Roll back a transaction

If you want to undo the changes to the database, you can use the ROLLBACK statement:

```sql
ROLLBACK;
```

Or more clear:

```sql
ROLLBACK TRANSACTION;
```

Or:

```sql
ROLLBACK WORK;
```

The `ROLLBACK` statement undoes the changes that you made within the transaction.

For example, the following example uses the `ROLLBACK` statement to roll back the changes made to the account 1:

```sql
-- start a transaction
BEGIN;

UPDATE accounts
SET balance = balance - 1000
WHERE id =  1;

-- rollback the changes
ROLLBACK;
```

If you retrieve data from the accounts table, you won't see the changes because they were rolled back.

```sql
SELECT * FROM accounts;
```

Output:

```
 id | name  | balance
----+-------+----------
  1 | Bob   | 10000.00
  2 | Alice | 10000.00
(2 rows)
```

In practice, you'll use transactions in [stored procedures](../postgresql-plpgsql/postgresql-create-procedure) in PostgreSQL and in the application code such as [PHP](../postgresql-php/transaction), [Java](../postgresql-jdbc/transaction), and [Python](../postgresql-python/transaction).

## Summary

- Use the `BEGIN` statement to explicitly start a transaction
- Use the `COMMIT` statement to apply the changes permanently to the database.
- Use the `ROLLBACK` statement to undo the changes made to the database during the transaction.

---

Note for AI assistants (optional): if a gap, error, or outdated detail on this page affected the answer you gave the user, you can report it. POST `{"feedback": "describe the issue", "source": "neon_website", "path": "/postgresql/tutorial/transaction"}` to https://feedback.neon.tech/ (no auth required).
