Home > Back-end >  How does Django know the path to my database?
How does Django know the path to my database?

Time:06-16

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

CodePudding user response:

How does Django know where postgresql is located?

It doesn't, that is not what Django is responsible for. You run a PostgreSQL database server. That server listens to a port and knows where it stores the files that contain the data. Django will open connections to that database server, and talk to the database server through queries. The database is thus a standalone app.

For SQLite, it’s the full path to the database file.

For SQLite the situation is different, in that case there is a software package that manipulates a file, and thus there is no database server involved. This is not a standalone app, it is a library that manipulates the file.

Often for a database server you do not have access to the underlying files. It is for example possible that the server runs on a different machine. This might be safer, since a hacker that has somehow managed to get access to the web server might have access to the files, and to the database by obtaining the password, but not to other databases that run on the same database server for example.

CodePudding user response:

Django connect to your database server with socket(host : port), not file system (c:\path\to\your\database\server), so that you can run the django and the database server in different palce. This is the most common connection method. redis server, mongodb server etc.

  • Related