пятница, 18 мая 2018 г.

DJANGO POSTRESQL STACK


Инструкция по коннекту Django с базой Postgresql

  1. pip install psycopg2 само собой (поправка, теперь нужно ставить psycopg2-binary, типа название изменилось)
  2. sudo -u postgres -i
  3. psql
  4. В консоли базы делаем
    CREATE USER name;
    или сразу с паролем:
    CREATE USER name WITH PASSWORD 'password';
     
     
    CREATE DATABASE db_name OWNER name;
     
    Потом нам нужно будет пароль поставить, если сразу не задали, джанго без пароля выдаст ошибку
    
    ALTER USER user_name WITH PASSWORD 'new_password';
     
    Если нужно просмотреть все базы то это команда \l, там будет видно получилось у вас или нет 
     
  5. В настройках пишем:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': 'db_name',
            'USER': 'name',
            'PASSWORD': '',
            'HOST': 'localhost',
            'PORT': '',
        }
    }
  6. Потом вот эта выдержка из джанго документации:

    Optimizing PostgreSQL’s configuration

    Django needs the following parameters for its database connections:
    • client_encoding: 'UTF8',
    • default_transaction_isolation: 'read committed' by default, or the value set in the connection options (see below),
    • timezone: 'UTC' when USE_TZ is True, value of TIME_ZONE otherwise.
    If these parameters already have the correct values, Django won’t set them for every new connection, which improves performance slightly. You can configure them directly in postgresql.conf or more conveniently per database user with ALTER ROLE.
    Django will work just fine without this optimization, but each new connection will do some additional queries to set these parameters.


  7. Потом в окружении делаем
    python manage.py migrate
  8. Создаем админскую учетку
     python manage.py createsuperuser --username admin