Category: python
Connect Python with AWS RDS
Published on 15 Sep 2026
Explanation
Amazon RDS provides managed relational databases such as PostgreSQL and MySQL.
Code:
rds = {'engine': 'PostgreSQL', 'database': 'appdb', 'port': 5432}
print(rds)
Explanation
Python needs a database driver to communicate with the selected RDS engine.
Code:
pip install psycopg2-binary
Explanation
Use the database endpoint and credentials to create a PostgreSQL connection.
Code:
import psycopg2 connection = psycopg2.connect(host='rds-endpoint', database='appdb', user='appuser', password='secret')
Explanation
Python can execute SQL queries and retrieve records from RDS.
Code:
with connection.cursor() as cursor:
cursor.execute('SELECT id, name FROM users')
print(cursor.fetchall())
Explanation
Keep RDS private, restrict security groups, protect credentials, and enable encryption.
Code:
rules = ['Private subnet', 'Restricted access', 'Encrypted connection', 'Secure credentials'] print(rules)