jazzband / dj-database-url

Use Database URLs in your Django Application.
https://pypi.org/project/dj-database-url/
BSD 3-Clause "New" or "Revised" License
1.49k stars 205 forks source link

Django DB URL is not working with a DB hostname/IP inside of docker #123

Closed BeOleg closed 5 years ago

BeOleg commented 5 years ago

I use a hosted DB in Digital Ocean over private networking.

The private network IP is 10.138.x.x.

When I try to use the Django ORM to get an object I get:

/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py in connect(dsn, connection_factory, cursor_factory, **kwargs)
    124 
    125     dsn = _ext.make_dsn(dsn, **kwargs)
--> 126     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
    127     if cursor_factory is not None:
    128         conn.cursor_factory = cursor_factory

OperationalError: could not translate host name ""10.138.x.x"" to address: Name or service not known

However, when calling psycopg.connect with the same parameters a connection object is returned both when using the ID and the Hostname.

In [17]: psycopg2.connect(host='private-x.db.ondigitalocean.com', port='xxxx', user='xxxx', dbname='xxxx', password='xxxxx')      
Out[17]: <connection object at 0x7fd1bebd38d0; dsn: 'user=doadmin password=xxx dbname=x host=private-x.db.ondigitalocean.com port=25060', closed: 0>

In [18]: psycopg2.connect(host='10.138.x.x', port='xxxx', user='xxxx', dbname='xxxxx', password='xxxxxx')                                                      
Out[18]: <connection object at 0x7fd1bebd3a08; dsn: 'user=xxxx password=xxx dbname=xxxx host=10.138.x.x port=xxxx', closed: 0>

Any assistance will be appreciated.

mattseymour commented 5 years ago

Can you share the database string? Obfuscating any private information.

BeOleg commented 5 years ago

Solved. had double quotes (env param was quoted). You might consider escaping those :)