[Python] How does Django know the path to my database?

Discussão em 'Python' iniciado por Stack, Fevereiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    Just following the Django tutorials and decided I would do them with Postgresql instead of SQLlite.

    I added the following to my settings file and everything worked:

    DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': 'django_tutorial',
    'USER': 'django_admin',
    'PASSWORD': 'password123',
    'HOST': '127.0.0.1',
    'PORT': '5432'
    }
    }

    django_tutorial=> \dt
    List of relations
    Schema | Name | Type | Owner
    --------+----------------------------+-------+--------------
    public | accounts | table | django_admin
    public | auth_group | table | django_admin
    public | auth_group_permissions | table | django_admin
    public | auth_permission | table | django_admin
    public | auth_user | table | django_admin
    public | auth_user_groups | table | django_admin
    public | auth_user_user_permissions | table | django_admin
    public | django_admin_log | table | django_admin
    public | django_content_type | table | django_admin
    public | django_migrations | table | django_admin
    public | django_session | table | django_admin
    (11 rows)


    My question is this - How does Django know where postgresql is located? Originally I thought the name was supposed to be the C:\ path, but it only needed the DB name?

    Like for example the docs say this about sqllite:

    The name of the database to use. For SQLite, it’s the full path to the database file. When specifying the path, always use forward slashes, even on Windows (e.g. C:/homes/user/mysite/sqlite3.db).

    Continue reading...

Compartilhe esta Página