Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support unix socket path as host in database url #132

Closed
kyle-hamlin opened this issue Apr 21, 2020 · 3 comments
Closed

Support unix socket path as host in database url #132

kyle-hamlin opened this issue Apr 21, 2020 · 3 comments

Comments

@kyle-hamlin
Copy link

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:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": "postgres",
        "USER": "postgres",
        "PASSWORD": "secret-stuff",
        "HOST": "/cloudsql/<project_id>:<region>:<instance_id>", 
        "PORT": "5432",
    }
}

However dj-database-url doesn't seem to be able to recognize the unix socket path as the host:

import dj_database_url

In [1]: dj_database_url.parse("postgres://postgres:secret-stuff@/cloudsql/<project_id>:<region>:<instance_id>/postgres")
Out[1]:
{'NAME': 'cloudsql/<project_id>:<region>:<instance_id>/postgres',
 'USER': 'postgres',
 'PASSWORD': 'secret-stuff',
 'HOST': '',
 'PORT': '',
 'CONN_MAX_AGE': 0,
 'ENGINE': 'django.db.backends.postgresql_psycopg2'}
@frenetic
Copy link

frenetic commented Oct 9, 2020

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).

@juliolvfilho
Copy link

juliolvfilho commented Dec 14, 2020

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!

jared-hardy added a commit to jared-hardy/dj-database-url that referenced this issue Oct 6, 2021
Add synthetic test correlating to Google Cloud SQL connector socket, to make sure Issue jazzband#132 is solved.
palfrey pushed a commit that referenced this issue Dec 12, 2022
* Use URL Parse library to decode % encodes

Use URL Parse library for what it's good for: to decode % URL encode strings. Don't do it here -- we're already importing a better library for that!

* Test revision with Google Cloud SQL connector

Add synthetic test correlating to Google Cloud SQL connector socket, to make sure Issue #132 is solved

Co-authored-by: jared-hardy <jaredhardy@gmail.com>
@palfrey
Copy link
Member

palfrey commented Dec 12, 2022

Solved by #181

@palfrey palfrey closed this as completed Dec 12, 2022
LilaKelland added a commit to PHACDataHub/cloudrun-deployment-example that referenced this issue Jun 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants