Django inside WSL cannot connect to host database

117 views
Skip to first unread message

Robbi Nespu

unread,
May 31, 2023, 3:38:48 AM5/31/23
to Django users

Hello,
I am using windows machine to develop django project but now i want to make my program run on WSL and keep the DB are running on host (windows machine),

This mean the Django program inside WSL will try to connect to postgresql on WINDOWS.



> $ cat /etc/resolv.conf | grep name
> nameserver 172.21.48.1


The NMAP show the IP and PORT are correct

>
> $ nmap -p 5432 172.21.48.1
> Starting Nmap 7.80 ( https://nmap.org ) at 2023-05-31 15:26 +08
> Nmap scan report for WINDOWSMACHINE.mshome.net (172.21.48.1)
> Host is up (0.00049s latency).
>
> PORT STATE SERVICE
> 5432/tcp filtered postgresql
>
> Nmap done: 1 IP address (1 host up) scanned in 0.24 seconds


This are my setting-database

> DATABASES = {
> 'default': {
> 'ENGINE': 'django.db.backends.postgresql',
> 'NAME' : 'LMHOST',
> 'USER' : 'postgres',
> 'PASSWORD' : '',
> 'HOST': '172.21.48.1',
> 'PORT': 5432,
> }
> }

but the connection cannot be established. Here the output I get

> $ python manage.py migrate
> Traceback (most recent call last):
> File "/home/robbi/.virtualenvs/DJANGO_VIRTUALENV/lib/python3.9/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
> self.connect()
> File "/home/robbi/.virtualenvs/DJANGO_VIRTUALENV/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
> return func(*args, **kwargs)
> File "/home/robbi/.virtualenvs/DJANGO_VIRTUALENV/lib/python3.9/site-packages/django/db/backends/base/base.py", line 270, in connect
> self.connection = self.get_new_connection(conn_params)
> File "/home/robbi/.virtualenvs/DJANGO_VIRTUALENV/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
> return func(*args, **kwargs)
> File "/home/robbi/.virtualenvs/DJANGO_VIRTUALENV/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 275, in get_new_connection
> connection = self.Database.connect(**conn_params)
> File "/home/robbi/.virtualenvs/DJANGO_VIRTUALENV/lib/python3.9/site-packages/psycopg/connection.py", line 728, in connect
> raise ex.with_traceback(None)
> psycopg.OperationalError: connection failed: server closed the connection unexpectedly
> This probably means the server terminated abnormally
> before or while processing the request.


any clue on this? I have already tried disable firewall but still no luck

--
Regards
Robbi Nespu

PGP: D311 B5FF EEE6 0BE8 9C91 FA9E 0C81 FA30 3B3A 80BA

DieHardMan 300

unread,
May 31, 2023, 4:55:17 AM5/31/23
to Django users
Are you set "ALLOWED_HOSTS" in settings.py for  "172.21.48.1"  ?
ในวันที่ วันพุธที่ 31 พฤษภาคม ค.ศ. 2023 เวลา 14 นาฬิกา 38 นาที 48 วินาที UTC+7 Robbi Nespu เขียนว่า:
Reply all
Reply to author
Forward
0 new messages