def initialize(g, app):
    """
    If postgresql url is defined in configuration params a
    scoped session will be created
    """
    if 'DATABASES' in app.config and 'POSTGRESQL' in app.config['DATABASES']:
        # Database connection established for console commands
        for k, v in app.config['DATABASES']['POSTGRESQL'].items():
            init_db_conn(k, v)

        if 'test' not in sys.argv:
            # Establish a new connection every request
            @app.before_request
            def before_request():
                """
                Assign postgresql connection pool to the global
                flask object at the beginning of every request
                """
                # inject stack context if not testing
                from flask import _app_ctx_stack
                for k, v in app.config['DATABASES']['POSTGRESQL'].items():
                    init_db_conn(k, v, scopefunc=_app_ctx_stack)
                g.postgresql_pool = pool

            # avoid to close connections if testing
            @app.teardown_request
            def teardown_request(exception):
                """
                Releasing connection after finish request, not required in unit
                testing
                """
                pool = getattr(g, 'postgresql_pool', None)
                if pool is not None:
                    for k, v in pool.connections.items():
                        v.session.remove()
        else:
            @app.before_request
            def before_request():
                """
                Assign postgresql connection pool to the global
                flask object at the beginning of every request
                """
                for k, v in app.config['DATABASES']['POSTGRESQL'].items():
                    init_db_conn(k, v)
                g.postgresql_pool = pool