I'm running a test Python script that uploads a small amount of data to a PostgreSQL database using SQL Alchemy. Apache Airflow (hosted by Google Cloud Composer) is running this script on a schedule. The script always runs totally fine whenever I run it on Airflow locally and connect directly to the DB.
When I run it on Cloud Composer however, I need to connect through Google's Cloud SQL Proxy for security reasons.
Here's the weird part - the script runs fine about 50% of the time when connecting through Google's cloud-sql-proxy. The other 50% of the time I get the error (psycopg2.OperationalError) FATAL: password authentication failed for user "XXXX
. And then usually it retries and it connects just fine.
I'm assuming it's probably something to do with how I am managing my connection (how it's being opened, pooled, or closed) so here is how I'm doing that. I'm using SQLAlchemy in my Python script like so:
from sqlalchemy import create_engine
engine = create_engine(f"postgresql://{USER}:{PASS}@{HOST}/{DB_NAME}")
conn = engine.connect()
# Upload a bit of data here
conn.close()
engine.dispose()
I've always used this way of uploading data locally so I'm a bit confused why that wouldn't work with Google Cloud Proxy / Google Cloud Composer (hosted Airflow).