Django PostgreSQL Database Password URL

I am using Django 1.9.2 and psyopg2 2.6.1 with Python 3.5.0 in a project that I created using http://cookiecutter-django.readthedocs.org/en/latest/ . I have a database configuration that looks like this:

import environ from django.utils.http import urlquote env = environ.Env() DATABASES = { # 1) This does not work. # 'default': env.db("DATABASE_URL", # default="postgres://myuser:% s@127.0.0.1 :5432/mydb" % "1234#abc") # 2) This does not work. # 'default': env.db("DATABASE_URL", # default="postgres://myuser:% s@127.0.0.1 :5432/mydb" % urlquote("1234#abc")) # 3) This works 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'mydb', 'USER': 'myuser', 'PASSWORD': '1234#abc', 'PORT': 5432, 'HOST': '127.0.0.1' } } 

My database password has # . I can successfully connect to option number 3, but with the other two I get the following errors:

1) ValueError: invalid literal for int() with base 10: '1234'

2) django.db.utils.OperationalError: FATAL: password authentication failed for user "myuser" .

How to send this c # password correctly in PostgreSQL?

+5
source share
1 answer

Try to encode it as a raw string, i.e. in your case:

  'default': env.db("DATABASE_URL", default="postgres://myuser:% s@127.0.0.1 :5432/mydb" % r"1234#abc") 

Send back your results.

0
source

Source: https://habr.com/ru/post/1243239/


All Articles