简体   繁体   中英

could not connect to postgresql server django lightsail

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

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']

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM