How to run Django management commands against Google Cloud SQL

前端 未结 1 1830
盖世英雄少女心
盖世英雄少女心 2020-12-09 12:37

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 b

相关标签:
1条回答
  • 2020-12-09 13:19

    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.

    0 讨论(0)
提交回复
热议问题