django-cloud-sql-postgres
from jezweb/claude-skills
Skills for Claude Code CLI such as full stack dev Cloudflare, React, Tailwind v4, and AI integrations.
213 stars24 forksUpdated Jan 25, 2026
npx skills add https://github.com/jezweb/claude-skills --skill django-cloud-sql-postgresSKILL.md
Django on Google Cloud SQL PostgreSQL
Status: Production Ready
Last Updated: 2026-01-24
Dependencies: None
Latest Versions: Django@5.1, psycopg2-binary@2.9.9, gunicorn@23.0.0, google-cloud-sql-connector@1.12.0
Quick Start (10 Minutes)
1. Install Dependencies
pip install Django psycopg2-binary gunicorn
For Cloud SQL Python Connector (recommended for local dev):
pip install "cloud-sql-python-connector[pg8000]"
Why this matters:
psycopg2-binaryis the PostgreSQL adapter for Djangogunicornis required for App Engine Standard (Python 3.10+)- Cloud SQL Python Connector provides secure connections without SSH tunneling
2. Configure Django Settings
settings.py (production with Unix socket):
import os
# Detect App Engine environment
IS_APP_ENGINE = os.getenv('GAE_APPLICATION', None)
if IS_APP_ENGINE:
# Production: Connect via Unix socket
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ['DB_NAME'],
'USER': os.environ['DB_USER'],
'PASSWORD': os.environ['DB_PASSWORD'],
'HOST': f"/cloudsql/{os.environ['CLOUD_SQL_CONNECTION_NAME']}",
'PORT': '', # Empty for Unix socket
}
}
else:
# Local development: Connect via Cloud SQL Proxy
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('DB_NAME', 'mydb'),
'USER': os.environ.get('DB_USER', 'postgres'),
'PASSWORD': os.environ.get('DB_PASSWORD', ''),
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
CRITICAL:
- App Engine connects via Unix socket at
/cloudsql/PROJECT:REGION:INSTANCE - Local development requires Cloud SQL Auth Proxy on
127.0.0.1:5432 - Never hardcode connection strings - use environment variables
3. Create app.yaml
runtime: python310
entrypoint: gunicorn -b :$PORT myproject.wsgi:application
env_variables:
DB_NAME: "mydb"
DB_USER: "postgres"
CLOUD_SQL_CONNECTION_NAME: "project-id:region:instance-name"
# Cloud SQL connection
beta_settings:
cloud_sql_instances: "project-id:region:instance-name"
handlers:
- url: /static
static_dir: static/
- url: /.*
script: auto
secure: always
CRITICAL:
beta_settings.cloud_sql_instancesenables the Unix socket at/cloudsql/...- DB_PASSWORD should be set via
gcloud app deployor Secret Manager, not in app.yaml
The 6-Step Setup Process
Step 1: Create Cloud SQL Instance
# Create PostgreSQL instance
gcloud sql instances create myinstance \
--database-version=POSTGRES_15 \
--tier=db-f1-micro \
--region=us-central1
# Create database
gcloud sql databases create mydb --instance=myinstance
# Create user
gcloud sql users create postgres \
--instance=myinstance \
--password=YOUR_SECURE_PASSWORD
Key Points:
- Use
POSTGRES_15or later for best compatibility db-f1-microis cheapest for dev ($7-10/month), usedb-g1-smallor higher for production- Note the connection name:
PROJECT_ID:REGION:INSTANCE_NAME
Step 2: Configure Django Project
requirements.txt:
Django>=5.1,<6.0
psycopg2-binary>=2.9.9
gunicorn>=23.0.0
whitenoise>=6.7.0
settings.py additions:
import os
# Security settings for production
DEBUG = os.environ.get('DEBUG', 'False') == 'True'
ALLOWED_HOSTS = [
'.appspot.com',
'.run.app',
'localhost',
'127.0.0.1',
]
# Static files with WhiteNoise
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MIDDLEWARE.insert(1, 'whitenoise.middleware.WhiteNoiseMiddleware')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
# Database connection pooling
DATABASES['default']['CONN_MAX_AGE'] = 60 # Keep connections open for 60 seconds
Why these settings:
CONN_MAX_AGE=60reduces connection overhead (Cloud SQL has connection limits)- WhiteNoise serves static files without Cloud Storage
ALLOWED_HOSTSmust include.appspot.comfor App Engine
Step 3: Set Up Local Development with Cloud SQL Proxy
Install Cloud SQL Auth Proxy:
# macOS
brew install cloud-sql-proxy
# Linux
curl -o cloud-sql-proxy https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.14.1/cloud-sql-proxy.linux.amd64
chmod +x cloud-sql-proxy
Run the proxy:
# Authenticate first
gcloud auth application-default login
# Start proxy (runs on 127.0.0.1:5432)
./cloud-sql-proxy PROJECT_ID:REGION:INSTANCE_NAME
# Or with specific port
./cloud-sql-proxy PROJECT_ID:REGION:INSTANCE_NAME --port=5432
Set environment variables for local dev:
export DB_NAME=mydb
export DB_USER=postgres
export DB_PASSWORD=your_password
export DEBUG=True
Key Points:
- Proxy creates a secure tunnel to Cloud SQL
- No need to w
...
Repository
jezweb/claude-skillsParent repository
Repository Stats
Stars213
Forks24
LicenseMIT License