简体   繁体   中英

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 . I'm developing a web app called tasksplanner and I want to use django for my backend and postgresql v12 for my database.

By default django comes with sqlite3.

In the file settings.py of my django project (called backend) by default I find this code:

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

However, I want to use postgresql.

So, following the instructions in here I modified my settings.py to look like:

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? What do I do to make it create the postgresql database automatically? Or do I have to create it manually?

  2. Is the extension for the postgresql database correct? Should it be something different than *.mkd

  3. I would also like the postgresql database to be created on the same location as the where sqlite3 database is created. How can I achieve this? If I do :

    '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. 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.

The solution to you problem is to create a postgresql database it can connect to with those details.

Django can create a sqlite database for you because sqlite is purely file based. It can not create a postgres database because it would require a server running it as well.

Therefore, you need to start up a postgres database server, create a user there and then tell django the access details. You can get such a server up and running very quickly by using a docker image: https://hub.docker.com/_/postgres

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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