= The Trac Environment

[[TracGuideToc]]
[[PageOutline(2-5,Contents,pullout)]]

Trac uses a directory structure and a database for storing project data. The directory is referred to as the '''environment'''.

Trac supports [http://sqlite.org/ SQLite], [http://www.postgresql.org/ PostgreSQL] and [http://mysql.com/ MySQL] databases. With PostgreSQL and MySQL you have to create the database before running `trac-admin initenv`.

== Creating an Environment

A new Trac environment is created using the [TracAdmin#initenv initenv] command:
{{{#!sh
$ trac-admin /path/to/myproject initenv
}}}

`trac-admin` will ask you for the name of the project and the [#DatabaseConnectionStrings database connection string].

A base configuration can be specified using the `--inherit` or `--config` options. The `--inherit` option can be specified one or more times to utilize a [TracIni#GlobalConfiguration global configuration]. The options specified in a `--config` file will be used to override the defaults when initializing `trac.ini`. See below for an [#SourceCodeRepository example] of defining repositories in a configuration file.

=== Useful Tips

 - Place your environment's directory on a filesystem which supports sub-second timestamps, as Trac monitors the timestamp of its configuration files and changes happening on a filesystem with too coarse-grained timestamp resolution may go undetected in Trac < 1.0.2. This is also true for the location of authentication files when using TracStandalone.

 - The user under which the web server runs will require file system write permission to the environment directory and all the files inside. Please remember to set the appropriate permissions. The same applies to the source code repository, although the user under which Trac runs will only require write access to a Subversion repository created with the BDB file system; for other repository types, check the corresponding plugin's documentation.

 - Non-ascii environment paths are not supported.

 - TracPlugins located in a [TracIni#inherit-plugins_dir-option shared plugins directory] that is defined in an [TracIni#GlobalConfiguration inherited configuration] are not enabled by default, in contrast to plugins in the environment `plugins` directory. Hence, if they need to create extra tables, for example, the tables will not be created during environment creation and you'll need to [TracUpgrade#UpgradetheTracEnvironment upgrade the environment]. Alternatively you can avoid the need to upgrade the environment by explicitly enabling the plugin(s) in a configuration file using the `--inherit` or `--config` option. See TracAdmin#FullCommandReference for more information.

{{{#!div style="border: 1pt dotted; margin: 1em;"
**Caveat:** don't confuse the //Trac environment directory// with the //source code repository directory//.

This is a common beginners' mistake.
It happens that the structure for a Trac environment is loosely modeled after the Subversion repository directory structure, but those are two disjoint entities and they are not and //must not// be located at the same place.
}}}

== Database Connection Strings

You will need to specify a database connection string at the time the environment is created. The default is SQLite, which is sufficient for most projects. The SQLite database file is stored in the environment directory, and can easily be [wiki:TracBackup backed up] together with the rest of the environment.

Note that if the username or password of the connection string (if applicable) contains the `:`, `/` or `@` characters, they need to be [http://meyerweb.com/eric/tools/dencoder/ URL encoded].
{{{#!sh
$ python -c "import urllib; print urllib.quote('password@:/123', '')"
password%40%3A%2F123
}}}

=== SQLite Connection String

The connection string for an SQLite database is:
{{{
sqlite:db/trac.db
}}}
where `db/trac.db` is the path to the database file within the Trac environment.

=== PostgreSQL Connection String

The connection string for PostgreSQL is a bit more complex. For example, to connect to a PostgreSQL database named `trac` on `localhost` for user `johndoe` and password `letmein`, use:
{{{
postgres://johndoe:letmein@localhost/trac
}}}

If PostgreSQL is running on a non-standard port, for example 9342, use:
{{{
postgres://johndoe:letmein@localhost:9342/trac
}}}

On UNIX, you might want to select a UNIX socket for the transport, either the default socket as defined by the PGHOST environment variable:
{{{
postgres://user:password@/database
}}}

or a specific one:
{{{
postgres://user:password@/database?host=/path/to/socket/dir
}}}

See the [http://www.postgresql.org/docs/ PostgreSQL documentation] for detailed instructions on how to administer [http://postgresql.org PostgreSQL].
Generally, the following is sufficient to create a database user named `tracuser` and a database named `trac`:
{{{#!sh
$ createuser -U postgres -E -P tracuser
$ createdb -U postgres -O tracuser -E UTF8 trac
}}}

When running `createuser` you will be prompted for the password for the `tracuser`. This new user will not be a superuser, will not be allowed to create other databases and will not be allowed to create other roles. These privileges are not needed to run a Trac instance. If no password is desired for the user, simply remove the `-P` and `-E` options from the `createuser` command. Also note that the database should be created as UTF8. LATIN1 encoding causes errors, because of Trac's use of unicode.

Under some default configurations (Debian), run the `createuser` and `createdb` scripts as the `postgres` user:
{{{#!sh
$ sudo su - postgres -c 'createuser -U postgres -S -D -R -E -P tracuser'
$ sudo su - postgres -c 'createdb -U postgres -O tracuser -E UTF8 trac'
}}}

Trac uses the `public` schema by default, but you can specify a different schema in the connection string:
{{{
postgres://user:pass@server/database?schema=yourschemaname
}}}

=== MySQL Connection String

The format of the MySQL connection string is similar to PostgreSQL, with the `postgres` scheme being replaced by `mysql`. For example, to connect to a MySQL database on `localhost` named `trac` for user `johndoe` with password `letmein`:
{{{
mysql://johndoe:letmein@localhost:3306/trac
}}}

=== Changing Database Backend

The TracAdmin `convert_db` command allows migrating between SQLite, MySQL and PostgreSQL databases.

[[TracAdminHelp(convert_db)]]

== Deleting a Trac Environment

The Trac environment can be deleted using standard filesystem utilities to delete the directory that was passed to the `initenv` command. For an environment using SQLite, this will also delete the SQLite database, which resides on-disk in the environment `db` directory. For an environment using PostgreSQL or MySQL, you will need to use the associated database utilities to delete the database.

== Source Code Repository

A single environment can be connected to more than one repository. However, by default Trac is not connected to any source code repository, and the ''Browse Source'' navigation item will not be displayed. There are several ways to connect repositories to an environment, see TracRepositoryAdmin.

Repositories can be defined at the time of environment creation by specifying a configuration file to the `initenv` command using the `--config` or `--inherit` options. Cached repositories will be synchronized if the repository connector(s) are enabled. Example:

{{{#!sh
$ trac-admin $ENV initenv --config=config.ini
$ cat config.ini
[components]
tracopt.versioncontrol.* = enabled

[repositories]
.dir = /path/to/default/type/repos
repos1.type = git
repos1.dir = /path/to/git/repos
repos2.type = svn
repos2.dir = /path/to/svn/repos
}}}

== Directory Structure

An environment consists of the following files and directories:

 * `README` - Brief description of the environment.
 * `VERSION` - Environment version identifier.
 * `files`
  * `attachments` - Attachments to wiki pages and tickets.
 * `conf`
  * `trac.ini` - Main [TracIni configuration file].
 * `db`
  * `trac.db` - The SQLite database, if you are using SQLite.
 * `htdocs` - Directory containing web resources, which can be referenced in templates using the path `/chrome/site/...`.
 * `log` - Default directory for log files when `file` logging is enabled and a relative path is given.
 * `plugins` - Environment-specific [wiki:TracPlugins plugins].
 * `templates` - Custom Genshi environment-specific templates.
  * `site.html` - Method to [TracInterfaceCustomization#SiteAppearance customize] the site header, footer, and style.

----
See also: TracAdmin, TracBackup, TracIni
