pg8000: Pure-Python PostgreSQL Driver

1.31.5 · active · verified Sun Mar 29

pg8000 is a pure-Python PostgreSQL driver that fully complies with the DB-API 2.0 specification. It distinguishes itself by being written entirely in Python, eliminating the need for external C libraries like `libpq`, which enhances portability and simplifies installation. The library provides both a standard DB-API 2.0 interface and a native API for streamlined usage. Currently at version 1.31.5, it is actively maintained with regular updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection using the DB-API 2.0 compliant interface, perform DDL and DML operations with parameterized queries, and fetch results. It also includes basic error handling and uses environment variables for secure credential management.

import pg8000.dbapi
import os

# Database connection details from environment variables
DB_USER = os.environ.get("PG_USER", "postgres")
DB_PASSWORD = os.environ.get("PG_PASSWORD", "password")
DB_HOST = os.environ.get("PG_HOST", "localhost")
DB_PORT = int(os.environ.get("PG_PORT", "5432"))
DB_DATABASE = os.environ.get("PG_DATABASE", "testdb")

conn = None
try:
    # Establish a DB-API 2.0 compliant connection
    conn = pg8000.dbapi.connect(
        user=DB_USER,
        password=DB_PASSWORD,
        host=DB_HOST,
        port=DB_PORT,
        database=DB_DATABASE
    )
    cursor = conn.cursor()

    # Create a table (if it doesn't exist)
    cursor.execute("CREATE TABLE IF NOT EXISTS mytable (id SERIAL PRIMARY KEY, name VARCHAR(100))")
    conn.commit() # Commit the DDL operation

    # Insert data using a parameterized query
    cursor.execute("INSERT INTO mytable (name) VALUES (%s)", ("pg8000_test",))
    conn.commit() # Commit the DML operation

    # Query data
    cursor.execute("SELECT id, name FROM mytable WHERE name = %s", ("pg8000_test",))
    result = cursor.fetchone()
    print(f"Fetched: {result}")

except pg8000.dbapi.Error as e:
    print(f"Database error: {e}")
    if conn:
        conn.rollback() # Rollback on database errors
except Exception as e:
    print(f"An unexpected error occurred: {e}")
finally:
    if conn:
        conn.close() # Ensure connection is closed

view raw JSON →