在django中安装Postgresql数据库的问题

Problem with the Postgresql database installation in django

我在dajngo 中的postgresql 数据库有问题。当我 运行 命令 "python -m pip install psycopg2" 它工作。所以我 运行 命令 "python manage.py makemigrations",我有这个问题:

command result1
command result2

但是当我 运行 命令 "pip freeze" 时,结果是这样的:
Django==2.2.6
psycopg2==2.8.4
pytz==2019.3
sqlparse==0.3.0

这是我的 settings.py 文件(数据库):
my settings.py - database

这是我的配置:

Windows 10 64 位
- Django 2.2.6
- Psycopg2 2.8.4
- PostgreSQL 12
- 点 19.3.1

并且 C:\XXX\PostgreSQL\bin 在我的路径中。 我使用 Visual Studio 代码 IDE.

0。安装 miniconda

1. conda 基本用法

超级简单:

  • 列出所有可用的 conda 环境:conda env list
  • 创建新的 conda 环境:conda create --name <my_env_name>
  • 输入 conda 环境:conda activate <my_env_name>source activate <my_env_name>
  • 停用 conda 环境:conda deactivatesource deactivate

进入后conda env:

  • 通过以下方式安装软件包:conda install <package-name> 主要使用 -c <reponame> 使用 最喜欢的 repos 是:conda-forgeanaconda 或生物信息学 bioconda(非常最新)
  • 通过以下方式删除软件包:conda remove <package-name>
  • 通过以下方式列出此环境中的所有包:conda list

所以你看到的都和virtualenv很像。

2。搜索 conda 包安装命令

只需 google conda install <packagename> 然后你会找到大部分 anaconda 站点 使用正确的命令 (-c whatever) 并且 OS 和版本 ...

然而,conda 软件包大多不是一流的。 Pip 更一流。 您可以通过将 pip 安装到 conda 环境中来解决此问题。

3。创建环境并安装 python 和 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

所以conda install -c conda-forge python后的输出是:

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

将安装以下新软件包:

_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  

因此它使用 python 自动安装 pip。 所以在这个安装之后,你还有 pip 用于安装到 conda 环境!

我所做的是尝试找到所需软件包的 conda 安装。 仅当我无法使用 conda 获得所需的版本或包时, 我在这个环境中切换到 pip install。 Pip,由于在本地安装到虚拟环境中,将在本地安装 一切都进入conda环境。 (顺便说一句,我意识到,也许你一开始使用了一个全局 pip 和 不是你的虚拟环境中的 pip 吗?也许这就是问题所在?)

4.将 postgresql 安装到 conda env

进入conda env后,执行:

conda install -y -c conda-forge postgresql

5.在 django

中设置 postgresql

我使用的来源是:[this][http://krischer.github.io/jane/setup/index.html#building-the-documentation] and [this][http://krischer.github.io/jane/setup/index.html#postgresql-setup].

django使用的数据库是内部数据库。

首先初始化一个外部(基础)数据库:

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!

在该状态下 - 创建非超级用户

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

并创建一个内部数据库

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`!

这是你需要告诉django的数据库 这个用户和这个密码。

所以你设置 settings.py:

nano mysite/settings.py

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

并做:

python manage.py migrate

如果事情不起作用,您必须杀死 postgres 才能重新启动。

# 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

但我不知道你如何在 windows 中终止一个 postgres 进程......(也许你可以在 windows 中添加如何做到这一点?)

通过以下方式重启 postgresql:

pg_ctl -D db_djangogirls -l logfile start