Integration
PostgreSQL Integration Application Integration
python
# Python psycopg2 integration
import psycopg2
from psycopg2.extras import RealDictCursor
def connect_to_postgresql():
try:
conn = psycopg2.connect(
host="localhost",
database="myapp",
user="postgres",
password="password",
port=5432
)
cursor = conn.cursor(cursor_factory=RealDictCursor)
# Execute query
cursor.execute("SELECT version();")
version = cursor.fetchone()
print(f"PostgreSQL version: {version}")
return conn, cursor
except Exception as error:
print(f"Error connecting to PostgreSQL: {error}")
return None, None
Connection Pooling
python
# Using connection pooling
from psycopg2 import pool
connection_pool = psycopg2.pool.SimpleConnectionPool(
1, 20,
user="postgres",
password="password",
host="localhost",
port="5432",
database="myapp"
)
# Get connection from pool
conn = connection_pool.getconn()
cursor = conn.cursor()
# Return connection to pool
connection_pool.putconn(conn)
Replication Setup
bash
# Master server configuration
wal_level = replica
archive_mode = on
archive_command = 'cp %p /var/lib/postgresql/17/main/archive/%f'
max_wal_senders = 3
wal_keep_segments = 64
# Replica server setup
standby_mode = 'on'
primary_conninfo = 'host=master-server port=5432 user=replicator'
restore_command = 'cp /var/lib/postgresql/17/main/archive/%f %p'