configure Django db setting

weixin_34402408發表於2017-04-20
Configure the Django Database Settings

Next, we need to specify PostgreSQL as our database in the settings file. As this is the local database, we need to redefine the DATABASES variable in the testing and developing settings files. Edit both files, settings/developing.py and settings/testing.py and add:

try:
    db_pass = os.environ["DB_PASS"]
except KeyError:
    print("Error: environment variable DB_PASS must be set.")
    exit(1)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'db_name',
        'USER': 'user_name'
        'PASSWORD': db_pass,
        'HOST': '',
        'PORT': '',
    }
}
CREATE DATABASE db_name;
CREATE USER  user_name;
ALTER USER user_name WITH PASSWORD 'mypassword'; 
ALTER ROLE user_name SET client_encoding TO 'utf8';
ALTER ROLE user_name SET default_transaction_isolation TO 'read committed';
ALTER ROLE user_name SET timezone TO 'EST’;
GRANT ALL PRIVILEGES ON DATABASE db_name TO user_name;

Ok, now we are ready to check and sync and migrate our database:

$ python manage.py check
$ python manage.py makemigrations
$ python manage.py migrate

You only need to migrate the database in one environment (developing or testing). This is because the migration is applied to the database, which is the same for both of them.

$ python manage.py createsuperuser

相關文章