505错误无法连接到服务器:连接被拒绝服务器是否在主机“ 127.0.0.1”上运行并接受端口5432上的TCP / IP连接?

时间:2019-08-29 05:39:13

标签: django google-app-engine google-cloud-platform psql

这是我第一个使用django在Google Cloud Console上发布的项目,但它给出了错误:无法连接到服务器:连接被拒绝     服务器是否在主机“ 127.0.0.1”上运行并接受     端口5432上的TCP / IP连接? 加载其他页面,但是当我想连接到psql db时,这是在将debug设置为True后得到的错误消息。以下是我的数据库设置和pg_hba.conf文件。我必须在这里错过一些重要的东西。


    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'HOST': '/cloudsql/appname:europe-west1:instance name',
            'USER': '',
            'PASSWORD': '',
            'NAME': '',
            'PORT': '5432'

        }
    }
else:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'HOST': '127.0.0.1',
            'PORT': '5432',
            'NAME': '',
            'USER': '',
            'PASSWORD': '',
        }
    }
# TYPE  DATABASE        USER            ADDRESS                 METHOD
#"local" is for Unix domain socket connections only
local   all             all                                     trust
#IPv4 local connections:
host    all             all             127.0.0.1/32            trust
#IPv6 local connections:
host    all             all             ::1/128                 trust
#Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust
host    all             all             197.237.174.222/32      md5
host    all             all              ::/0                   md5

这是在db设置中删除if else之后的新异常

Environment:


Request Method: POST
Request URL: http://bs.appspot.com/admin/login/?next=/admin/

Django Version: 2.2.3
Python Version: 3.6.8
Installed Applications:
['jet.dashboard',
 'jet',
 'bs.apps.BylangappConfig',
 'crispy_forms',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'users.apps.UsersConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/env/lib/python3.6/site-packages/django/db/backends/base/base.py" in ensure_connection
  217.                 self.connect()

File "/env/lib/python3.6/site-packages/django/db/backends/base/base.py" in connect
  195.         self.connection = self.get_new_connection(conn_params)

File "/env/lib/python3.6/site-packages/django/db/backends/postgresql/base.py" in get_new_connection
  178.         connection = Database.connect(**conn_params)

File "/env/lib/python3.6/site-packages/psycopg2/__init__.py" in connect
  126.     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)

The above exception (could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/cloudsql/bs:europe-west1:bylanguages-dev/.s.PGSQL.5432"?
) was the direct cause of the following exception:

File "/env/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/env/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/env/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/env/lib/python3.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)

File "/env/lib/python3.6/site-packages/django/contrib/admin/sites.py" in login
  399.         return LoginView.as_view(**defaults)(request)

File "/env/lib/python3.6/site-packages/django/views/generic/base.py" in view
  71.             return self.dispatch(request, *args, **kwargs)

File "/env/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
  45.         return bound_method(*args, **kwargs)

File "/env/lib/python3.6/site-packages/django/views/decorators/debug.py" in sensitive_post_parameters_wrapper
  76.             return view(request, *args, **kwargs)

File "/env/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
  45.         return bound_method(*args, **kwargs)

File "/env/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "/env/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
  45.         return bound_method(*args, **kwargs)

File "/env/lib/python3.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)

File "/env/lib/python3.6/site-packages/django/contrib/auth/views.py" in dispatch
  61.         return super().dispatch(request, *args, **kwargs)

File "/env/lib/python3.6/site-packages/django/views/generic/base.py" in dispatch
  97.         return handler(request, *args, **kwargs)

File "/env/lib/python3.6/site-packages/django/views/generic/edit.py" in post
  141.         if form.is_valid():

File "/env/lib/python3.6/site-packages/django/forms/forms.py" in is_valid
  185.         return self.is_bound and not self.errors

File "/env/lib/python3.6/site-packages/django/forms/forms.py" in errors
  180.             self.full_clean()

File "/env/lib/python3.6/site-packages/django/forms/forms.py" in full_clean
  382.         self._clean_form()

File "/env/lib/python3.6/site-packages/django/forms/forms.py" in _clean_form
  409.             cleaned_data = self.clean()

File "/env/lib/python3.6/site-packages/django/contrib/auth/forms.py" in clean
  196.             self.user_cache = authenticate(self.request, username=username, password=password)

File "/env/lib/python3.6/site-packages/django/contrib/auth/__init__.py" in authenticate
  73.             user = backend.authenticate(request, **credentials)

File "/env/lib/python3.6/site-packages/django/contrib/auth/backends.py" in authenticate
  20.             user = UserModel._default_manager.get_by_natural_key(username)

File "/env/lib/python3.6/site-packages/django/contrib/auth/base_user.py" in get_by_natural_key
  44.         return self.get(**{self.model.USERNAME_FIELD: username})

File "/env/lib/python3.6/site-packages/django/db/models/manager.py" in manager_method
  82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/env/lib/python3.6/site-packages/django/db/models/query.py" in get
  402.         num = len(clone)

File "/env/lib/python3.6/site-packages/django/db/models/query.py" in __len__
  256.         self._fetch_all()

File "/env/lib/python3.6/site-packages/django/db/models/query.py" in _fetch_all
  1242.             self._result_cache = list(self._iterable_class(self))

File "/env/lib/python3.6/site-packages/django/db/models/query.py" in __iter__
  55.         results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)

File "/env/lib/python3.6/site-packages/django/db/models/sql/compiler.py" in execute_sql
  1098.             cursor = self.connection.cursor()

File "/env/lib/python3.6/site-packages/django/db/backends/base/base.py" in cursor
  256.         return self._cursor()

File "/env/lib/python3.6/site-packages/django/db/backends/base/base.py" in _cursor
  233.         self.ensure_connection()

File "/env/lib/python3.6/site-packages/django/db/backends/base/base.py" in ensure_connection
  217.                 self.connect()

File "/env/lib/python3.6/site-packages/django/db/utils.py" in __exit__
  89.                 raise dj_exc_value.with_traceback(traceback) from exc_value

File "/env/lib/python3.6/site-packages/django/db/backends/base/base.py" in ensure_connection
  217.                 self.connect()

File "/env/lib/python3.6/site-packages/django/db/backends/base/base.py" in connect
  195.         self.connection = self.get_new_connection(conn_params)

File "/env/lib/python3.6/site-packages/django/db/backends/postgresql/base.py" in get_new_connection
  178.         connection = Database.connect(**conn_params)

File "/env/lib/python3.6/site-packages/psycopg2/__init__.py" in connect
  126.     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)

Exception Type: OperationalError at /admin/login/
Exception Value: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/cloudsql/bs:europe-west1:bs-dev/.s.PGSQL.5432"?

1 个答案:

答案 0 :(得分:0)

在查看您发布的跟踪信息后,我注意到了这一点:

Exception Type: OperationalError at /admin/login/
Exception Value: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/cloudsql/bs:europe-west1:bs-dev/.s.PGSQL.5432"?

您用于连接到Cloud SQL的格式是这种格式。

ProjectID:zone:instance

您确定要传递的项目ID是正确的吗?您的项目ID是“ bs”吗?