Add ssl=true
to your DATABASE_URL
Download certificate that is required for secure connection with RDS e.g. AWS docs
Add required arguments while creating a session
import ssl
from sqlalchemy.ext.asyncio import (
AsyncSession,
create_async_engine
)
from sqlalchemy.orm import sessionmaker
RDS_CERT_PATH = "path/to/your/cert/file"
DATABASE_URL = "postgresql://x:y@z:5432/psql?ssl=true"
ssl_context = ssl.create_default_context(cafile=RDS_CERT_PATH)
ssl_context.verify_mode = ssl.CERT_REQUIRED
connect_args = {"ssl": ssl_context}
engine = create_async_engine(
DATABASE_URL,
connect_args=connect_args,
)
async_session = sessionmaker(
engine,
class_=AsyncSession
)
This setup should make the work done, enjoy!