def initialize_database_user(engine, database_name, database_user,
    database_pass):
    """Grant user's privileges.

    :param engine: engine to use to execute queries
    :param database_name: database name
    :param database_user: the username that you want to init
    :param database_pass: password for the user
    """
    if engine.name == 'mysql':
        from MySQLdb import escape_string
        for host in ['%%', 'localhost']:
            engine.execute(
                'GRANT ALL PRIVILEGES ON {0}.* TO {1}@\'{2}\' IDENTIFIED BY "{3}" '
                .format(database_name, database_user, host, escape_string(
                database_pass)))
    elif engine.name == 'postgresql':
        res = engine.execute("SELECT 1 FROM pg_roles WHERE rolname='{0}' ".
            format(database_user))
        if not res.first():
            from psycopg2.extensions import AsIs, adapt
            engine.execute('CREATE USER %s WITH PASSWORD %s ', (AsIs(
                database_user), adapt(database_pass)))
        engine.execute('grant all privileges on database  {0} to {1} '.
            format(database_name, database_user))
    else:
        raise Exception(
            'Database engine %(engine)s not supported. You need to manually adds privileges to %(database_user)s in order to access to the database %(database_name)s.'
             % {'engine': engine.name, 'database_user': database_user,
            'database_name': database_name})
