This guide explains how to deploy pgAdmin to Google Cloud Run using Cloud Build.
-
Google Cloud SDK: Install and authenticate with
gcloudgcloud auth login gcloud auth application-default login
-
Project ID: Set your Google Cloud Project ID
export PROJECT_ID="your-project-id"
-
APIs Enabled: The deployment script will enable these automatically, or you can enable them manually:
- Cloud Build API
- Cloud Run API
- Artifact Registry API
- Cloud Storage API
-
Cloud Storage Bucket: A Cloud Storage bucket for persistent pgAdmin data (will be created by the script if it doesn't exist, or you can create it manually)
-
Service Accounts: Cloud Run service account with permissions to access the Cloud Storage bucket (the deployment uses service accounts for secure access)
-
Set your project ID (required):
export PROJECT_ID="your-project-id"
-
(Optional) Customize deployment settings:
export REGION="us-central1" export REPO_NAME="docker-repo" export SERVICE_NAME="pgadmin" export BUCKET_NAME="pgadmin-bucket-name" export SERVICE_ACCOUNT="pgadmin-service-account@project.iam.gserviceaccount.com" export CLOUD_BUILD_SERVICE_ACCOUNT="cloudbuild@project.iam.gserviceaccount.com"
-
Run the deployment script:
./deploy.sh
The script will:
- Enable required APIs
- Create Artifact Registry repository if needed
- Build and push the Docker image to Artifact Registry
- Deploy to Cloud Run with Cloud Storage volume mount for persistent data
- Set pgAdmin credentials after deployment:
gcloud run services update pgadmin \ --region=us-central1 \ --set-env-vars="PGADMIN_DEFAULT_EMAIL=admin@example.com,PGADMIN_DEFAULT_PASSWORD=your-password"
gcloud builds submit \
--config=cloudbuild.yaml \
--substitutions="_REGION=us-central1,_REPO_NAME=docker-repo,_SERVICE_NAME=pgadmin,_MIN_INSTANCES=0,_MAX_INSTANCES=10,_BUCKET_NAME=your-bucket-name,_SERVICE_ACCOUNT=your-service-account@project.iam.gserviceaccount.com"You can customize the deployment by setting environment variables before running the script:
export REGION="us-central1" # GCP region
export REPO_NAME="docker-repo" # Artifact Registry repo name
export SERVICE_NAME="pgadmin" # Cloud Run service name
export MIN_INSTANCES="0" # Minimum instances (0 for scale-to-zero)
export MAX_INSTANCES="10" # Maximum instances
export BUCKET_NAME="your-bucket-name" # Cloud Storage bucket for pgAdmin data
export SERVICE_ACCOUNT="service-account@project.iam.gserviceaccount.com" # Cloud Run service account
export CLOUD_BUILD_SERVICE_ACCOUNT="cloudbuild@project.iam.gserviceaccount.com" # Cloud Build service account (for impersonation)Note: pgAdmin credentials (email and password) should be configured via Cloud Run environment variables after deployment, or set during the initial deployment using Cloud Run environment variables. See the "Setting pgAdmin Credentials" section below.
If you prefer to deploy manually:
gcloud services enable \
cloudbuild.googleapis.com \
run.googleapis.com \
artifactregistry.googleapis.comgcloud artifacts repositories create docker-repo \
--repository-format=docker \
--location=us-central1 \
--description="Docker repository for pgAdmin"Create a Cloud Storage bucket for pgAdmin persistent data:
gsutil mb -p ${PROJECT_ID} -l us-central1 gs://pgadmin-bucket-nameCreate a service account for Cloud Run:
gcloud iam service-accounts create pgadmin-service-account \
--display-name="pgAdmin Cloud Run Service Account"Grant the service account permissions to access the Cloud Storage bucket:
gsutil iam ch serviceAccount:pgadmin-service-account@${PROJECT_ID}.iam.gserviceaccount.com:objectAdmin gs://pgadmin-bucket-namegcloud builds submit \
--config=cloudbuild.yaml \
--substitutions="_REGION=us-central1,_REPO_NAME=docker-repo,_SERVICE_NAME=pgadmin,_MIN_INSTANCES=0,_MAX_INSTANCES=10,_BUCKET_NAME=pgadmin-bucket-name,_SERVICE_ACCOUNT=pgadmin-service-account@${PROJECT_ID}.iam.gserviceaccount.com"Set environment variables for pgAdmin login:
gcloud run services update pgadmin \
--region=us-central1 \
--set-env-vars="PGADMIN_DEFAULT_EMAIL=admin@example.com,PGADMIN_DEFAULT_PASSWORD=your-password"After deployment, get your service URL:
gcloud run services describe pgadmin \
--region=us-central1 \
--format="value(status.url)"Then access pgAdmin in your browser using:
- URL: The URL from above
- Email: The email you configured (default:
admin@example.com) - Password: The password you configured
pgAdmin requires email and password to be set via environment variables. You can set these during or after deployment:
Add environment variables when deploying:
gcloud run deploy pgadmin \
--image=your-image \
--region=us-central1 \
--set-env-vars="PGADMIN_DEFAULT_EMAIL=admin@example.com,PGADMIN_DEFAULT_PASSWORD=your-password"Update the service with credentials:
gcloud run services update pgadmin \
--region=us-central1 \
--set-env-vars="PGADMIN_DEFAULT_EMAIL=admin@example.com,PGADMIN_DEFAULT_PASSWORD=your-password"For better security, use Google Secret Manager:
# Create secret
echo -n "your-password" | gcloud secrets create pgadmin-password --data-file=-
# Update service to use secret
gcloud run services update pgadmin \
--region=us-central1 \
--update-secrets="PGADMIN_DEFAULT_PASSWORD=pgadmin-password:latest"This deployment uses Cloud Storage buckets mounted as volumes to persist pgAdmin data. This ensures that:
- Server registrations are preserved
- User settings are maintained
- Data persists across container restarts and deployments
The Cloud Storage bucket is mounted at /var/lib/pgadmin in the container.
-
Use Secrets Manager for Passwords: See the "Setting pgAdmin Credentials" section above for details on using Secret Manager.
-
Enable Authentication (remove
--allow-unauthenticated):gcloud run services update pgadmin \ --region=us-central1 \ --no-allow-unauthenticated
-
Use HTTPS: Cloud Run automatically provides HTTPS for your service
-
Service Account Permissions: Ensure the Cloud Run service account has only the minimum required permissions (objectAdmin on the Cloud Storage bucket)
-
Secure Cloud Storage Bucket: Configure bucket-level permissions and consider enabling bucket versioning for data protection
- Check that all required APIs are enabled
- Verify your gcloud authentication:
gcloud auth list - Check Cloud Build logs in the Google Cloud Console
- Verify environment variables are set correctly (especially
PGADMIN_DEFAULT_EMAILandPGADMIN_DEFAULT_PASSWORD) - Check Cloud Run logs:
gcloud run services logs read pgadmin --region=us-central1 - Ensure PORT is set to 8080 (already configured in Dockerfile)
- Verify the Cloud Storage bucket exists and is accessible
- Check that the service account has proper permissions to access the bucket
- Verify the service is deployed:
gcloud run services list - Check IAM permissions if authentication is enabled
- Verify the service URL is correct
- Ensure pgAdmin credentials are set correctly
- Verify the bucket exists:
gsutil ls gs://your-bucket-name - Check service account permissions:
gsutil iam get gs://your-bucket-name - Ensure the service account has
objectAdminrole on the bucket
To update the service with new code:
# Just run the deployment script again
./deploy.shOr rebuild and redeploy:
gcloud builds submit --config=cloudbuild.yamlTo remove the service:
gcloud run services delete pgadmin --region=us-central1To remove the Artifact Registry repository:
gcloud artifacts repositories delete docker-repo --location=us-central1To remove the Cloud Storage bucket (
gsutil rm -r gs://your-bucket-nameOr to delete the bucket and all contents:
gsutil rb gs://your-bucket-nameTo remove the service account:
gcloud iam service-accounts delete pgadmin-service-account@${PROJECT_ID}.iam.gserviceaccount.com