def table_exists(conn, table_name, schema=False):
    """Checks if a table exists.

    Parameters
    ----------
    conn
        A Psycopg2 connection.
    table_name : str
        The table name.
    schema : str
        The schema to which the table belongs.

    """

    cur = conn.cursor()

    table_exists_sql = ('select * from information_schema.tables '
                        f'where table_name={table_name!r}')

    if schema:
        table_exists_sql += f' and table_schema={schema!r}'

    cur.execute(table_exists_sql)

    return bool(cur.rowcount)