I am pretty new to Django and very new to docker. This is my first time using docker. I am trying to connect my django app in one docker container to postgreSQL database in another docker container. I was following a youtube tutorial when I encountered this problem. I wasn’t following the tutorial exactly as shown in the video though. I am using Docker on Windows 10.
My ‘docker-compose.yml’ file:
version: '3.8'
services:
db:
container_name: db_ramrobazar
image: postgres
restart: always
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- '5432:5432'
volumes:
- postgres_data:/var/lib/postgresql/data/
# env_file: .env
networks:
- djangonetwork
web:
container_name: web_ramrobazar
build:
context: .
depends_on:
- db
command: >
sh -c "python manage.py makemigrations &&
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
ports:
- "8000:8000"
env_file: .env
links:
- db:db
networks:
- djangonetwork
volumes:
- .:/usr/src/app
volumes:
postgres_data:
networks:
djangonetwork:
driver: bridge
My Dockerfile
file:
FROM python:3.8-slim-buster
# setting work directory
WORKDIR /usr/src/app
# env variables
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWEITEBYTECODE 1
# install psycopg dependencies
RUN apt-get update && apt-get install -y
build-essential
libpq-dev
&& rm -rf /var/lib/apt/lists/*
# install dependencies
RUN pip install --upgrade pip
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
My .env
file:
DEBUG=True
SECRET_KEY=mysecretkey
ALLOWED_HOSTS=*
DATABASE_URL=postgres://postgres:postgres@localhost:5432/postgres
# postgres
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=postgres
ENGINE=django.db.backends.postgresql_psycopg2
HOST=localhost
PORT=5432
My settings.py
file:
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3',
# }
# }
DATABASES = {
'default': {
'ENGINE': config('ENGINE'),
'NAME': config('POSTGRES_DB'),
'USER': config('POSTGRES_USER'),
'PASSWORD': 'postgres',
'HOST': config('HOST'),
'PORT': config('PORT'),
}
}
# DATABASE_URL = config('DATABASE_URL').replace("'", "")
# DATABASES = {
# 'default': dj_database_url.parse(DATABASE_URL, conn_max_age=600)
# }
When I delete all the migration files (like 0001_initial.py) and execute docker-compose up --build
, I get the following:
(venv) C:UsersashutDesktopramrobazar>docker-compose up --build
[+] Building 7.8s (12/12) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 32B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/python:3.8-slim-buster 3.0s
=> [1/7] FROM docker.io/library/python:3.8-slim-buster@sha256:234da35e659c02a785a1e9d2002e386bea80f293572f90ad0c 0.0s
=> [internal] load build context 1.3s
=> => transferring context: 1.01MB 1.2s
=> CACHED [2/7] WORKDIR /usr/src/app 0.0s
=> CACHED [3/7] RUN apt-get update && apt-get install -y build-essential libpq-dev && rm -rf /var/li 0.0s
=> CACHED [4/7] RUN pip install --upgrade pip 0.0s
=> CACHED [5/7] COPY requirements.txt . 0.0s
=> CACHED [6/7] RUN pip install -r requirements.txt 0.0s
=> [7/7] COPY . . 2.0s
=> exporting to image 1.5s
=> => exporting layers 1.4s
=> => writing image sha256:c1c5807098513138a5cf51f8c690c8ba8636f078ab32d6e9bcdf9930c670995a 0.0s
=> => naming to docker.io/library/ramrobazar_web 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
[+] Running 2/2
- Container db_ramrobazar Created 0.0s
- Container web_ramrobazar Recreated 0.1s
Attaching to db_ramrobazar, web_ramrobazar
db_ramrobazar |
db_ramrobazar | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_ramrobazar |
db_ramrobazar | 2022-05-20 07:11:29.657 UTC [1] LOG: starting PostgreSQL 14.3 (Debian 14.3-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
db_ramrobazar | 2022-05-20 07:11:29.657 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_ramrobazar | 2022-05-20 07:11:29.657 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_ramrobazar | 2022-05-20 07:11:29.670 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_ramrobazar | 2022-05-20 07:11:29.680 UTC [26] LOG: database system was shut down at 2022-05-20 06:52:31 UTC
db_ramrobazar | 2022-05-20 07:11:29.688 UTC [1] LOG: database system is ready to accept connections
web_ramrobazar | /usr/local/lib/python3.8/site-packages/django/core/management/commands/makemigrations.py:121: RuntimeWarning: Got an error checking a consistent migration history performed for database connection 'default': could not connect to server: Connection refused
web_ramrobazar | Is the server running on host "localhost" (127.0.0.1) and accepting
web_ramrobazar | TCP/IP connections on port 5432?
web_ramrobazar | could not connect to server: Cannot assign requested address
web_ramrobazar | Is the server running on host "localhost" (::1) and accepting
web_ramrobazar | TCP/IP connections on port 5432?
web_ramrobazar |
web_ramrobazar | warnings.warn(
web_ramrobazar | Migrations for 'account':
web_ramrobazar | ramrobazar/account/migrations/0001_initial.py
web_ramrobazar | - Create model User
web_ramrobazar | - Create model UserProfile
web_ramrobazar | Migrations for 'inventory':
web_ramrobazar | ramrobazar/inventory/migrations/0001_initial.py
web_ramrobazar | - Create model Brand
web_ramrobazar | - Create model Category
web_ramrobazar | - Create model Product
web_ramrobazar | - Create model ProductAttribute
web_ramrobazar | - Create model ProductAttributeValue
web_ramrobazar | - Create model ProductInventory
web_ramrobazar | - Create model ProductType
web_ramrobazar | - Create model Stock
web_ramrobazar | - Create model SoldStatus
web_ramrobazar | - Add field product_type to productinventory
web_ramrobazar | - Create model Media
web_ramrobazar | Traceback (most recent call last):
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
web_ramrobazar | self.connect()
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
web_ramrobazar | return func(*args, **kwargs)
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 225, in connect
web_ramrobazar | self.connection = self.get_new_connection(conn_params)
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
web_ramrobazar | return func(*args, **kwargs)
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 203, in get_new_connection
web_ramrobazar | connection = Database.connect(**conn_params)
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 122, in connect
web_ramrobazar | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
web_ramrobazar | psycopg2.OperationalError: could not connect to server: Connection refused
web_ramrobazar | Is the server running on host "localhost" (127.0.0.1) and accepting
web_ramrobazar | TCP/IP connections on port 5432?
web_ramrobazar | could not connect to server: Cannot assign requested address
web_ramrobazar | Is the server running on host "localhost" (::1) and accepting
web_ramrobazar | TCP/IP connections on port 5432?
web_ramrobazar |
web_ramrobazar |
web_ramrobazar | The above exception was the direct cause of the following exception:
web_ramrobazar |
web_ramrobazar | Traceback (most recent call last):
web_ramrobazar | File "manage.py", line 22, in <module>
web_ramrobazar | main()
web_ramrobazar | File "manage.py", line 18, in main
web_ramrobazar | execute_from_command_line(sys.argv)
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
web_ramrobazar | utility.execute()
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 440, in execute
web_ramrobazar | self.fetch_command(subcommand).run_from_argv(self.argv)
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 414, in run_from_argv
web_ramrobazar | self.execute(*args, **cmd_options)
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 460, in execute
web_ramrobazar | output = self.handle(*args, **options)
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 98, in wrapped
web_ramrobazar | res = handle_func(*args, **kwargs)
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 91, in handle
web_ramrobazar | self.check(databases=[database])
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 487, in check
web_ramrobazar | all_issues = checks.run_checks(
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/core/checks/registry.py", line 88, in run_checks
web_ramrobazar | new_errors = check(app_configs=app_configs, databases=databases)
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/core/checks/model_checks.py", line 36, in check_all_models
web_ramrobazar | errors.extend(model.check(**kwargs))
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/db/models/base.py", line 1461, in check
web_ramrobazar | *cls._check_indexes(databases),
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/db/models/base.py", line 1864, in _check_indexes
web_ramrobazar | connection.features.supports_covering_indexes
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/utils/functional.py", line 49, in __get__
web_ramrobazar | res = instance.__dict__[self.name] = self.func(instance)
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/features.py", line 84, in is_postgresql_11
web_ramrobazar | return self.connection.pg_version >= 110000
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/utils/functional.py", line 49, in __get__
web_ramrobazar | res = instance.__dict__[self.name] = self.func(instance)
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 354, in pg_version
web_ramrobazar | with self.temporary_connection():
web_ramrobazar | File "/usr/local/lib/python3.8/contextlib.py", line 113, in __enter__
web_ramrobazar | return next(self.gen)
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 639, in temporary_connection
web_ramrobazar | with self.cursor() as cursor:
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
web_ramrobazar | return func(*args, **kwargs)
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 284, in cursor
web_ramrobazar | return self._cursor()
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 260, in _cursor
web_ramrobazar | self.ensure_connection()
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
web_ramrobazar | return func(*args, **kwargs)
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
web_ramrobazar | self.connect()
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 91, in __exit__
web_ramrobazar | raise dj_exc_value.with_traceback(traceback) from exc_value
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
web_ramrobazar | self.connect()
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
web_ramrobazar | return func(*args, **kwargs)
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 225, in connect
web_ramrobazar | self.connection = self.get_new_connection(conn_params)
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
web_ramrobazar | return func(*args, **kwargs)
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 203, in get_new_connection
web_ramrobazar | connection = Database.connect(**conn_params)
web_ramrobazar | File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 122, in connect
web_ramrobazar | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
web_ramrobazar | django.db.utils.OperationalError: could not connect to server: Connection refused
web_ramrobazar | Is the server running on host "localhost" (127.0.0.1) and accepting
web_ramrobazar | TCP/IP connections on port 5432?
web_ramrobazar | could not connect to server: Cannot assign requested address
web_ramrobazar | Is the server running on host "localhost" (::1) and accepting
web_ramrobazar | TCP/IP connections on port 5432?
web_ramrobazar |
web_ramrobazar exited with code 1
When only the database ‘db_ramrobazar’ is running in docker and I try to execute python manage.py makemigrations
, I get the following:
(venv) C:UsersashutDesktopramrobazar>python manage.py makemigrations
C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangocoremanagementcommandsmakemigrations.py:121:
RuntimeWarning: Got an error checking a consistent migration history performed for database connection 'default': connection to server at "localhost" (::1), port 5432 failed: FATAL: password authentication failed for user "postgres"
warnings.warn(
Migrations for 'account':
ramrobazaraccountmigrations001_initial.py
- Create model User
- Create model UserProfile
Migrations for 'inventory':
ramrobazarinventorymigrations001_initial.py
- Create model Brand
- Create model Category
- Create model Product
- Create model ProductAttribute
- Create model ProductAttributeValue
- Create model ProductInventory
- Create model ProductType
- Create model Stock
- Create model SoldStatus
- Add field product_type to productinventory
- Create model Media
After getting the above result from makemigrations
, when I execute python manage.py migrate
, I get the following:
(venv) C:UsersashutDesktopramrobazar>python manage.py migrate
Traceback (most recent call last):
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangodbbackendsbasebase.py", line 244, in ensure_connection
self.connect()
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangoutilsasyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangodbbackendsbasebase.py", line 225, in connect
self.connection = self.get_new_connection(conn_params)
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangoutilsasyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangodbbackendspostgresqlbase.py", line 203, in get_new_connection
connection = Database.connect(**conn_params)
File "C:UsersashutDesktopramrobazarvenvlibsite-packagespsycopg2__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "localhost" (::1), port 5432 failed: FATAL: password authentication failed for user "postgres"
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:UsersashutDesktopramrobazarmanage.py", line 22, in <module>
main()
File "C:UsersashutDesktopramrobazarmanage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangocoremanagement__init__.py", line 446, in execute_from_command_line
utility.execute()
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangocoremanagement__init__.py", line 440, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangocoremanagementbase.py", line 414, in run_from_argv
self.execute(*args, **cmd_options)
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangocoremanagementbase.py", line 460, in execute
output = self.handle(*args, **options)
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangocoremanagementbase.py", line 98, in wrapped
res = handle_func(*args, **kwargs)
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangocoremanagementcommandsmigrate.py", line 91, in handle
self.check(databases=[database])
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangocoremanagementbase.py", line 487, in check
all_issues = checks.run_checks(
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangocorechecksregistry.py", line 88, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangocorechecksmodel_checks.py", line 36, in check_all_models
errors.extend(model.check(**kwargs))
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangodbmodelsbase.py", line 1461, in check
*cls._check_indexes(databases),
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangodbmodelsbase.py", line 1864, in _check_indexes
connection.features.supports_covering_indexes
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangoutilsfunctional.py", line 49, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangodbbackendspostgresqlfeatures.py", line 84, in is_postgresql_11
return self.connection.pg_version >= 110000
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangoutilsfunctional.py", line 49, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangodbbackendspostgresqlbase.py", line 354, in pg_version
with self.temporary_connection():
File "C:Program FilesPython310libcontextlib.py", line 135, in __enter__
return next(self.gen)
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangodbbackendsbasebase.py", line 639, in temporary_connection
with self.cursor() as cursor:
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangoutilsasyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangodbbackendsbasebase.py", line 284, in cursor
return self._cursor()
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangodbbackendsbasebase.py", line 260, in _cursor
self.ensure_connection()
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangoutilsasyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangodbbackendsbasebase.py", line 243, in ensure_connection
with self.wrap_database_errors:
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangodbutils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangodbbackendsbasebase.py", line 244, in ensure_connection
self.connect()
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangoutilsasyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangodbbackendsbasebase.py", line 225, in connect
self.connection = self.get_new_connection(conn_params)
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangoutilsasyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:UsersashutDesktopramrobazarvenvlibsite-packagesdjangodbbackendspostgresqlbase.py", line 203, in get_new_connection
connection = Database.connect(**conn_params)
File "C:UsersashutDesktopramrobazarvenvlibsite-packagespsycopg2__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: connection to server at "localhost" (::1), port 5432 failed: FATAL: password authentication failed for user "postgres"
I have tried changing the password of the ‘postgres’ user using the CLI like below but it doesn’t do anything.
# psql -U postgres -d postgres
psql (14.3 (Debian 14.3-1.pgdg110+1))
Type "help" for help.
postgres=# ALTER USER postgres WITH PASSWORD 'postgres';
ALTER ROLE
postgres=#
Please let me know if anything else is needed.
Edit
I replaced localhost
with db
. But then I got the error mentioned in this link. I tried the solution in one of the comments which said that managements commands like python manage.py makemigrations
should be run from the web container using the command docker-compose run --rm web python manage.py makemigrations
. But when I do that I get the following.
(venv) C:UsersashutDesktopramrobazar>docker-compose run --rm web python manage.py makemigrations
[+] Running 1/0
- Container db_ramrobazar Created 0.0s
[+] Running 2/2
- Container db_ramrobazar Started 3.2s
- Container web_ramrobazar Started 1.4s
/usr/local/lib/python3.8/site-packages/django/core/management/commands/makemigrations.py:121: RuntimeWarning: Got an error checking a consistent migration history performed for database connection 'default': could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
warnings.warn(
No changes detected
This is the same error I get when I run docker-compose up --build
or docker-compose up
mentioned above.
Edit Again
As suggested by Sardar Faisal, I removed
command: >
sh -c "python manage.py makemigrations &&
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
and ran docker-compose run -p 8000:8000 web_ramrobazar sh
but I got the error no such service: web_ramrobazar
. So I ran docker-compose run -p 8000:8000 web sh
instead and I got the following:
[+] Running 1/0
- Container db_ramrobazar Created 0.0s
[+] Running 2/2
- Container db_ramrobazar Started 3.1s
- Container web_ramrobazar Started 1.5s
# python manage.py makemigrations
/usr/local/lib/python3.8/site-packages/django/core/management/commands/makemigrations.py:121: RuntimeWarning: Got an error checking a consistent migration history performed for database connection 'default': could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
warnings.warn(
No changes detected
#
This is the same error mentioned above in the first edit.
Updated settings.py:
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3',
# }
# }
# DATABASES = {
# 'default': {
# 'ENGINE': config('ENGINE'),
# 'NAME': config('POSTGRES_DB'),
# 'USER': config('POSTGRES_USER'),
# 'PASSWORD': 'postgres',
# 'HOST': config('HOST'),
# 'PORT': config('PORT'),
# }
# }
DATABASE_URL = config('DATABASE_URL').replace("'", "")
DATABASES = {
'default': dj_database_url.parse(DATABASE_URL, conn_max_age=600)
}
Updated .env:
DEBUG=True
SECRET_KEY=mysecretkey
ALLOWED_HOSTS=*
DATABASE_URL=postgres://postgres:postgres@db:5432/postgres
# postgres
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=postgres
ENGINE=django.db.backends.postgresql_psycopg2
HOST=localhost
PORT=5432
2
Answers
So, it looks like that I had already found the answer in the first edit. I just forgot to revert a silly change that I made while trying out different things. Here's the solution:
In my
.env
file:Change the
DATABASE_URL
as shown above.I am using
python-decouple
for environment variables. You can find it here. I am also usingdj-database-url
for theDATABASE_URL
environment variable. You can find it here.In my
settings.py:
Make sure to run all your management commands as shown below (make appropriate changes according to your service name and other names):
This worked for me. I found the above commands here in a comment by the user Iain Shelvington.
Try to remove
Run the container with
docker-compose run -p 8000:8000 web_ramrobazar sh
And once it is in the shell then run you migration commands.
Otherwise you can write a management command that checks if the postgresql is running and then run the command.
Maybe you want to add to the web_ram..
Here is wait_for_db which you can write in one of your apps: