简体   繁体   English

migrate命令后,Django自动创建sqlite3但不自动创建postgresql数据库

[英]After migrate command, Django automatically creates sqlite3 but doesn't automatically create postgresql database

I'm using Windows 10 and Visual Studio Code IDE with python 3.8.5 .我将 Windows 10 和 Visual Studio Code IDE 与 python 3.8.5 一起使用。 I'm developing a web app called tasksplanner and I want to use django for my backend and postgresql v12 for my database.我正在开发一个名为 tasksplanner 的网络应用程序,我想将 django 用于我的后端,并将 postgresql v12 用于我的数据库。

By default django comes with sqlite3.默认情况下,django 带有 sqlite3。

In the file settings.py of my django project (called backend) by default I find this code:默认情况下,在我的 django 项目(称为后端)的文件 settings.py 中,我找到了以下代码:

DATABASES = {
     'default': {
         'ENGINE': 'django.db.backends.sqlite3',
         'NAME': BASE_DIR / 'db.sqlite3',
     }
}

If on the terminal I do:如果在终端上我这样做:

python manage.py migrate

or或者

python manage.py makemigrations tasksplanner 

django automatically creates an sqlite3 database called db.sqlite3 inside the backend project folder django 会在后端项目文件夹中自动创建一个名为 db.sqlite3 的 sqlite3 数据库

However, I want to use postgresql.但是,我想使用 postgresql。

So, following the instructions in here I modified my settings.py to look like:因此,按照此处的说明,我将 settings.py 修改为如下所示:

DATABASES = {
    # 'default': {
    #     'ENGINE': 'django.db.backends.sqlite3',
    #     'NAME': BASE_DIR / 'db.sqlite3',
    # }

    'default': {

            'ENGINE': 'django.db.backends.postgresql',

            'NAME': 'tasks_planner_db.mkd',

            'USER': 'postgres',

            'PASSWORD': '1234',

            'HOST': '127.0.0.1',

            'PORT': '5432',
    }

}

And I'm getting the following error:我收到以下错误:

conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: FATAL:  database "tasks-planner-db.mkd" does not exist

My questions are:我的问题是:

  1. Why isn't django creating the postgresql database automatically like it did with sqlite3?为什么 django 不像使用 sqlite3 那样自动创建 postgresql 数据库? What do I do to make it create the postgresql database automatically?我该怎么做才能让它自动创建 postgresql 数据库? Or do I have to create it manually?还是我必须手动创建它?

  2. Is the extension for the postgresql database correct? postgresql 数据库的扩展名是否正确? Should it be something different than *.mkd它应该与 *.mkd 不同吗

  3. I would also like the postgresql database to be created on the same location as the where sqlite3 database is created.我还希望在与创建 sqlite3 数据库的位置相同的位置创建 postgresql 数据库。 How can I achieve this?我怎样才能做到这一点? If I do :如果我做 :

    'NAME': BASE_DIR /'tasks-planner-db.mkd', 'NAME': BASE_DIR /'tasks-planner-db.mkd',

I get the error:我收到错误:

lib\site-packages\django\db\backends\postgresql\base.py", line 160, in get_connection_params
    if len(settings_dict['NAME'] or '') > self.ops.max_name_length():
TypeError: object of type 'WindowsPath' has no len()

Django can only create sqlite databases. Django 只能创建 sqlite 数据库。 When you change the database to a postgresql it asks you for your username password and port so it can connect to the existing database, not to create a knew one.当您将数据库更改为 postgresql 时,它会询问您的用户名密码和端口,以便它可以连接到现有数据库,而不是创建一个已知的数据库。

The solution to you problem is to create a postgresql database it can connect to with those details.您问题的解决方案是创建一个可以连接到这些详细信息的 postgresql 数据库。

Django can create a sqlite database for you because sqlite is purely file based. Django 可以为您创建一个 sqlite 数据库,因为 sqlite 纯粹是基于文件的。 It can not create a postgres database because it would require a server running it as well.它不能创建 postgres 数据库,因为它也需要一个运行它的服务器。

Therefore, you need to start up a postgres database server, create a user there and then tell django the access details.因此,你需要启动一个 postgres 数据库服务器,在那里创建一个用户,然后告诉 django 访问细节。 You can get such a server up and running very quickly by using a docker image: https://hub.docker.com/_/postgres您可以使用 docker 镜像快速启动并运行这样的服务器: https : //hub.docker.com/_/postgres

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM