ADBC PostgreSQL Driver
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
- deprecated The `ADBC:postgresql:typname` metadata key, previously attached to schema fields for unknown columns, has been deprecated in favor of the `Opaque` canonical extension type. Users should not rely on this key's continued existence.
- gotcha The PostgreSQL driver uses `COPY` for optimal performance in query execution by default. However, this optimization is not supported for all queries (e.g., `SHOW` queries). Such queries may fail or produce unexpected results.
- gotcha The PostgreSQL ADBC driver's support for prepared statements with parameters is currently limited to queries that *do not* return result sets (e.g., `INSERT`, `UPDATE`). This is due to the driver's reliance on the `COPY` protocol for performance, which is not compatible with parameterized `SELECT` statements.
- gotcha There are known limitations and specific behaviors in type mapping between PostgreSQL and Arrow/Python types. Notably, PostgreSQL `NUMERIC` types are read as their string representation, and time zone information in `timestamp` values is ignored during binding.
- gotcha Failure to explicitly close connections and cursors can lead to resource leaks (e.g., database connections remaining open indefinitely). While the Python driver manager attempts to close unclosed cursors when a connection is closed, explicit management is best practice.
Install
-
pip install adbc-driver-postgresql -
mamba install adbc-driver-postgresql -
# Note: This Python package requires an underlying C/C++ ADBC PostgreSQL driver (libadbc_driver_postgresql.so/.dll/.dylib) to be available in your system's library path or specified via ADBC_POSTGRESQL_LIBRARY environment variable. Standard pip/conda installations often handle this for common platforms. export ADBC_POSTGRESQL_LIBRARY=/path/to/libadbc_driver_postgresql.so
Imports
- connect
import adbc_driver_postgresql.dbapi conn = adbc_driver_postgresql.dbapi.connect(...)
Quickstart
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.")