I aim to use postgres as default database for my django models. I am using docker-compose
for the postgres and it seems to be up and running.
version: '3.2'
services:
postgres:
image: postgres:13.4
environment:
POSTGRES_DB: backend_db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
volumes:
- database-data:/var/lib/postgresql/data/
ports:
- 5432:5432
networks:
- postgres
volumes:
database-data:
driver: local
networks:
postgres:
driver: bridge
However, when I am doing python3 manage.py migrate --database postgres
I am getting the following:
Operations to perform:
Apply all migrations: admin, auth, authentication, authtoken, contenttypes, sessions, token_blacklist, vpp_optimization
Running migrations:
No migrations to apply.
The problem is evident also when I am doing python3 manage.py runserver
, where I am getting:
Performing system checks...
System check identified no issues (0 silenced).
You have 31 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, authentication, authtoken, contenttypes, sessions, token_blacklist, vpp_optimization.
Run 'python manage.py migrate' to apply them.
April 11, 2022 - 05:32:10
Django version 3.1.7, using settings 'core.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
It appears like the command python3 manage.py migrate --database postgres
was not executed.
Here is the settings.py
part of the databases:
DATABASES = {
'default': get_config(
'DATABASE_URL',
'sqlite:///' BASE_DIR.child('db.sqlite3'),
cast=db_url
),
'postgres': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': get_config('DB_NAME', 'backend_db'),
'USER': get_config('DB_USER', 'postgres'),
'PASSWORD': get_config('DB_PASSWORD', 'postgres'),
'HOST': get_config('DB_HOST', 'postgres-service'),
'PORT': get_config('DB_PORT', '5432')
}
}
Any idea of what I am doing wrong here?
CodePudding user response:
The problem is that when you run:
python3 manage.py migrate --database postgres
You are explicitly defining that the database where you want to migrate the database schema is in postgres
. But when you run the app you use:
python3 manage.py runserver
And according to your settings is there anything that indicate that you will use the postgres db. So I think at this point you are usign the sqlite
db.
One thing that you can do is to setup an env variable to specify which database to use when running the app. Hope it can help.