Last Updated : 23 Jul, 2025
A transaction in database terminology is not a new concept. Similar to familiar terms such as "cash transaction" in banking, a transaction in the context of databases like PostgreSQL is a unit of work that ensures data integrity and consistency. Transactions are fundamental when you need to add, delete, or update records. Without transactions, these operations could compromise data integrity due to potential anomalies or concurrent access issues.
Why Transactions MatterTransactions are vital for maintaining the accuracy and reliability of a database. They ensure that even if an operation fails or the system crashes, the data remains consistent and accurate. This article explores how PostgreSQL handles transactions, emphasizing the crucial ACID properties that safeguard data integrity.
ACID Properties of TransactionThe ACID properties describe the essential characteristics that ensure transactions are processed reliably:
There are three main commands in a transaction block. They are:
BEGIN;PostgreSQL Transactions Examples
// set of statements
[COMMIT | ROLLBACK];
Now we will understand the importance of each and every transaction control command, for that, we have to set up a table in the database first.
PostgreSQL
CREATE TABLE BankStatements (
customer_id serial PRIMARY KEY,
full_name VARCHAR NOT NULL,
balance INT
);
INSERT INTO BankStatements (
customer_id ,
full_name,
balance
)
VALUES
(1, 'Sekhar rao', 1000),
(2, 'Abishek Yadav', 500),
(3, 'Srinivas Goud', 1000);
1. BEGIN
BEGIN command is used to initiate a transaction. To start a transaction we should give BEGIN command at first if we don't give it like that then the database cant able recognizes the transaction.
Example 1:
BEGIN;
INSERT INTO BankStatements (
customer_id,
full_name,
balance
)
VALUES(
4, 'Priya chetri', 500
);
COMMIT;
Output
2. COMMITCOMMIT command is used to save changes and reflect them in the database whenever we display the required data. For suppose we updated data in the database but we didn't give COMMIT then the changes are not reflected in the database. To save the changes done in a transaction, we should COMMIT that transaction for sure.
Example 2:
BEGIN;
UPDATE BankStatements
SET balance = balance - 500
WHERE
customer_id = 1;SELECT customer_id, full_name, balance
FROM BankStatements;
UPDATE BankStatements
SET balance = balance + 500
WHERE
customer_id = 2;
COMMIT;SELECT customer_id, full_name, balance
FROM BankStatements;
Output:
3. ROLLBACKROLLBACK command is used to undo the changes done in transactions. As we know transactions in database languages are used for purpose of large computations, for example in banks. For suppose, the employee of the bank incremented the balance record of the wrong person mistakenly then he can simply rollback and can go to the previous state.
Example 3
BEGIN;
DELETE FROM BankStatements
WHERE
customer_id = 1;
SELECT customer_id, full_name, balance
FROM BankStatements;
ROLLBACK;
SELECT customer_id, full_name, balance
FROM BankStatements;
Output:
ConclusionTransactions are a fundamental aspect of managing data integrity in PostgreSQL. Developers can ensure that their database operations are safe, consistent, and reliable by effectively using transactions. Understanding how to implement and utilize these transaction commands within PostgreSQL can significantly enhance database management and operational stability.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4