簡體   English   中英

505錯誤無法連接到服務器:連接被拒絕服務器是否在主機“ 127.0.0.1”上運行並接受端口5432上的TCP / IP連接?

[英]505 Error could not connect to server: Connection refused Is the server running on host “127.0.0.1” and accepting TCP/IP connections on port 5432?

這是我在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"?

查看您發布的跟蹤信息后,我注意到了這一點:

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:區域:instance

您確定所傳遞的項目ID是正確的嗎? 您的項目ID是“ bs”嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM