could not connect to postgresql server django lightsail

て烟熏妆下的殇ゞ 提交于 2020-12-13 04:43:46

问题


I keep running into the problem of not being able to connect to the database:

django.db.utils.OperationalError: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/opt/bitnami/postgresql/.s.PGSQL.5432/.s.PGSQL.5432"?

its my list of databases:

                                  List of databases
    Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
------------+----------+----------+-------------+-------------+-----------------------
 postgres   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
            |          |          |             |             | postgres=CTc/postgres
 template1  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
            |          |          |             |             | postgres=CTc/postgres
(3 rows)

and its the postgres config section of settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'HOST': '/opt/bitnami/postgresql/.s.PGSQL.5432',
        'PORT': '5432',
        'USER': 'postgres',
        'PASSWORD': 'mypass'
    }
}

I also tried answers to these two questions

  • Can't connect to Postgres database with Bitnami Django stack
  • Can't connect the postgreSQL with psycopg2 but none of them helped.

here is pip list:

 psycopg2-binary==2.8.6

my debug mode is false:

DEBUG = False

and in allowed hosts I have my private IP:

 ALLOWED_HOSTS = ['XXX.XXX.XXX.XXX']

回答1:


There is a confusion in Bitnami documentation. The file traceback says it can't find the file: .s.PGSQL.5432 This file is actually located in /tmp folder. (don't ask)

If you change your settings.py to have a /tmp folder as a Host it will work:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'postgres',
        # 'HOST': '/opt/bitnami/postgresql',
        'HOST': '/tmp/',
        'PORT': '5432',
        'USER': 'postgres',
        'PASSWORD': '1234'
    }
}

Another option is to create a symlink:

sudo ln -s /tmp/.s.PGSQL.5432 /opt/bitnami/postgresql/.s.PGSQL.5432

but I haven't tested it.



来源:https://stackoverflow.com/questions/64860073/could-not-connect-to-postgresql-server-django-lightsail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!