examples/using-cloudsql/README.md
This GoFr example connects to a Google Cloud SQL instance using the
cloudsql datasource. On GCP it
authenticates with IAM database authentication — no static database password
and no Cloud SQL Auth Proxy sidecar — while still supporting ordinary
username/password auth for local development.
The
cloudsqldatasource supports both Postgres and MySQL. This example is written for Postgres (it uses a$1placeholder and aSERIALschema). To run it against MySQL, change the placeholder to?and use anAUTO_INCREMENTschema.
The Cloud SQL datasource is plugged in with app.AddSQLDB, so app.SQL() /
ctx.SQL and all of GoFr's SQL logging, metrics and health checks work exactly
as they do with the built-in SQL datasource.
The same one line works in both environments — only configuration changes. With
DB_IAM_AUTH=true it uses IAM auth; otherwise it uses a standard username/password
connection.
app := gofr.New()
app.AddSQLDB(cloudsql.New(app.Config))
All settings come from environment variables (see configs/.env):
| Variable | Description |
|---|---|
DB_HOST | Instance connection name, project:region:instance |
DB_DIALECT | postgres or mysql |
DB_NAME | Database name |
DB_USER | IAM principal (SA email without .gserviceaccount.com) or DB user |
DB_IAM_AUTH | true for IAM auth, false for username/password |
DB_PASSWORD | Only used when DB_IAM_AUTH=false |
DB_CLOUDSQL_IP_TYPE | PUBLIC, PRIVATE or PSC (defaults to PUBLIC) |
gcloud auth application-default login.CREATE TABLE customers (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL
);
go run main.go
Then:
curl -X POST http://localhost:8000/customers -d '{"name":"alice"}'
curl http://localhost:8000/customers
For local development without GCP, set DB_IAM_AUTH=false, DB_USER=postgres
and DB_PASSWORD=... in configs/.env, pointing DB_HOST at a Cloud SQL
instance you can reach.