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

Unable to connect to Postgres on a custom socket #19

Open
IlyaSemenov opened this issue Apr 24, 2020 · 2 comments
Open

Unable to connect to Postgres on a custom socket #19

IlyaSemenov opened this issue Apr 24, 2020 · 2 comments

Comments

@IlyaSemenov
Copy link

Currently, there is no way to connect to a Postgres running on a custom socket (e.g. in Google App Engine).

  1. with this Django settings.py (as per Django docs):
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "HOST": "/cloudsql/xyz",
        "NAME": "foo",
    }
}

jet-admin fails:

Connected to database engine "postgresql:///cloudsql/xyz/foo"
...
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL:  database "cloudsql/xyz/foo" does not exist
  1. with this Django settings.py:
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": "foo",
        "OPTIONS": {"host": "/cloudsql/xyz"}
    }
}

jet-admin constructs weird URL and ignores the options:

Connected to database engine "postgresql://foo"
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not translate host name "foo" to address: nodename nor servname provided, or not known
  1. with this Django settings.py:
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": "foo",
        "OPTIONS": {"host": "/cloudsql/xyz"}
    }
}

JET_DATABASE_EXTRA = "?host="/cloudsql/xyz"

it still fails to connect:

Connected to database engine "postgresql://foo?host=/cloudsql/xyz"
...
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not translate host name "foo?host=" to address: nodename nor servname provided, or not known
  1. with this Django settings.py:
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "HOST": "localhost",
        "NAME": "foo",
        "OPTIONS": {"host": "/cloudsql/xyz"}
    }
}

JET_DATABASE_EXTRA = "?host="/cloudsql/xyz"

it's finally constructing a working connection string:

Connected to database engine "postgresql://localhost/foo?host=/cloudsql/xyz"

but, in this case Django wouldn't connect to the database.

@IlyaSemenov
Copy link
Author

With authentication, this gets even weirder. For Django config:

DATABASES = {
    "default": {
        "NAME": "foo",
        "USER": "foo",
        "PASSWORD": "bang",
        "HOST": "",
        "PORT": "",
        "OPTIONS": {"host": "/tmp/myproject:us-east1:foo"},
        "ENGINE": "django.db.backends.postgresql",
    }
}

your build_engine_url returns:

postgresql://foo:bangfoo

🤦

@IlyaSemenov
Copy link
Author

Luckily the related code isn't hidden behind layers of closures, so a simple monkey patch would suffice:

import jet_bridge_base.db

jet_bridge_base.db.build_engine_url = lambda conf: env.str("DATABASE_URL")

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

1 participant