![]() |
VOOZH | about |
psycopg is a PostgreSQL adapter for Python that enables Python applications to communicate with PostgreSQL databases. It provides built-in transaction management features, allowing multiple SQL statements to be executed as a single unit of work while ensuring data integrity and consistency.
Install the required package using:
pip install --upgrade pip
pip install "psycopg[binary]"
Transaction management allows multiple database operations to be executed as a single logical unit of work. Transactions follow the ACID properties, which help maintain data integrity, consistency, and reliability during database operations.
| Property | Description |
|---|---|
| Atomicity | Ensures all operations in a transaction succeed together. If any operation fails, the entire transaction is rolled back. |
| Consistency | Ensures the database remains in a valid and consistent state before and after a transaction. |
| Isolation | Prevents concurrent transactions from interfering with each other until changes are committed. |
| Durability | Ensures committed changes are permanently saved, even if a system failure occurs. |
Necessary Concepts:
First, create a PostgreSQL database and a table named psy. The psy table contains three columns: id, num, and data, which will be used throughout the examples in this article.
Example: A PostgreSQL connection is established and autocommit mode is enabled to automatically save each SQL statement. A cursor is then used to insert records into the psy table, retrieve the stored data, and display the results, while handling any database-related errors.
Output:
Explanation:
Example: A PostgreSQL connection and cursor are created using psycopg, and SQL queries are executed inside a transaction block. Changes are committed if all operations succeed; otherwise, the transaction is rolled back to maintain database consistency.
Output:
Explanation:
Transaction characteristics control how a transaction behaves while interacting with the database. In psycopg, these properties can be configured using connection attributes such as isolation_level, read_only, and deferrable. If they are not explicitly set, PostgreSQL uses the server's default settings.
1. Isolation Level
The isolation level determines how changes made by one transaction are visible to other concurrent transactions.
2. Deferrable
The deferrable property determines whether constraint checks can be postponed until the transaction is committed.
3. Read Only
The read_only property restricts a transaction to read operations only and prevents any modifications to the database.
Example: A PostgreSQL connection is established using psycopg, and the connection is configured in read-only mode. A cursor is then used to execute SELECT queries and retrieve data from the psy table. Since the transaction is read-only, any attempt to modify the database using INSERT, UPDATE, or DELETE statements will result in an error.
Note: PostgreSQL does not support READ UNCOMMITTED separately and treats it as READ COMMITTED.
Output
Explanation: