ADBC PostgreSQL Driver

1.11.0 · active · verified Sat Apr 11

The `adbc-driver-postgresql` package provides Python bindings for a libpq-based ADBC (Arrow Database Connectivity) driver for PostgreSQL. It offers a DBAPI 2.0-compatible interface, enabling high-performance data access to PostgreSQL databases by leveraging Apache Arrow for columnar data interchange. The library is currently active, with version 1.11.0, and typically releases in conjunction with the broader Apache Arrow ADBC project.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to a PostgreSQL database using `adbc-driver-postgresql.dbapi`, execute DDL and DML statements, and fetch results as both an Apache Arrow Table and a single row. It assumes a PostgreSQL instance is accessible via the provided URI, preferably set via an environment variable for security.

import os
import adbc_driver_postgresql.dbapi

# Configure your PostgreSQL connection URI.
# For example, using a local Dockerized PostgreSQL with default credentials:
# docker run -it --rm -e POSTGRES_PASSWORD=password -e POSTGRES_DB=testdb -p 5432:5432 postgres:latest
# export ADBC_POSTGRESQL_TEST_URI="postgresql://postgres:password@localhost:5432/testdb"
uri = os.environ.get("ADBC_POSTGRESQL_TEST_URI", "postgresql://user:password@localhost:5432/mydb")

try:
    with adbc_driver_postgresql.dbapi.connect(uri) as conn:
        with conn.cursor() as cur:
            # DDL operations
            cur.execute("DROP TABLE IF EXISTS example_adbc;")
            cur.execute("CREATE TABLE example_adbc (id INTEGER, name VARCHAR(50));")

            # DML operations with bind parameters
            cur.executemany("INSERT INTO example_adbc VALUES (?, ?);", [(1, 'Alice'), (2, 'Bob')])
            conn.commit() # Commit changes for DML

            # Query data and fetch as an Arrow Table (requires pyarrow)
            cur.execute("SELECT id, name FROM example_adbc WHERE id > 0;")
            result_table = cur.fetch_arrow_table()
            print("Fetched Arrow Table:")
            print(result_table)

            # Query data and fetch a single row
            cur.execute("SELECT id, name FROM example_adbc WHERE id = 1;")
            first_row = cur.fetchone()
            print(f"\nFetched one row: {first_row}")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Ensure your PostgreSQL database is running and ADBC_POSTGRESQL_TEST_URI is set correctly.")

view raw JSON →