Connecting to a PostgreSQL DB on CloudSQL via Python server running on Cloud Run

Once we have our PostgreSQL database running on CloudSQL, with the flag cloudsql.iam_authentication set to on. We can now have a look at how we could create a Python FastAPI backend that connects to the database.

Let's start by creating a project folder and Python environment.

mkdir cloudsql-pg-connector
cd cloudsql-pg-connector
python3 -m venv env
source env/bin/activate

We now have our project ready for the installation of packages and coding.

Start by installing the needed packages.

pip install fastapi "cloud-sql-python-connector[pg8000]" SQLAlchemy gunicorn

Write a comment ...

Vishnu Dileesh

Show your support

Hey there! 👋 I love sharing my coding journey, insights, and tutorials to help fellow developers grow. If you've found my content valuable, consider supporting my work! Your contributions help me keep creating quality content, explore new topics, and share more useful resources with the community. Thanks for your support! ❤️

Write a comment ...