title: How does one set up multiple accounts with separate databases for Django on one server?
body: <p>What options are there for installing Django such that multiple users (each with an "Account") can each have their own database?</p>
  
  <p>The semantics are fairly intuitive. There may be more than one User for an Account. An Account has a unique database (and a database corresponds to an account). Picture WordpressMU. :)</p>
  
  <p>I've considered this:</p>
  
  <ol>
  <li><p><strong>External solution - Multiplex to multiple servers/daemons</strong></p>
  
  <p>Multiple Django installations, with each Django installation / project corresponding to an account that sets its own DATABASE_NAME, e.g.</p>
  
  <p>File system:</p>
  
  <pre><code>/bob
    /settings.py (contains DATABASE_NAME="bob")
  
  /sue
    /settings.py (contains DATABASE_NAME="sue")
  </code></pre>
  
  <p>Then having a Django instance running for each of bob and sue. I don't like this methodology- it feels brutish and it smells foul. But I'm confident it would work, and based on the suggestions it might be the cleanest, smartest way to do it.</p>
  
  <p>The apps can be stored elsewhere; the only thing that need be unique to the django configuration is the settings.py (and even there, only DATABASE_NAME, etc. need be different, the rest can be imported).</p>
  
  <p>(Incidentally, I'm using lighttpd and FastCGI.)</p></li>
  <li><p><strong>Internal solution - Django multiplexing database settings</strong></p>
  
  <p>On the other hand, I've thought of having one single Django installation, and</p>
  
  <p>(a) Adding a "prefix_" to each database table, corresponding to account of the logged-in user; or</p>
  
  <p>(b) Changing the database according to the account of the User that is logged in.</p>
  
  <p>I'd be particularly interested in seeing the "Django way" to do these (hoping that it's something dead-simple). For example, middleware that takes a Request's User and changes the django.conf.SETTINGS['DATABASE_NAME'] to the database for this user's account.</p>
  
  <p>This raises red flags, viz. Is this thread-safe? i.e. Does changing django.conf.SETTINGS affect other processes? Is there just an inherent danger in changing django.conf.SETTINGS -- would the DB connection be setup already? Is restarting the DB connection part of the public API? -- I'm going to have a look at the Django source when I look to this problem again.</p>
  
  <p>I'm conscious that 2(a) and (b) could require User authentication to be stored and accessed in a different mechanism that the core.</p></li>
  </ol>
  
  <p>For now, I'm going to go with the external mapping at the webserver layer- it's simplest and cleanest for now. However, I don't like the idea of FastCGI daemons running for every account- it seems to needlessly waste memory, particularly if there will be 2000+ accounts. However, I'd like to keep this discussion open as it's an interesting problem and the solution doesn't seem ideal for certain cases.</p>
  
  <p>Comments duly appreciated.
  Cheers</p>