Managing FastAPI Projects with Poetry: A Step-by-Step Guide

In the early stages of development on Google Cloud Platform (GCP), I needed to directly connect to the production Cloud SQL (PostgreSQL) instance for debugging and testing. Although Cloud SQL does provide a public IP address, it requires registering the client's IP address for access, so I decided to look for another way.
Due to GCP’s strict security policies—such as requiring IAM authentication and often disabling public IP access—direct access can be challenging.
That's where the Cloud SQL Auth Proxy comes in. It creates a secure tunnel from my local machine to GCP’s Cloud SQL instance using IAM credentials.
⚠️ Important: This approach is suitable for early-stage development to improve speed and flexibility. However, once your development environment is more mature or moved into a shared or production stage, you should stop using this method and adopt more secure alternatives.
┌────────────────────┐
│ Dev Machine │
│ psql client │
│ (192.168.1.123) │
└────────────────────┘
↓
┌─────────────────────┐
│ Ubuntu 24.04 laptop │
│ cloud-sql-proxy │ ← IAM authentication
│ (192.168.1.456) │
└─────────────────────┘
↓
┌────────────────────────────┐
│ GCP Cloud SQL (PostgreSQL) │
│ (Private IP, no public IP) │
└────────────────────────────┘
Before using the Cloud SQL Auth Proxy, I needed to authenticate my local environment with Google Cloud using Application Default Credentials. This generates a credentials file that the proxy will use to connect to the Cloud SQL instance securely.
$ gcloud auth application-default login
After logging in through the browser, the following file is created:
~/.config/gcloud/application_default_credentials.json
The proxy uses this file for authentication when launched with the --credentials-file
flag.
On my Ubuntu 24.04 laptop, I installed the proxy with the following commands:
$ curl -o cloud-sql-proxy https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.16.0/cloud-sql-proxy.linux.amd64
$ chmod +x cloud-sql-proxy
$ sudo mv cloud-sql-proxy /usr/local/bin/
$ cloud-sql-proxy --version
cloud-sql-proxy version 2.16.0+linux.amd64
I created a systemd service to automatically start the proxy on boot:
$ sudo vim /etc/systemd/system/cloud-sql-proxy.service
Create a new systemd service file with the following configuration:
[Unit]
Description=Cloud SQL Auth Proxy
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/cloud-sql-proxy \
--credentials-file=/home/gcp/.config/gcloud/application_default_credentials.json \
--address=0.0.0.0 \
--port=5432 \
your-project-id:your-region:your-instance-name
Restart=on-failure
User=gcp
WorkingDirectory=/home/gcp
[Install]
WantedBy=multi-user.target
Note: Replace User=gcp
with your actual Linux username (whoami
will tell you). Make sure this user has access to the credentials file at ~/.config/gcloud/application_default_credentials.json.
127.0.0.1
. Use --address=0.0.0.0
to allow connections from other devices on your local network.
$ sudo systemctl daemon-reexec
$ sudo systemctl daemon-reload
$ sudo systemctl enable cloud-sql-proxy
$ sudo systemctl start cloud-sql-proxy
$ sudo systemctl status cloud-sql-proxy
[sudo] password for gcp:
● cloud-sql-proxy.service - Cloud SQL Auth Proxy
Loaded: loaded (/etc/systemd/system/cloud-sql-proxy.service; enabled; preset: enabled)
Active: active (running) since Thu 2025-05-15 13:47:18 PDT; 1h 17min ago
Main PID: 64281 (cloud-sql-proxy)
Tasks: 13 (limit: 18846)
Memory: 8.8M (peak: 11.9M)
CPU: 495ms
CGroup: /system.slice/cloud-sql-proxy.service
...
$ sudo ss -tulnp | grep 5432
tcp LISTEN 0 4096 *:5432 *:* users:(("cloud-sql-proxy",pid=64281,fd=6))
If you're using UFW (Uncomplicated Firewall), allow port 5432:
$ sudo ufw status
$ sudo ufw allow 5432/tcp
$ psql -h 192.168.1.456 -U appuser -d testdb
$ cloud-sql-proxy \
--credentials-file=~/.config/gcloud/application_default_credentials.json \
--address=0.0.0.0 \
--port=5432 \
your-project-id:your-region:your-instance-name
With the Cloud SQL Auth Proxy running on my Ubuntu laptop, I’m now able to securely access my Cloud SQL instance from any device on my local network. This setup offers the convenience and flexibility needed for active development while still aligning with GCP’s security standards.
Comments
Post a Comment