Closed kyle-hamlin closed 1 year ago
Readme states you can use percent encoding
. But it did not work for me.
Im trying to set up a dev environment on GCP (cloud run + cloud sql).
Workaround: You need to encode the HOST part of DATABASE_URL string and then decode again:
import dj_database_url, urllib.parse
# "postgres://" + user + ":" + password + "@" + urllib.parse.quote("/cloudsql/project_id:region:instance_id") + "/" + database
database_url = "postgres://user:password@%2Fcloudsql%2Fproject_id%3Aregion%3Ainstance_id/database"
config = dj_database_url.parse(database_url)
# {
# 'NAME': 'database',
# 'USER': 'user',
# 'PASSWORD': 'password',
# 'HOST': '/cloudsql/project_id%3Aregion%3Ainstance_id',
# 'PORT': '',
# 'CONN_MAX_AGE': 0,
# 'ENGINE': 'django.db.backends.postgresql_psycopg2'
# }
config['HOST'] = urllib.parse.unquote(config['HOST'])
# "/cloudsql/project_id:region:instance_id"
then works!
When running Django applications on Google Cloud serverless products: App Engine, Cloud Functions, and most recently Cloud Run, connecting to Cloud SQL instances requires the use of a unix socket as the database host. This works fine using the traditional
DATABASES
dict in the settings file:However
dj-database-url
doesn't seem to be able to recognize the unix socket path as the host: