== Database Setup
The Yubico U2F Validation Server requires an SQL database for storing data.
Internally, u2fval uses http://www.sqlalchemy.org[SQLAlchemy] to connect to
the database. Depending on which SQL database you use you might need to install
an adapter for it. For a list of supported databases, and the required adapters,
see http://docs.sqlalchemy.org/en/rel_0_9/core/engines.html[SQLAlchemy Engine Configuration].

This document assumes you are using Ubuntu with the http://www.postgresql.org[Postgresql]
database. Start by installing Progresql and the needed adapter:

  # apt-get install postgresql python-psycopg2

To create a user and a database for u2fval, start by opening the psql prompt:

  # su postgres -c psql

Now create a user and a database:

  postgres=# create user u2fval with password 'password';
  postgres=# create database u2fval with owner u2fval;
  postgres=# \q

We're done. We now have a database named "u2fval", with a user named "u2fval",
and the password "password". This gives us the following connection string:

  postgresql://u2fval:password@localhost/u2fval

Edit the DATABASE_CONFIGURATION setting in the /etc/yubico/u2fval/u2fval.conf
file with the connection string, so it reads:

  DATABASE_CONFIGURATION = 'postgresql://u2fval:password@localhost/u2fval'

Save and close the file, and initialize the database for use with u2fval:

  # u2fval db init

That's it, the database is now configured and ready.

== Database migrations
From time to time, changes may be made to the database structure between
releases. When this happens, migration will have to be performed to adapt an
existing database without damaging any data. As a precaution, you should make a
full backup of your database before performing migrations. Once you've made
your backup, upgrade u2fval to the latest version, then run the following
command:

  # u2fval db upgrade

Running this command will migrate your database to the current structure.
Running this command when you're already at the lastest version does nothing.

Migrating a database requires "alembic", which you can get by running:

  $ pip install alembic

Alternatively you can install u2fval with the optional db_migration "extra":

  $ pip install u2fval[db_migration]
