This guide shows how to set up Cloud SQL PostgreSQL for the production deployment.
gcloud sql instances create cybersec-db \
--database-version=POSTGRES_15 \
--tier=db-f1-micro \
--region=us-central1 \
--root-password=YOUR_ROOT_PASSWORDOr via Console:
- Go to Cloud SQL → Create Instance → PostgreSQL
- Instance ID:
cybersec-db - Password: Choose a secure password
- Region:
us-central1(same as Cloud Run) - Machine:
db-f1-micro(for demo) or higher for production - Storage: 10GB minimum
# Create database
gcloud sql databases create cybersec_alerts --instance=cybersec-db
# Create user
gcloud sql users create cybersec_user \
--instance=cybersec-db \
--password=YOUR_USER_PASSWORD- Go to Cloud Run → Select your service → Edit & Deploy New Revision
- Under Connections, click Add Connection
- Select your Cloud SQL instance:
cybersec-db - Click Deploy
In Cloud Run service settings, add:
DATABASE_URL=postgresql+psycopg2://cybersec_user:YOUR_USER_PASSWORD@/cybersec_alerts?host=/cloudsql/PROJECT_ID:us-central1:cybersec-db
SECRET_KEY=your-random-secret-key-here
# Optional but recommended:
MAILGUN_API_KEY=your-mailgun-key
MAILGUN_DOMAIN=your-mailgun-domain
FROM_EMAIL=alerts@yourdomain.com
Important: Replace:
YOUR_USER_PASSWORDwith the password you setPROJECT_IDwith your actual GCP project IDyour-random-secret-key-herewith a secure random string
The auto-deploy is already configured. Just push to main:
git push origin mainOr deploy manually:
gcloud run deploy cybersec-saas \
--source . \
--region us-central1 \
--allow-unauthenticatedAfter deployment:
- Check health:
https://YOUR-SERVICE-URL/health - Login with demo credentials:
- Email:
admin@example.com - Password:
password123
- Email:
Development/Demo:
- Use
db-f1-microinstance (~$7/month) - Stop instance when not in use
Production:
- Use
db-customwith appropriate CPU/memory - Enable automatic backups
- Set up High Availability if needed
- Verify Cloud SQL connection is added to Cloud Run
- Check DATABASE_URL format matches Cloud SQL Unix socket format
- Ensure database and user exist
- Database is being auto-seeded on startup
- Check Cloud Run logs for seeding errors
- Verify DATABASE_URL is set correctly
- Check Cloud Run logs for Python errors
- Verify all required dependencies in requirements.txt
- Ensure SECRET_KEY environment variable is set