Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In django 1.11, how to allow users to login on a read-only database?

Tags:

I have 2 instances each running its own postgres database.

One is for production usage. The other is a read-only database that performs replication from the production database.

Both instances run the same django 1.11 application codebase.

When I attempt to login to the django read-only version, I cannot login because the very action of login apparently execute some update or insert statements.

I get an internal error about read-only database: cannot execute INSERT in a read-only transaction

What are my options if I want to allow users to access the read-only database using the same codebase?

UPDATE

I have already tried django-postgres-readonly. Same results.

like image 711
Kim Stacks Avatar asked Feb 28 '18 08:02

Kim Stacks


People also ask

Which database is best for Django?

MySQL and PostgreSQL work best with Django.


1 Answers

On the codebase that's talking to the read-only database

Step 1: install django-no-last-login v0.1.0

Step 2: inside settings.py add/change the following

SESSION_ENGINE = 'django.contrib.sessions.backends.file'

INSTALLED_APPS += [
    'nolastlogin',
]
NO_UPDATE_LAST_LOGIN = True

By default Django uses database for session engine, so switch to something else.

Also the plugin makes it easy to turn off the update last login behavior by Django.

Django auto updates the last login time. Since we want zero database writes, so we need to use that.

like image 194
Kim Stacks Avatar answered Sep 19 '22 05:09

Kim Stacks