I’m encountering an authentication issue with a Django multi-tenant application using JWT tokens.
The Issue:
- Trying to restore a postgres pg_dump from a prod to local environment using pg_restore
- Database restores work fine within the same environment (prod→prod or local→local)
- When restoring prod database to local, users exist in the correct tenant schema (‘wwm’)
- However, authentication attempts return 401 Unauthorized for
/api/users/token/
- Token refresh attempts return Bad Request for
/api/users/token/refresh/
- When I hit an authenticated endpoint after the pg_restore the backend always returns “{
“detail”: “User not found”,
“code”: “user_not_found”
}”
Setup:
- Django with SimpleJWT for authentication
- PostgreSQL 16 in Docker containers
- The data is persisted in volumes hence on the host OS
- Both the dev and prod environment are docker compose networks with the only difference being that volumes are used to persist the postgres data in between docker-compose up and downs.
- The host OS and local OS are different (Linux and MacOS respectively)
- Custom user model (users_baseuser)
- DRF
- Multi-tenant setup using schema-based tenancy and a custom middleware for tenant selection
Database Investigation:
- Users are present in the tenant schema after restore
- Query
SET search_path TO wwm; SELECT * FROM users_baseuser;
shows users exist
Current Backup/Restore Process:
# Backup
docker exec $CONTAINER pg_dump -U $DB_USER -Fc -d $DB_NAME > "$BACKUP_FILE"
# Restore
docker exec -i $CONTAINER pg_restore -U $DB_USER -Fc -d $DB_NAME -c --if-exists
I have all containers running when I run the restore command, this doesn’t cause any errors when done (prod→prod or local→local).
Has anyone encountered similar issues with authentication after database restores in different environments? This has kept me busy for a couple of days and I’m at a loss as to what it could be seeing as the backup and restore works perfectly within the same environment.
Any insights would be appreciated!