Louise Billard
Louise Billard

Reputation: 5

Problem with the Postgresql database installation in django

I have a problem with the postgresql database in dajngo. When I run the command "python -m pip install psycopg2" it works. So I run the command "python manage.py makemigrations", I have this issues :

command result1
command result2

But when I run the command "pip freeze", this is the result :
Django==2.2.6
psycopg2==2.8.4
pytz==2019.3
sqlparse==0.3.0

And this is my settings.py file (Database):
my settings.py - database

This is my configuration:

Windows 10 64bits
- Django 2.2.6
- Psycopg2 2.8.4
- Postgresql 12
- pip 19.3.1

And C:\XXX\PostgreSQL\12\bin is in my PATH. I use the Visual Studio Code IDE.

Upvotes: 0

Views: 1083

Answers (1)

Gwang-Jin Kim
Gwang-Jin Kim

Reputation: 10163

0. install miniconda

1. conda basic usage

is super easy:

  • list all available conda environments: conda env list
  • create new conda env: conda create --name <my_env_name>
  • enter conda env: conda activate <my_env_name> or source activate <my_env_name>
  • deactivate conda env: conda deactivate or source deactivate

after entering conda env:

  • install packages by: conda install <package-name> mostly with -c <reponame> with favorite repos being: conda-forge, anaconda or in bioinformatics bioconda (very up to date)
  • remove packages by: conda remove <package-name>
  • list all packages in this env by: conda list

So you see all very similar to virtualenv.

2. search conda package install command

Simply google conda install <packagename> then you find mostly anaconda site with the correct command (-c whatever) and for which OS and versions ...

However, conda packages are mostly not top notch. Pip is more top notch. You solve this problem by installing pip into the conda env.

3. create environment and install python and pip

# choose your conda env name
conda create --name <my_django_project> 

# enter your conda env
source activate <my_django_project>

# install python, [ipython, jupyter]
conda install -c conda-forge python ipython jupyter
# just leave ipython and jupyter away if you don't want them
# you enforce versions by attaching `=<versionnumber>` 
# e.g.
conda install -c conda-forge python=3.8
# however, for my 32-bit computer it suggests 3.7.1
# and I would go with that

# you CAN install python v3.8, but I won't recommend it
# https://anaconda.org/conda-forge/python

# install pip # pip is automatically installed by conda
# when installing python like above.
# conda install -c conda-forge pip

So output after conda install -c conda-forge python is:

The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    wheel-0.32.3               |           py37_0          35 KB
    pip-18.1                   |           py37_0         1.7 MB
    python-3.7.1               |       h0371630_7        35.9 MB
    setuptools-40.6.3          |           py37_0         613 KB
    ------------------------------------------------------------
                                           Total:        38.3 MB

The following NEW packages will be INSTALLED:

_libgcc_mutex:   0.1-main               
ca-certificates: 2018.03.07-0           
certifi:         2018.11.29-py37_0      
libedit:         3.1.20170329-h6b74fdf_2
libffi:          3.2.1-h97ff0df_4       
libgcc-ng:       8.2.0-h9268252_1       
libstdcxx-ng:    8.2.0-h9268252_1       
ncurses:         6.1-he6710b0_1         
openssl:         1.1.1a-h7b6447c_0      
pip:             18.1-py37_0            
python:          3.7.1-h0371630_7       
readline:        7.0-h7b6447c_5         
setuptools:      40.6.3-py37_0          
sqlite:          3.26.0-h7b6447c_0      
tk:              8.6.8-hbc83047_0       
wheel:           0.32.3-py37_0          
xz:              5.2.4-h14c3975_4       
zlib:            1.2.11-h7b6447c_3  

So it installs pip automatically with python. So after this installation, you have also pip for installing into the conda environment!

What I do is I try to find conda installation of desired packages. Only if I cannot get the desired versions or the package at all using conda, I switch to pip install in this environment. Pip, since locally installed into the virtual env, will locally install everything into the conda environment. (By the way, I realize, maybe you used at the beginning a global pip and not a pip in your virtual env? maybe that was the problem?)

4. install postgresql into conda env

after entering conda env, do:

conda install -y -c conda-forge postgresql

5. set up postgresql in django

The sources I used were: [this][http://krischer.github.io/jane/setup/index.html#building-the-documentation] and [this][http://krischer.github.io/jane/setup/index.html#postgresql-setup].

The database which django uses is an inner database.

first initialize an outer (base) database:

initdb -D db_djangogirls # this is a database physically in your folder

# start postgres by using this db
postgres -D db_djangogirls & # runs postgres
# press RET to send it to background!

in that state - create non-super user

createuser --encrypted --pwprompt djangogirls
# pass '<yourpassword>' 2x

and create an inner database

createdb --owner=djangogirls djangogirls_db # this is the name of the inner database and that name of the inner you have to use in `settings.py`!

And this is the database you need to tell django this user and this password.

So you set the settings.py:

nano mysite/settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'djangogirls_db',
        'USER': 'djangogirls',
        'PASSWORD': 'djangogirls',
        'HOST': 'localhost',
        'PORT': '',
    }
}

and do:

python manage.py migrate

If things don't work, you have to kill postgres to restart.

# In linux, you monitor servers by:
ps aux | grep postgres

# the line with databasename - the first one - that number you use to kill the process
kill <number>
# e.g. the line
# <yourname> 30453  0.0  0.0  14760  2780 pts/6    S+   08:36   0:00 grep --color=auto postgres

But I don't know how you kill a postgres process in windows ... (maybe you can add how to do that in windows?)

restart postgresql by:

pg_ctl -D db_djangogirls -l logfile start

Upvotes: 1

Related Questions