{"id":2397,"library":"aws-psycopg2","title":"AWS Psycopg2","description":"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.","status":"active","version":"1.3.8","language":"en","source_language":"en","source_url":"https://github.com/AbhimanyuHK/aws-psycopg2","tags":["aws","lambda","postgresql","psycopg2","database","serverless"],"install":[{"cmd":"pip install aws-psycopg2","lang":"bash","label":"Install via pip"}],"dependencies":[{"reason":"This package is a pre-compiled version of psycopg2 for AWS Lambda, statically linking PostgreSQL client libraries.","package":"psycopg2"}],"imports":[{"note":"The 'aws-psycopg2' package provides the standard 'psycopg2' module. You import 'psycopg2' directly, not from 'aws_psycopg2'.","wrong":"from aws_psycopg2 import connect","symbol":"psycopg2","correct":"import psycopg2"}],"quickstart":{"code":"import psycopg2\nimport os\n\nhost = os.environ.get('DB_HOST', 'localhost')\ndatabase = os.environ.get('DB_NAME', 'mydatabase')\nuser = os.environ.get('DB_USER', 'myuser')\npassword = os.environ.get('DB_PASSWORD', 'mypassword')\n\ntry:\n    conn = psycopg2.connect(host=host, database=database, user=user, password=password)\n    cur = conn.cursor()\n    cur.execute('SELECT version();')\n    db_version = cur.fetchone()\n    print(f\"PostgreSQL database version: {db_version}\")\n    cur.close()\n    conn.close()\nexcept Exception as e:\n    print(f\"Error connecting to the database: {e}\")\n","lang":"python","description":"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."},"warnings":[{"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`).","message":"Standard `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.","severity":"gotcha","affected_versions":"All versions of standard psycopg2/psycopg2-binary when deployed to AWS Lambda without custom layers or compilation."},{"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.","message":"When 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"Using `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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}