How to run Django management commands against Google Cloud SQL
Asked Answered
S

1

9

Currently , I have deployed my django project on google app engine. I need to run python manage.py migrate command so that auth_user table should be created on my google cloud instance . But don't know where to run this command.

Salchunas answered 2/2, 2016 at 12:46 Comment(5)
Usually you just ssh into the server and run the commands in bash, but I dont know how it works with Google-app-engine. If this is your first django app, you'll find much more documentation and guides about deploying to Heroku, or to a vps such as DigitalOcean.Iata
@HåkenLid how it's possible?Salchunas
@HåkenLid I can't able to find a link regarding ssh into gae project. I'm really stuck on this step.Salchunas
I don't know either. That's why I suggested using a different hosting platform. Or you could use djangoappengine? djangoappengine.readthedocs.org/en/latest/index.html Looks like it makes it possible to run remote management commands from your local dev installation like this: manage.py remote migrateIata
classic appengine VM or Managed VM? you can SSH only into Managed VM instancesDight
F
10

If I get it right, your app runs on App Engine (sandboxed environment) and uses Cloud SQL.

1) Configure your database in settings.py as you can see below.

if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
    # Running on production App Engine, so use a Google Cloud SQL database.
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': '/cloudsql/project-id:instance-name',
            'NAME': 'database-name',
            'USER': 'root',
        }
    }
elif os.getenv('SETTINGS_MODE') == 'prod':
    # Running in development, but want to access the Google Cloud SQL instance in production.
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'INSTANCE': 'cloud-sql-instance-ip-address',
            'NAME': 'database-name',
            'USER': 'root',
            'PASSWORD': 'password',
        }
    }
else:
    # Running in development, so use a local MySQL database.
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'database-name',
            'USER': 'username',
            'PASSWORD': 'password',
        }
    }

2) Set environment variable SETTINGS_MODE to prod (or do not set if you want to access your local MySQL server).

3) Run the below command from your machine.

$ SETTINGS_MODE=prod python manage.py migrate

You can find more details in App Engine documentation - Management commands and Alternate development database and settings.

Flavoprotein answered 3/2, 2016 at 9:25 Comment(4)
I am using Django 1.9 and I had to use HOST instead of INSTANCE.Nephelometer
Thanks for sharing this .. It worked out quite well for me . I used this technique to solve my problem of running migrations on Google App Engine Flexible EnvironmentSinusitis
Hi. I can't seem to make this work. And it seems like the only understandable and doable solution I found so far. My only problem is the 2nd step "Set environment variable SETTINGS_MODE to prod" - this is confusing. Where do I set it? In app.yaml (tried it already)? Somewhere in google console (already looked and tried to find where I could..)? Or in settings.py (but it doesn't make too much sense for me)? Any info would help. Thanks in advance.Astonishing
How does this compare to the 'dev_appserver'? (docs: cloud.google.com/appengine/docs/standard/python3/…)Tiphani

© 2022 - 2024 — McMap. All rights reserved.