Install & Compatibility
Where this runs
tested against v1.3.8 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 88.5MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 2.5s · import 0.014s · 89MB
87MB installed
● package 87MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
psycopg2
✓ import psycopg2
✗ import psycopg2
This quickstart demonstrates how to establish a connection to a PostgreSQL database using `aws-psycopg2` in a Lambda-compatible manner. It retrieves database credentials from environment variables, which is a common practice in serverless environments.
import psycopg2
import os
host = os.environ.get('DB_HOST', 'localhost')
database = os.environ.get('DB_NAME', 'mydatabase')
user = os.environ.get('DB_USER', 'myuser')
password = os.environ.get('DB_PASSWORD', 'mypassword')
try:
conn = psycopg2.connect(host=host, database=database, user=user, password=password)
cur = conn.cursor()
cur.execute('SELECT version();')
db_version = cur.fetchone()
print(f"PostgreSQL database version: {db_version}")
cur.close()
conn.close()
except Exception as e:
print(f"Error connecting to the database: {e}")
Debug
Known issues
gotchaStandard `pip install psycopg2` or `psycopg2-binary` will likely fail in AWS Lambda functions due to missing underlying C libraries (e.g., libpq.so) in the Lambda execution environment. `aws-psycopg2` specifically solves this by providing a statically linked version.fixUse `aws-psycopg2` for pre-compiled binaries, or create a custom Lambda layer with `psycopg2-binary` compiled for Amazon Linux (e.g., using `--platform manylinux2014_x86_64`).
affects: All versions of standard psycopg2/psycopg2-binary when deployed to AWS Lambda without custom layers or compilation.
gotchaWhen deploying to AWS Lambda, ensure the Python version of `aws-psycopg2` (or any `psycopg2` layer) matches your Lambda function's runtime. Mismatched Python versions can lead to 'No module named psycopg2._psycopg' or other import errors.fixAlways install `aws-psycopg2` (or build your layer) specifically for the target Python runtime of your Lambda function (e.g., Python 3.9, 3.10, 3.11). Ensure your deployment method correctly places the library for the chosen runtime.
affects: All versions
gotchaUsing `aws-psycopg2` (or any `psycopg2` variant) in AWS Lambda often requires packaging it correctly, typically within a Lambda layer or as part of a container image deployment. Simply zipping a local `pip install` from a non-Amazon Linux environment will lead to runtime errors.fixDeploy `aws-psycopg2` via a Lambda layer (recommended) or a Docker container image. If packaging directly into a `.zip`, ensure it's built in an Amazon Linux environment (or cross-compiled) to match Lambda's runtime environment.
affects: All versions
Upgrade
Version history
1.3.8latest on PyPI · released Jul 2, 2022
Audit
Dependencies
psycopg2requiredThis package is a pre-compiled version of psycopg2 for AWS Lambda, statically linking PostgreSQL client libraries.