Run-time database configuration

Sometimes the database connection settings are not known until run-time, when these values may be loaded from a configuration file or the environment. In these cases, you can defer the initialization of the database by specifying None as the database_name.

  1. database = PostgresqlDatabase(None) # Un-initialized database.
  2. class SomeModel(Model):
  3. class Meta:
  4. database = database

If you try to connect or issue any queries while your database is uninitialized you will get an exception:

  1. >>> database.connect()
  2. Exception: Error, database not properly initialized before opening connection

To initialize your database, call the init() method with the database name and any additional keyword arguments:

  1. database_name = raw_input('What is the name of the db? ')
  2. database.init(database_name, host='localhost', user='postgres')

For even more control over initializing your database, see the next section, Dynamically defining a database.