Psycopg2 and PostgreSQL Integration Guide
Psycopg2 is a popular Python adapter for PostgreSQL, enabling seamless interaction between Python applications and PostgreSQL databases. It offers advanced features like connection pooling, server-side cursors, and thread safety, making it suitable for both simple and complex database tasks.
This guide provides an in-depth explanation of using Psycopg2 to connect to a PostgreSQL database, execute queries, and manage database transactions effectively.
Syntax and Basic Usage
Installation
Install Psycopg2 using pip:
pip install psycopg2
For faster binary installation:
pip install psycopg2-binary
Connecting to PostgreSQL
The connect method is used to establish a connection:
import psycopg2 # Establish connection connection = psycopg2.connect( dbname="your_database", # Name of the database user="your_user", # Database username password="your_password", # Database password host="localhost", # Hostname port="5432" # Port number )
Code Example: Performing Basic Database Operations
1. Connecting and Querying
Code:
# Import psycopg2 module
import psycopg2
try:
# Establish connection to PostgreSQL
connection = psycopg2.connect(
dbname="sample_db", # Name of the database
user="admin", # Username for the database
password="admin_password", # Password for the database
host="localhost", # Database server
port="5432" # Default PostgreSQL port
)
# Create a cursor object to execute SQL queries
cursor = connection.cursor()
# Execute a SQL query
cursor.execute("SELECT version();") # Fetch PostgreSQL version
# Retrieve and display the query result
version = cursor.fetchone()
print("PostgreSQL version:", version)
except Exception as e:
print("Error:", e) # Print any connection or query errors
finally:
# Close the cursor and connection
if cursor:
cursor.close()
if connection:
connection.close()
2. Inserting and Fetching Data
Code:
try:
# Establish the database connection
connection = psycopg2.connect(
dbname="sample_db",
user="admin",
password="admin_password",
host="localhost",
port="5432"
)
# Start a cursor session
cursor = connection.cursor()
# Create a table if not exists
cursor.execute("""
CREATE TABLE IF NOT EXISTS employees (
id SERIAL PRIMARY KEY,
name VARCHAR(50),
age INT,
department VARCHAR(50)
);
""")
# Insert data into the table
cursor.execute("""
INSERT INTO employees (name, age, department)
VALUES (%s, %s, %s)
""", ("Alice", 30, "HR"))
# Commit the transaction
connection.commit()
# Fetch and display data
cursor.execute("SELECT * FROM employees;")
rows = cursor.fetchall()
for row in rows:
print(row)
except Exception as e:
print("Error:", e)
finally:
# Ensure resources are cleaned up
if cursor:
cursor.close()
if connection:
connection.close()
Explanation
1. Connection Establishment:
2. Query Execution:
3. Transaction Management:
4. Resource Management:
Advanced Features
Common Errors and Solutions
1. Error: psycopg2.OperationalError
2. Error: psycopg2.DatabaseError
All PostgreSQL Questions, Answers, and Code Snippets Collection.
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