在djang中安装Postgresql数据库时出现问题

2024-10-03 02:47:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我对dajngo的postgresql数据库有问题。当我运行命令“python-mpipinstallpsycopg2”时,它可以工作。所以我运行命令“python管理.pymakemigrations“,我有这个问题:

command result1
command result2

但当我运行命令“pip freeze”时,结果是:
Django==2.2.6
心理2==2.8.4
pytz==2019.3
sqlparse==0.3.0

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

这是我的配置:

Windows 10 64位
-Django 2.2.6
-心理学2.8.4
-Postgresql 12
-pip 19.3.1

C:\XXX\PostgreSQL\12\bin在我的路径中。 我使用visualstudio代码IDE。在


Tags: pipdjangopy命令数据库postgresqlcommandfreeze
1条回答
网友
1楼 · 发布于 2024-10-03 02:47:07

0。安装miniconda

1。conda基本用法

非常简单:

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

输入conda env后:

  • 安装程序包的方法是:conda install <package-name>大多数使用-c <reponame>和 最受欢迎的回购是:conda-forgeanaconda或生物信息学bioconda(非常最新)
  • 删除包的方式:conda remove <package-name>
  • 列出此环境中的所有包:conda list

所以你看到的都和virtualenv很相似。在

2。搜索conda包安装命令

只需google conda install <packagename>就可以找到大部分anaconda网站 使用正确的命令(-c whatever)以及操作系统和版本。。。在

然而,康达的包装大多不是一流的。皮普更顶尖。 把这个问题安装到环境中解决。在

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之后的输出是:

^{pr2}$

将安装以下新软件包:

_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得到想要的版本或软件包, 在这个环境中,我切换到pip install。 由于Pip是本地安装到虚拟env中的,所以它将在本地安装 一切都进入康达环境。 (顺便说一句,我意识到,也许你在一开始使用了一个全球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]和[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

如果事情不起作用,你必须杀死博士后才能重新开始。在

# 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

相关问题 更多 >