Search code examples
djangopostgresqldjango-1.11

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


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.


Solution

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