I’m currently working on a Django project where I’m using a virtual environment (venv) to run Django locally on my machine (Windows). Additionally, I have a PostgreSQL database running in a Docker container.
I’m encountering issues trying to establish a connection between my Django application and the PostgreSQL database. Whenever I attempt to run python manage.py makemigrations, I get the following error:
RuntimeWarning: Got an error checking a consistent migration history performed for database connection 'default': connection to server at "host.docker.internal" (141.31.78.207), port 5432 failed: Connection timed out (0x0000274C/10060)
Is the server running on that host and accepting TCP/IP connections?
warnings.warn(
No changes detected
Here are the relevant portions of my Django settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'SilverSurfer',
'USER': 'postgres',
'PASSWORD': "root",
'HOST': "host.docker.internal",
'PORT': '5432',
}
}
And here’s my Docker Compose file:
version: "3.9"
services:
db:
image: postgres:15.0
environment:
POSTGRES_DB: SilverSurfer
POSTGRES_USER: postgres
POSTGRES_PASSWORD: root
ports:
- "5432:5432"
volumes:
- ./db.sql:/docker-entrypoint-initdb.d/db.sql
It’s worth noting that my Docker container for the database is running:
CONTAINER ID IMAGE COMMAND CREATED STATUS
528ad4e3232f postgres:15.0 "docker-entrypoint.s…" 37 minutes ago Up 37 minutes
PORTS NAMES
0.0.0.0:5432->5432/tcp 02_db-db-1
I’ve tried connecting to the PostgreSQL database using psql, but I receive a similar timeout error.
Could someone please assist me in troubleshooting this issue? Any insights or suggestions would be greatly appreciated. Thank you in advance!
I experimented also with alternative hostnames such as "localhost", "0.0.0.0", and even my local IP address obtained from ipconfig ("172.16.98.1"). I encountered similar timeout errors.
Originally, I planned to have two separate Docker containers – one for Django and another for PostgreSQL. However, I encountered difficulties in establishing communication between the Django container and the PostgreSQL container. This setup also resulted in connection issues, preventing Django from connecting to the PostgreSQL database in the other Docker container.
With two Docker containers I also tried to create a network and specify the container name of the Postgre container in the Django DB settings, but that didn’t work either.
Full trace from error message trying with localhost
and 127.0.0.1
:
(myenv) PS C:UsersAbdulSoftware_ProjekteSilverSurfer4_Backend> python manage.py makemigrations
Traceback (most recent call last):
File "C:UsersAbdulSoftware_ProjekteSilverSurfer4_Backendmanage.py", line 22, in <module>
main()
File "C:UsersAbdulSoftware_ProjekteSilverSurfer4_Backendmanage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:UsersAbdulSoftware_ProjekteSilverSurfer4_BackendmyenvLibsite-packagesdjangocoremanagement__init__.py", line 442, in execute_from_command_line
utility.execute()
File "C:UsersAbdulSoftware_ProjekteSilverSurfer4_BackendmyenvLibsite-packagesdjangocoremanagement__init__.py", line 436, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:UsersAbdulSoftware_ProjekteSilverSurfer4_BackendmyenvLibsite-packagesdjangocoremanagementbase.py", line 412, in run_from_argv
self.execute(*args, **cmd_options)
File "C:UsersAbdulSoftware_ProjekteSilverSurfer4_BackendmyenvLibsite-packagesdjangocoremanagementbase.py", line 458, in execute
output = self.handle(*args, **options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersAbdulSoftware_ProjekteSilverSurfer4_BackendmyenvLibsite-packagesdjangocoremanagementbase.py", line 106, in wrapper
res = handle_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersAbdulSoftware_ProjekteSilverSurfer4_BackendmyenvLibsite-packagesdjangocoremanagementcommandsmakemigrations.py", line 156, in handle
loader.check_consistent_history(connection)
File "C:UsersAbdulSoftware_ProjekteSilverSurfer4_BackendmyenvLibsite-packagesdjangodbmigrationsloader.py", line 313, in check_consistent_history
applied = recorder.applied_migrations()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersAbdulSoftware_ProjekteSilverSurfer4_BackendmyenvLibsite-packagesdjangodbmigrationsrecorder.py", line 81, in applied_migrations
if self.has_table():
^^^^^^^^^^^^^^^^
File "C:UsersAbdulSoftware_ProjekteSilverSurfer4_BackendmyenvLibsite-packagesdjangodbmigrationsrecorder.py", line 57, in has_table
with self.connection.cursor() as cursor:
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersAbdulSoftware_ProjekteSilverSurfer4_BackendmyenvLibsite-packagesdjangoutilsasyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "C:UsersAbdulSoftware_ProjekteSilverSurfer4_BackendmyenvLibsite-packagesdjangodbbackendsbasebase.py", line 330, in cursor
return self._cursor()
^^^^^^^^^^^^^^
File "C:UsersAbdulSoftware_ProjekteSilverSurfer4_BackendmyenvLibsite-packagesdjangodbbackendsbasebase.py", line 306, in _cursor
self.ensure_connection()
File "C:UsersAbdulSoftware_ProjekteSilverSurfer4_BackendmyenvLibsite-packagesdjangoutilsasyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "C:UsersAbdulSoftware_ProjekteSilverSurfer4_BackendmyenvLibsite-packagesdjangodbbackendsbasebase.py", line 289, in ensure_connection
self.connect()
File "C:UsersAbdulSoftware_ProjekteSilverSurfer4_BackendmyenvLibsite-packagesdjangoutilsasyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "C:UsersAbdulSoftware_ProjekteSilverSurfer4_BackendmyenvLibsite-packagesdjangodbbackendsbasebase.py", line 270, in connect
self.connection = self.get_new_connection(conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersAbdulSoftware_ProjekteSilverSurfer4_BackendmyenvLibsite-packagesdjangoutilsasyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "C:UsersAbdulSoftware_ProjekteSilverSurfer4_BackendmyenvLibsite-packagesdjangodbbackendspostgresqlbase.py", line 275, in get_new_connection
connection = self.Database.connect(**conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersAbdulSoftware_ProjekteSilverSurfer4_BackendmyenvLibsite-packagespsycopg2__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfc in position 91: invalid start byte
2
Answers
You are connecting from the host machine to the docker container. You don’t need to (and should not) use the address
host.docker.internal
.Since you are publishing the port already,
it’s enought to use
localhost
or127.0.0.1
as theHOST
in this case.Note that if you later on change the application code also to a container and start it from the same compose file, the correct host is then
db
(the service name for the database).Press
Win + I
to open the system settings. Click Network & Internet from the menu options. In the next window, select Status on the left-hand side pane.Click Change adapter options to open the Network Connections tool.
or
Type
ipconfig /all
in CMD and press Enter. This will show all the information about your network adapter: Physical Address: This is the MAC address of your network adapter. DHCP Enabled: Indicates if the network connection is using DHCP or Static IP AddressDo Not Use Hyper-V Virtual Ethernet Adapter IP Address.
Replace
use that
IP:PORT(DB) Eg 192.168.1.111:5432
to connect with postgres DB