Production-ready Psycopg layers for AWS Lambda supporting Python 3.9 through 3.13.
Download the latest layer for your Python version:
| Python Version | Download Link | Layer ARN |
|---|---|---|
| 3.9 | Download | arn:aws:lambda:region:account:layer:psycopg-python39:1 |
| 3.10 | Download | arn:aws:lambda:region:account:layer:psycopg-python310:1 |
| 3.11 | Download | arn:aws:lambda:region:account:layer:psycopg-python311:1 |
| 3.12 | Download | arn:aws:lambda:region:account:layer:psycopg-python312:1 |
| 3.13 | Download | arn:aws:lambda:region:account:layer:psycopg-python313:1 |
# Upload layer to AWS Lambda
aws lambda publish-layer-version \
--layer-name psycopg-python313 \
--description "Psycopg layer for Python 3.13" \
--zip-file fileb://python3.13-v1.zip \
--compatible-runtimes python3.13 \
--compatible-architectures x86_64import json
import os
import psycopg2
from psycopg2.extras import RealDictCursor
def lambda_handler(event, context):
# Psycopg2 is now available!
try:
# Connect to PostgreSQL database
conn = psycopg2.connect(
host=os.environ['DB_HOST'],
database=os.environ['DB_NAME'],
user=os.environ['DB_USER'],
password=os.environ['DB_PASSWORD'],
port=os.environ.get('DB_PORT', '5432')
)
# Execute query
with conn.cursor(cursor_factory=RealDictCursor) as cur:
cur.execute("SELECT version();")
result = cur.fetchone()
conn.close()
return {
'statusCode': 200,
'body': json.dumps({
'message': 'Database connection successful!',
'version': result['version']
})
}
except Exception as e:
return {
'statusCode': 500,
'body': json.dumps(f'Error: {str(e)}')
}| Feature | Value |
|---|---|
| Psycopg2 Version | Latest (psycopg2-binary) |
| Layer Size | ~3.6MB (compressed) |
| Architecture | x86_64 |
| PostgreSQL Support | All PostgreSQL versions |
| Python Versions | 3.9, 3.10, 3.11, 3.12, 3.13 |
| Memory Usage | ~171MB (uncompressed) |
- Python 3.9+ installed
- System dependencies for Psycopg2 compilation (psycopg2-binary includes pre-compiled binaries)
# Clone the repository
git clone https://github.com/serverlessia/lambda-psycopg-layer.git
cd lambda-psycopg-layer
# Build layers for all Python versions
./scripts/build-multi-version.sh
# Test all layers
./scripts/test-multi-version.sh# Build for specific Python version (defaults to 3.13)
./scripts/build.shOpen in VS Code DevContainer for automatic multi-version building and testing:
- Open project in VS Code
- Use "Reopen in Container" command
- Layers will be built and tested automatically
- Full PostgreSQL Support: All PostgreSQL features and data types
- Connection Pooling: Compatible with connection pooling libraries
- Extensions: Includes psycopg2.extras and psycopg2.extensions
- Binary Package: Pre-compiled binaries for fast Lambda cold starts
- Error Handling: Full exception hierarchy (Error, DatabaseError, OperationalError, etc.)
| Component | Status | Notes |
|---|---|---|
| Python 3.9 | ✅ Working | Full Psycopg2 support |
| Python 3.10 | ✅ Working | Full Psycopg2 support |
| Python 3.11 | ✅ Working | Full Psycopg2 support |
| Python 3.12 | ✅ Working | Full Psycopg2 support |
| Python 3.13 | ✅ Working | Full Psycopg2 support |
| CI/CD | ✅ Active | Automated builds and releases |
| DevContainer | ✅ Ready | Multi-version support |
MIT License - see LICENSE file for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Test with
./scripts/test-multi-version.sh - Submit a pull request
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Documentation: Wiki