Connecting Python to a Postgresql database
July 8, 2025
Discover how to connect Python to a Postgresql database without storing credentials or keys on filesystem.

Connecting Python to Postgresql is made seamless with the psycopg2 library. This article demonstrates how to connect to a Postgresql database using Python without storing credentials in plain text or in the .pgpass file. The architecture we will be using consists of a Postgresql database hosted on an Ubuntu 24 (Noble) Amazon Web Services (AWS) EC2 instance and Python 3.10 running on Ubuntu 22 (Jammy). We will use a symmetric AWS KMS key to encrypt and decrypt database credentials.
Requirements
- Python 3 (preferably running on Ubuntu)
- Python libraries: psycopg2, boto3
- AWS: Key Management Service (KMS) Customer managed symmetric key
- Postgresql database cluster (needs to accept TCP/IP connections)
Get database connection parameters
First we will need to know the database host and database name which will be used to create a config file with .ini extension in the user's home directory. Then we will prompt for the user name and password using Python's getpass to avoid echoing back the data input by the user. We'll use our AWS KMS key to perform encryption of the user name and password. Lastly the encrypted data will be written to the config file w/ permissions set to non-readable, non-writable, and non-executable. The following code shows how we can do the aforementioned in Python.
Connecting to the postgresql database
Now that we have the connection config file created we are ready to connect to the postgresql database cluster and begin sql'ing away. The below aws_postgresql_connect.py program reads the encrypted data from the config file created by the aws_postgresql_connect_init.py program then decrypts the byte data with the same AWS KMS key. It is important to include the encryption algorithm (EncryptionAlgorithm='RSAES_OAEP_SHA_256') in the decrypt call if using an asymmetric key in AWS KMS. In this case we are using a symmetric key so the EncryptionAlgorithm='SYMMETRIC_DEFAULT' encryption algorithm property isn't needed but we still include it for posterity sake. The data read is then split by delimiter to derive user name/password and the config file permissions are disabled. Finally we use the imported psycopg2 libarary to connect to our postgresql cluster in AWS EC2 and run a sample query.
Key Takeaways
In this article we have connected to a postgresql database cluster hosted in AWS EC2 without storing database credentials or encryption keys anywhere on the filesystem. Although the code presented could be refactored to eliminate redundancies or abstracted into a class, the key points listed below could help in securely connecting to databases.
- Minimize visibility and access to database credentials
- If hosting in a cloud service provider, make use of symmetric keys to protect sensitive data such as user names or passwords
- Use encryption when storing database credentials on filesystem
- Try to avoid storing encryption keys or plain text user names/passwords