Registry / database / aws-psycopg2

aws-psycopg2

JSON →
library1.3.8pypypi✓ verified 24d ago

aws-psycopg2 is a custom-compiled Python package of the popular psycopg2 PostgreSQL adapter, specifically designed for use in AWS Lambda environments. It addresses the common issue of missing PostgreSQL C libraries in the AWS Lambda Amazon Linux AMI by statically linking libpq.so. The library provides a seamless way to connect Python Lambda functions to PostgreSQL databases. It is actively maintained with releases supporting various Python runtime versions. The current version is 1.3.8.

pip install aws-psycopg2
INSTALL
IMPORT
SIG · AWS-PSYCOPG2
A
aws-psycopg2
databasepythonv1.3.8
Install
2.5s avg
Import
35ms
Disk
87MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
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
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 88.5MB
glibc
py 3.103.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.
fix
Use `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.
fix
Always 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.
fix
Deploy `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.
Agent activity
32 hits · last 30 days
node
28
OpenAI (training)
1
Resources
aws-psycopg2 — pip install aws-psycopg2 · libregistry