Files
86314dc9d8 fix: avoid apt package when psycopg-binary is used (#379)
* feat: detect psycopg[binary] and skip apt packages

When using psycopg2-binary or psycopg[binary], the PostgreSQL client
libraries (libpq-dev, libpq5) are not needed because these variants
ship with pre-compiled binaries. This change:

- Adds usesBinaryPsycopg() to detect binary variants
- Updates usesPostgres() to return false when binary variants are used
- Skips libpq-dev (build) and libpq5 (runtime) apt package installation
- Adds comprehensive unit tests for binary psycopg detection

This reduces image size and build time for projects using binary variants.

* test: add psycopg[binary] integration test

Adds a dedicated test example for psycopg[binary] to verify that the
bracket syntax for optional dependencies is correctly detected. This
ensures the binary detection works for both psycopg2-binary and
psycopg[binary] variants.

Test coverage now includes:
- psycopg2-binary (existing python-system-deps example)
- psycopg[binary] (new python-psycopg-binary example)
- psycopg non-binary (existing python-latest-psycopg example)
- psycopg2 non-binary (existing python-django example)

* test: snapshot update

* test: add binary pyscogp test

* test: ensure libpq is installed for pyscog operation

* build: add helper for running the current dir tests

* test: check to make sure the libpg library is not installed when a binary postgres connection is requested

* test: update snapshot

---------

Co-authored-by: Hanzo Dev <[email protected]>
2026-01-15 11:32:30 -07:00

40 lines
1.1 KiB
Python

import os
import psycopg
import ctypes.util
print("psycopg version:", psycopg.__version__)
major_version = int(psycopg.__version__.split('.')[0])
if major_version != 3:
print(f"ERROR: Expected psycopg 3.x but got {psycopg.__version__}")
exit(1)
# Verify libpq is installed on the system
libpq_path = ctypes.util.find_library('pq')
if not libpq_path:
print("ERROR: System libpq not found. It should be present for non-binary psycopg.")
exit(1)
print(f"System libpq found")
print("Using psycopg3 - libpq is available")
database_url = os.environ.get("DATABASE_URL")
if not database_url:
print("No DATABASE_URL provided, skipping connection test")
exit(0)
print(f"Connecting to database...")
conn = psycopg.connect(database_url)
print("Successfully connected to PostgreSQL!")
with conn.cursor() as cur:
cur.execute("SELECT version();")
version = cur.fetchone()[0]
print(f"PostgreSQL version: {version}")
cur.execute("SELECT 1 + 1 AS result;")
result = cur.fetchone()[0]
print(f"Test query result: 1 + 1 = {result}")
conn.close()
print("Connection closed successfully")