Django.Db.Utils.Operationalerror Could Not Connect to Server

django.db.utils.OperationalError Could not connect to server

It can be some issues:

  1. PostgreSQL is not running. Check it with sudo service postgresql status
  2. Your PostgresSQl is not running on port 5432. You can check it typing sudo netstat -nl | grep postgres

  3. You have something wrong trying to connect to your db like the username, the password or the databasename. Check that they are what postgres ask for you to connect it and that is the db_name that you want to access to.

  4. Problems with postmaster.pid in postgres. It can happen because of a shutdown unproperly done. It makes to remind a pid alive that doesn't allow your server start. To fix it you have to:

     * rm /usr/local/var/postgres/postmaster.pid 
    * pg_resetxlog -f /usr/local/var/postgres

    After this it should run properly if you make the runserver of postgres

Help in Mac OSX: How to start PostgreSQL server on Mac OS X?

django.db.utils.OperationalError: could not connect to server: No such file or directory

This steps worked for me
Deactivate the virtualenv if there is.

  1. pyenv deactivate

  2. Install the required libraries.

    sudo apt-get install libpq-dev python-dev

  3. Install PostgreSQL.

    sudo apt-get install postgresql postgresql-contrib

  4. Start the psql shell.

    sudo -u postgres psql

  5. Setup the postgres user password by entering the following command and you will be prompted for password in the psql shell. Press Control + D to quit after it is done.

    \password postgres

  6. Create a new database user called django_user.

    `

    sudo -u postgres createuser django_user

  7. Go to the psql shell again.

    sudo -u postgres psql

  8. Create the database and name it django_db.

    CREATE DATABASE django_db;

  9. Set the password for django_user.

    ALTER USER django_user WITH PASSWORD '';

  10. Grant the privilege properly.

    GRANT ALL PRIVILEGES ON DATABASE django_db TO django_user;

  11. Exit the psql shell and activate the virtualenv.

    pyenv activate

  12. Install the psycopg2 package.

    pip install psycopg2

  13. Edit the Django project settings.py as follow.

    DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'django_db',
    'USER': 'django_user',
    'PASSWORD': '',
    'HOST': 'localhost',
    'PORT': '5432',
    }
    }

  14. Initialize the db.

    python manage.py migrate

  15. Start the Django project and see if everything works fine.

    python manage.py runserver 0.0.0.0:8000

Done =)

django.db.utils.OperationalError: could not connect to server: Connection refused

Change SQL_HOST to db in your .envs/.prod file. This will let the Web container reach the DB container and perform the migration.

Docker compose containers can be accessed with their service name from other containers.

django.db.utils.OperationalError: could not connect to server: No such file on Unix domain socket /tmp/.s.PGSQL.5432?

Sure, you can go to the code and place the pdb before the self.connection = self.get_new_connection(conn_params) line, something like:

import pdb; pdb.set_trace()
self.connection = self.get_new_connection(conn_params)

Run the server and once it reaches that pdb line you could interact with the code in runtime mode, it will show you a pdb console and then you can see if that environment variables are setted or not:

(pdb) p os.environ.get('DATABASE_NAME')
(pdb) p os.environ.get('DATABASE_USER')
...

It will allow you to see if the variables are setted and with the correct value.



Related Topics



Leave a reply



Submit