This guide walks you through deploying the FastAPI backend to Google Cloud Run and setting up the PostgreSQL database using Google Cloud SQL.
- A Google Cloud account with billing enabled.
- Google Cloud CLI (
gcloud) installed and authenticated. - Docker installed on your machine.
# Login to your Google account
gcloud auth login
# Create a new project (or use an existing one)
gcloud projects create votequest-backend-prod
# Set the active project
gcloud config set project votequest-backend-prod
# Enable required APIs
gcloud services enable run.googleapis.com sqladmin.googleapis.com artifactregistry.googleapis.com cloudbuild.googleapis.comCloud Run needs a database to connect to. We will use Google Cloud SQL.
-
Create the Database Instance:
gcloud sql instances create votequest-db-instance \ --database-version=POSTGRES_14 \ --cpu=1 --memory=3840MB \ --region=us-central1 -
Set the default user password:
gcloud sql users set-password postgres \ --instance=votequest-db-instance \ --password="YOUR_STRONG_PASSWORD" -
Create the VoteQuest database:
gcloud sql databases create votequest_db \ --instance=votequest-db-instance -
Get the Connection Name:
gcloud sql instances describe votequest-db-instance --format="value(connectionName)"(Save this value, it will look like
votequest-backend-prod:us-central1:votequest-db-instance)
We need to upload your backend Docker image to Google Artifact Registry.
-
Create a repository in Artifact Registry:
gcloud artifacts repositories create votequest-repo \ --repository-format=docker \ --location=us-central1 -
Configure Docker to authenticate with GCP:
gcloud auth configure-docker us-central1-docker.pkg.dev
-
Build and push the image:
cd backend # Build the image docker build -t us-central1-docker.pkg.dev/votequest-backend-prod/votequest-repo/backend:latest . # Push to Artifact Registry docker push us-central1-docker.pkg.dev/votequest-backend-prod/votequest-repo/backend:latest
Now, deploy the container to Cloud Run and connect it to your Cloud SQL database.
gcloud run deploy votequest-api \
--image us-central1-docker.pkg.dev/votequest-backend-prod/votequest-repo/backend:latest \
--region us-central1 \
--allow-unauthenticated \
--add-cloudsql-instances="YOUR_CONNECTION_NAME_FROM_STEP_1" \
--set-env-vars="ENVIRONMENT=production" \
--set-env-vars="SECRET_KEY=generate_a_random_secure_string_here" \
--set-env-vars="GEMINI_API_KEY=your_actual_gemini_api_key" \
--set-env-vars="CORS_ORIGINS=[\"https://your-frontend-domain.netlify.app\"]" \
--set-env-vars="DATABASE_URL=postgresql+psycopg2://postgres:YOUR_STRONG_PASSWORD@/votequest_db?host=/cloudsql/YOUR_CONNECTION_NAME_FROM_STEP_1"Note: Make sure to replace YOUR_CONNECTION_NAME_FROM_STEP_1, YOUR_STRONG_PASSWORD, and the CORS_ORIGINS URL with your actual values.
Once the deployment finishes, the terminal will output a Service URL (e.g., https://votequest-api-xyz-uc.a.run.app).
- Copy this URL.
- Open your Netlify Dashboard (or local
.env.localfile). - Set your environment variable:
NEXT_PUBLIC_API_URL=https://votequest-api-xyz-uc.a.run.app
- Rebuild/Redeploy your frontend.