Handle special characters in database URL
See original GitHub issueIf the password contains special characters such as ? or /, the following exception is raised (here I tried to use “foo?bar” as a password):
Traceback (most recent call last):
File "./manage.py", line 19, in <module>
execute_from_command_line(sys.argv)
File "/var/www/myproject/dev/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/var/www/myproject/dev/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 376, in execute
sys.stdout.write(self.main_help_text() + '\n')
File "/var/www/myproject/dev/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 240, in main_help_text
for name, app in six.iteritems(get_commands()):
File "/var/www/myproject/dev/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 107, in get_commands
apps = settings.INSTALLED_APPS
File "/var/www/myproject/dev/env/local/lib/python2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
self._setup(name)
File "/var/www/myproject/dev/env/local/lib/python2.7/site-packages/django/conf/__init__.py", line 49, in _setup
self._wrapped = Settings(settings_module)
File "/var/www/myproject/dev/env/local/lib/python2.7/site-packages/django/conf/__init__.py", line 128, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/var/www/myproject/dev/env/local/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
__import__(name)
File "/var/www/myproject/dev/myproject/myproject/settings/base.py", line 175, in <module>
'default': dj_database_url.parse(get_env_variable('DATABASE_URL'))
File "/var/www/myproject/dev/env/local/lib/python2.7/site-packages/dj_database_url.py", line 84, in parse
'PORT': url.port or '',
File "/usr/lib/python2.7/urlparse.py", line 110, in port
port = int(port, 10)
ValueError: invalid literal for int() with base 10: 'foo'
I tried to escape the special characters with backslashes but then it got interpreted as "foo". I also tried to url-encode them, ie. replacing “?” by %63 but then the password returned by dj-database-url was “foo%63bar”.
The issue can be easily reproduced with the following URL: postgres://foo:foo?bar@localhost/foobar
.
Maybe I’m just missing something but if that’s the case I think it would be worth documenting it somewhere.
Issue Analytics
- State:
- Created 9 years ago
- Comments:12 (2 by maintainers)
Top Results From Across the Web
How to handle special characters in the password of a ...
How do I handle special characters in that string (e.g., $) so that it will actually function when I connect to my postgres...
Read more >Database URLs: should special characters in user name ...
If we decide to go with special handling, we have to come up with a grammar for our custom URL format (we do...
Read more >Using URL encoding to handle special characters in a ...
URL encoding is often required to convert special characters (such as "/", "&", "#", ...), because special characters: have special meaning in ...
Read more >In PostgreSQL URL I can't use a password containing ...
You have to use URI escapes for all problematic characters. For this user: CREATE ROLE "weird@name" PASSWORD '\/ @&?' LOGIN;.
Read more >Handling Special Characters
Oracle Enterprise Performance Management Cloud passwords, proxy passwords, and command parameter values may contain special characters. Special handling is ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Steps to Reproduce
export DB_URL=postgresql://foo:123XYZ##ABC%@example.com:5432/test_db
dj_database_url.parse(os.environ.get('DB_URL'))
Expected Results dj-database-url should either parse this properly or warn me with a helpful error if theres an issue
Actual results dj-database-url raises the error
ValueError: invalid literal for int() ...
because dj-database-url interprets the#
as a scheme delimiter or something.I should be able to work around this by encoding before parsing. Just leaving some details here in case it helps 😃 .
Yeah I’ve got the same. I think this issue should be reopened.
FWIW: You can use quote from urlib to properly parse DB connection string. I used
dj_database_url.parse(urllib.quote(os.environ.get('DATABASE_URL'), ':/@'))
instead of
dj_database_url.config()
and it works for my case