[Answered ]-GAE Django cloudsql socket

2đź‘Ť

âś…

It can’t connect to it because your local environment won’t recognize host /cloudsql/something:else, this is only be recognized on production environment.

You can follow these approach to make it working in dev and production environment.

Using the same production Cloud SQL instance in both production and local

First, you have to go on the Google Cloud Console to give access to your current ip.

Give access to your current ip

  1. Go to Cloud SQL console, click on your instance name.
  2. Click “edit” button
  3. in Assign IP Address, select “Assign IP Address”
  4. in below “Authorized IP Address”, add an IP address on your development computer.

Database setting

And then use the setting as following:

#!/usr/bin/env python
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'root',
        'PASSWORD': '<your-database-password>',
        'HOST': '<your-database-ip>',
        'NAME': '<your-database-name>',
    }
}

Use separate different databases for production and dev server

You can separate settings for production and local development server:

if os.getenv("SERVER_SOFTWARE", "").startswith("Google App Engine"):
    # production
    DATABASES = {
        # production database settings
    }
else:
    # local dev server
    DATABASES = {
        # local database settings
    }
👤Colin Su

Leave a comment